Java Springboot提交未找到控制器
当我在我的HTML表单上单击submit时,它没有到达/greeting端点Java Springboot提交未找到控制器,java,spring,spring-boot,thymeleaf,Java,Spring,Spring Boot,Thymeleaf,当我在我的HTML表单上单击submit时,它没有到达/greeting端点 @org.springframework.stereotype.Controller @EnableAutoConfiguration public class Controller { @Autowired assessment.PdfGeneratorUtil pdfGenaratorUtil; @GetMapping ("/") String home() { retu
@org.springframework.stereotype.Controller
@EnableAutoConfiguration
public class Controller {
@Autowired
assessment.PdfGeneratorUtil pdfGenaratorUtil;
@GetMapping ("/")
String home() {
return "static/assessment.html";
}
@PostMapping("/greeting")
public String greetingSubmit() {
Map<String,String> data = new HashMap<String,String>();
data.put("name","James");
以及html的一个片段:
<form action="#" th:action="@{/greeting}" method="post">
提交后浏览器中的URL为:
http://localhost:8080/assessment.html#
项目结构:
更新
它正在处理这段代码,只要它在提交时碰到Get controller方法,以及Post controller方法。我不太清楚是什么改变了
注意:我现在没有使用@modeldattribute,因为我正在测试控制器方法是否在正确的时间被调用
@GetMapping("/greeting")
String greetingForm() {
return "assessment";
}
@PostMapping("/greeting")
public String greetingSubmit() {
System.out.println(" HELLLOO HELLOOOO ");
Map<String,String> data = new HashMap<String,String>();
data.put("name","James");
try {
pdfGenaratorUtil.createPdf("greeting",data);
} catch (Exception e) {
e.printStackTrace();
}
return "finished";
}
@GetMapping(“/greeting”)
字符串问候形式(){
返回“评估”;
}
@邮戳(“/问候语”)
公共字符串greetingSubmit(){
System.out.println(“HELLLOO-hellooo”);
映射数据=新的HashMap();
数据输入(“姓名”、“詹姆斯”);
试一试{
pdfGenaratorUtil.createPdf(“问候”,数据);
}捕获(例外e){
e、 printStackTrace();
}
返回“完成”;
}
我将用您在代码中给出的最少细节来回答您的问题
@PostMapping("/greeting")
public String greetingSubmit(@ModelAttribute Greeting greeting) {
return "greeting";
}
看
return "greeting";
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.loginProcessingUrl("/authentication")
.usernameParameter("j_username")
.passwordParameter("j_password").permitAll()
.and()
.csrf().disable()
}
因此,上面的代码将根据您的解析器配置查找/templates/greeting.html
return "greeting";
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.loginProcessingUrl("/authentication")
.usernameParameter("j_username")
.passwordParameter("j_password").permitAll()
.and()
.csrf().disable()
}
或在您的登录表单中添加csrf,如下所示:
input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
用代码中给出的最小细节,我将尝试回答您的问题
@PostMapping("/greeting")
public String greetingSubmit(@ModelAttribute Greeting greeting) {
return "greeting";
}
看
return "greeting";
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.loginProcessingUrl("/authentication")
.usernameParameter("j_username")
.passwordParameter("j_password").permitAll()
.and()
.csrf().disable()
}
因此,上面的代码将根据您的解析器配置查找/templates/greeting.html
return "greeting";
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.loginProcessingUrl("/authentication")
.usernameParameter("j_username")
.passwordParameter("j_password").permitAll()
.and()
.csrf().disable()
}
或在您的登录表单中添加csrf,如下所示:
input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
可以尝试将@PostMapping(“/greeting”)更改为@RequestMapping(“/greeting”,method=RequestMethod.POST)?好主意。但它不起作用,当我将PostMapping更改为RequestMapping时,我无法加载包含submit按钮的页面。@AlGrant您在Spring启动时看到URL映射日志了吗?@AlGrant是否使用post request发送数据?在Header中设置contentType可以尝试将@PostMapping(“/greeting”)更改为@RequestMapping(“/greeting”,method=RequestMethod.POST)?好主意。但它不起作用,当我将PostMapping更改为RequestMapping时,我无法加载包含submit按钮的页面。@AlGrant在Spring引导启动时看到URL映射日志了吗?@AlGrant是否使用POST请求发送数据?在页眉中设置contentType