Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring lost model attribute between GET and POST

Tags:

java

spring

jsp

The get method will prepare the model and send to the "add person" jsp
It will also set the "mode" attribute to be "add" (so add and edit can share same jsp)
When the processSubmit result hasErrors ,mode attribute is gone
How to maintain mode attribute between calls?

@RequestMapping(value="/people/def/add" , method = RequestMethod.GET)
public String personAdd(@ModelAttribute("person") Person person,Model map) {        
    map.addAttribute("mode", "add");
    //DO SOME LOGIC
    return "personAdd";
}

@RequestMapping(value="/people/def/add" , method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("person") Person person,BindingResult result) { 
    personValidator.validate(person, result);
    if (result.hasErrors()) {
        //MODE ATTRIBUTE IS LOST
        return "personAdd";
like image 422
JavaSheriff Avatar asked Dec 09 '25 07:12

JavaSheriff


1 Answers

Request attributes live only for the life of request. So, if you want "mode" back in Post, you may have to submit it back as part of POST, may be by using hidden form control in your web form.

You have to add "@RequestParam("mode") String mode" to your "processSubmit" method to retrieve the value of mode from HTTP POST parameters

like image 61
Wand Maker Avatar answered Dec 10 '25 21:12

Wand Maker