簡體   English   中英

使用Thymeleaf和Spring MVC,Form參數為null

[英]Form parameter is null with Thymeleaf and Spring MVC

我遇到了Thymeleaf和Spring MVC的問題。

我正在關注spring.io網站http://spring.io/guides/gs/handling-form-submission/的教程,當我試圖擴展本教程時,我遇到了一個問題。

如果我在模型類中添加另一個參數(在我的例子中,我添加了一個Date參數和一個long參數),而不是將它們放在我的視圖上(想象一下這個日期參數是我的最后修改日期和這個長參數是一個隨機值),當我提交這個值時,它會使我的方法中的這2個參數為null。

這是我的一些代碼。

我的模特班

public class Greeting {

    private long id;
    private String content;
    private Date hour;
    private long longNumber;
.... getters and setters ....
}

我的控制器

@Controller
public class GreetingController {

@RequestMapping(value="/greeting", method=RequestMethod.GET)
public String greetingForm(Model model) {

    Greeting greeting = new Greeting();
    greeting.setHour(new Date());
    greeting.setLongNumber(1234L);
    System.out.println(greeting.toString());
    model.addAttribute("greeting", greeting);
    return "greeting";
}

@RequestMapping(value="/greeting", method=RequestMethod.POST)
public String greetingSubmit(@ModelAttribute Greeting greeting, Model model) {
    System.out.println(greeting.toString());
    model.addAttribute("greeting", greeting);
    return "result";
}
}

我的表格

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Handing Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Form</h1>
    <form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post">
        <p>Id: <input type="text" th:field="*{id}" /></p>
        <p>Message: <input type="text" th:field="*{content}" /></p>
        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>
</body>
</html>

結果頁面

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Handing Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Result</h1>
    <p th:text="'id: ' + ${greeting.id}" />
    <p th:text="'content: ' + ${greeting.content}" />
    <p th:text="'Date: ' + ${greeting.hour}" />
    <a href="/greeting">Submit another message</a>
</body>
</html>

這是我的模型類的控制台打印

Greeting [id=0, content=null, hour=Sun Apr 26 22:29:25 GMT-03:00 2015, longNumber=1234]
Greeting [id=0, content=aaaa, hour=null, longNumber=0]

如您所見,我的視圖中的兩個參數在帖子(第二行)之后變為空,在第一行之后,它們具有值。 我不明白為什么,因為我來自另一個好的框架(JSF),我沒有那樣的問題。 只是要知道,我正在使用spring 4,就像在教程中一樣,為什么我有一個不在我視圖中的參數? 因為這些參數中的一些將保存一些數據,就像上次注冊更新日期一樣,最后一位用戶更新了寄存器和我的應用程序ID。

任何人都可以幫我找到答案嗎?

編輯:

經過一段時間和實驗,我找到了解決問題的方法。 按照我的代碼片段,它的工作原理如下:

@Controller
@SessionAttributes({"obj"})
public class MyController {

    @RequestMapping(value = "/path", method = RequestMethod.GET)
    public ModelAndView myGet() {       
        ModelAndView modelAndView = new ModelAndView("view");
        MyClass object = new MyClass();
        modelAndView.addObject("obj", object );     
        return modelAndView;
    }

    @RequestMapping(value = "/path", method = RequestMethod.POST)
    public ModelAndView myPost(@ModelAttribute("obj") @Validated MyClass object, BindingResult result){

        ModelAndView modelAndView = new ModelAndView("view");       
        if(result.hasErrors())
            modelAndView.addObject("obj",object);
        else 
            service.save(object);
        return modelAndView;
    }   
}

這是預期的行為。 在獲取問候view ,您在responsemodel ,但只要完成http request/response ,它就不再存在。 您將保存<form>標記中的值,並且您需要在form submit時提供這些值。

這意味着,如果要將這些值發回,則需要添加2個隱藏input來保存這兩個值。

<form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post">
    <p>Id: <input type="text" th:field="*{id}" /></p>
    <p>Message: <input type="text" th:field="*{content}" /></p>
    <input type="hidden" th:field="*{hour}" />
    <input type="hidden" th:field="*{longNumber}" />
    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM