Form parameter is null with thymeleaf and Spring MVC

I have a problem with Thymeleaf and Spring MVC.

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

If I add another parameter to my model class (in my example I added a Date parameter and a long parameter) and did not put them in my view (just imagine this date parameter is my last modified date and this long parameter is a random value ), when I post this value it makes those 2 parameters null in my method.

Here are some of my codes.

My class model

public class Greeting {

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

      

My controller

@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";
}
}

      

My form

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

      

Results page

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

      

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

      

As you can see, both of my parameters, which do not appear in my view, become null after posting (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. Just to know, I am using Spring 4 as in the tutorial, and why am I having a parameter that is missing from my view? Since some of these parameters will contain some data exactly the same as the date of the last register update, the last user who updated the register and my app ID.

Can anyone help me find an answer for this?

Edit:

After some time and experimentation, I found a solution to my problem. Follow my piece of code that works the way it should 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;
    }   
}

      

+3


source to share


1 answer


This is expected behavior. You have model

in response

when you receive greetings view

, but until that is http

request/response

done, it no longer exists. You will be storing the values ​​in a tag <form>

and you will need to point those values ​​to form

submit

.

Meaning that if you want to send these values ​​back, you will need to add 2 hidden input

values ​​to store 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>

      

+2


source







All Articles