Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Thymeleaf创建的表单返回空对象_Java_Spring_Forms_Model View Controller_Thymeleaf - Fatal编程技术网

Java 使用Thymeleaf创建的表单返回空对象

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

我已经学习了所有的教程,但仍然存在一个问题,即我无法使用SpringMVC将对象从窗体获取到控制器。可能是什么情况?我正在使用Thymeleaf格式化我的jsp页面

这里是风景

<!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;
    }
}