Java 使用Thymeleaf创建的表单返回空对象
我已经学习了所有的教程,但仍然存在一个问题,即我无法使用SpringMVC将对象从窗体获取到控制器。可能是什么情况?我正在使用Thymeleaf格式化我的jsp页面 这里是风景Java 使用Thymeleaf创建的表单返回空对象,java,spring,forms,model-view-controller,thymeleaf,Java,Spring,Forms,Model View Controller,Thymeleaf,我已经学习了所有的教程,但仍然存在一个问题,即我无法使用SpringMVC将对象从窗体获取到控制器。可能是什么情况?我正在使用Thymeleaf格式化我的jsp页面 这里是风景 <!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title></title> <meta http-equiv="Content-Type" content="t
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Form</h1>
<form action="" th:action="@{/increaseprice}" th:object="${priceIncrease}" method="post">
<input type="text" th:field="*{message}" />
<p><input type="submit" value="Submit" />
</form>
</body>
</html>
Java类
package store.service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class PriceIncrease {
protected final Log logger = LogFactory.getLog(getClass());
private int percentage;
private String message;
public PriceIncrease(){
}
public PriceIncrease(String message){
this.message = message;
}
public void setPercentage(int i){
percentage = i;
logger.info("Percentage set to " + i);
}
public int getPercentage(){
return percentage;
}
public String getMessage(){
return this.message;
}
public void setMessage( String message ){
this.message = message;
}
}
我相信您不需要@ModelAttribute,因为SpringMVC足够智能,可以将表单映射到您的PriceIncrease控制器参数
package store.service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class PriceIncrease {
protected final Log logger = LogFactory.getLog(getClass());
private int percentage;
private String message;
public PriceIncrease(){
}
public PriceIncrease(String message){
this.message = message;
}
public void setPercentage(int i){
percentage = i;
logger.info("Percentage set to " + i);
}
public int getPercentage(){
return percentage;
}
public String getMessage(){
return this.message;
}
public void setMessage( String message ){
this.message = message;
}
}