I try to POST data from page to the controller, but I receive the unexpected issue.
First of all - while I do not tried the post method all works fine. I mean the mapping for the GET queries works correctly:
@Controller
public class ProgramPage {
@RequestMapping(value = "/program/add", method = RequestMethod.GET)
public ModelAndView getAddProgramPage(){
//business logic - adding objects and mapping to the html page works correctly
}
}
But after I added the simply POST form to the my HTML page and send data I receive the "Whitelabel Error Page".
- html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<title>Add program</title>
</head>
<body>
<form action="/addProgram" method="post"
style="width: 50%">
<div>
<input class='w3-input' name='exerciseLine' type='text'/>
</div>
<div >
<input type="submit" value="Save"/>
</div>
</form>
- the POST body that I receive in the DevTools:
{
exerciseLine: test
}
- the controller:
@Controller
public class ProgramPage {
@RequestMapping(value = "/addProgram", method = RequestMethod.POST)
public String testMethod(
@RequestParam(required = false, defaultValue = "-1") String exerciseLine
){
System.out.println(); //breakpoint
return "otherpage";
}
}
I execute the code in the debug mode with breakpoint in the testMethod()
method - the code do not enter into it.
So, why I can't handle this POST method and how to fix it?
Additional info:
- application.properties:
#Database settings
spring.datasource.url=jdbc:mysql://localhost:3306/db?useUnicode=yes&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=utf-8;
spring.datasource.sql-script-encoding=UTF-8
spring.jpa.open-in-view=false
#Disable thymeleaf cashing
spring.template.cache = false;
- gradle:
plugins {
id 'org.springframework.boot' version '2.7.3'
id 'io.spring.dependency-management' version '1.0.13.RELEASE'
id 'java'
}
group = 'com.hltr'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.30'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}
tasks.named('test') {
useJUnitPlatform()
}
CodePudding user response:
i notice your project import spring-boot-starter-security.The http status of 403 is auth error usually.