Thymeleaf和Spring MVC的Form参数为null [英] Form parameter is null with Thymeleaf and Spring MVC

查看:117
本文介绍了Thymeleaf和Spring MVC的Form参数为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Thymeleaf和Spring MVC上遇到问题.

i'm having a problem with Thymeleaf and Spring MVC.

我正在关注spring.io网站 http://spring上的教程. io/guides/gs/handling-form-submission/,当我尝试扩展本教程时,我遇到了一个问题.

I'm following a tutorial from spring.io website http://spring.io/guides/gs/handling-form-submission/ and when I've tried to expand this tutorial, I ran into a problem.

如果我在模型类中添加了另一个参数(在我的示例中,我添加了Date参数和long参数),并且没有将它们放在我的视图中(假设此date参数是我的最后修改日期,这个长参数是一个随机值),当我提交该值时,这会使我的方法中的这两个参数为空.

If I add another parameter to my model class (on my example, I've added a Date parameter and a long parameter), and not put them on my view (just imagine that this date parameter is my last modification date and this long parameter is a random value), when I submit this value, it makes this 2 parameters null on my method.

这是我的一些代码.

我的模特班

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>

这是我的模型类的控制台打印内容

This are my console prints of my model class

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.

As you can see, both of my parameters that are not in my view, become null after the post (the second line) and on the first line, they have values. I don't understand why, because I came from another good framework (JSF) and I don't have such a problem there. Just to know, I'm using spring 4, just like on the tutorial, and why I have a parameter that is not on my view? Because some of this parameters will hold some data just like last register updated date, last user who updated the register and my application ID.

有人可以帮我找到答案吗?

Can anyone help me find an answer for this?

经过一段时间的试验,我找到了解决问题的方法.遵循我的代码,它应该可以正常工作:

After some time and experimentation, I found a solution to my problem. Follow my piece of code that works just as it meant to be:

@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时,在response中有model,但是只要完成http request/response,它就不存在了.您将把这些值保存在<form>标记中,并且需要在form submit上提供这些值.

This is the expected behavior. You have the model in the response while getting to your greetings view, but as long as that http request/response is done, it doesn't exist anymore. You will hold the values in the <form> tag and you will need to provide these values upon form submit.

意思是,如果要发回这些值,则需要添加2个隐藏的input来容纳这两个值.

Meaning, that if you want to send these values back you will need to add 2 hidden inputs to hold these two values.

<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>

这篇关于Thymeleaf和Spring MVC的Form参数为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆