Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent Spring 3.0 MVC @ModelAttribute variables from appearing in URL?

Using Spring MVC 3.0.0.RELEASE, I have the following Controller:

@Controller @RequestMapping("/addIntake.htm") public class AddIntakeController{    private final Collection<String> users;    public AddIntakeController(){     users = new ArrayList<String>();     users.add("user1");     users.add("user2");     // ...     users.add("userN");   }    @ModelAttribute("users")   public Collection<String> getUsers(){     return this.users;   }    @RequestMapping(method=RequestMethod.GET)   public String setupForm(ModelMap model){      // Set up command object     Intake intake = new Intake();     intake.setIntakeDate(new Date());     model.addAttribute("intake", intake);      return "addIntake";   }    @RequestMapping(method=RequestMethod.POST)   public String addIntake(@ModelAttribute("intake")Intake intake, BindingResult result){      // Validate Intake command object and persist to database     // ...      String caseNumber = assignIntakeACaseNumber();      return "redirect:intakeDetails.htm?caseNumber=" + caseNumber;    }  } 

The Controller reads Intake information from a command object populated from an HTML form, validates the command object, persists the information to the database, and returns a case number.

Everything works great, except for when I redirect to the intakeDetails.htm page, I get a URL that looks like this:

http://localhost:8080/project/intakeDetails.htm?caseNumber=1&users=user1&users=user2&users=user3&users=user4...

How do I prevent the user Collection from showing up in the URL?

like image 920
Christian Avatar asked Jan 29 '10 16:01

Christian


People also ask

What is @ModelAttribute in Spring MVC?

@ModelAttribute is an annotation that binds a method parameter or method return value to a named model attribute, and then exposes it to a web view. In this tutorial, we'll demonstrate the usability and functionality of this annotation through a common concept, a form submitted from a company's employee.

Why do we use @ModelAttribute?

The @ModelAttribute annotation is used as part of a Spring MVC web app and can be used in two scenarios. Firstly, it can be used to inject data objects in the model before a JSP loads. This makes it particularly useful by ensuring that a JSP has all the data it needs to display itself.


2 Answers

model.asMap().clear(); return "redirect:" + news.getUrl(); 

:)

like image 170
sideways Avatar answered Nov 04 '22 06:11

sideways


Since spring 3.1 the RequestMappingHandlerAdapter provides a flag called ignoreDefaultModelOnRedirectthat you can use to prevent using the content of the defautl model if the controller redirects.

like image 40
elaich Avatar answered Nov 04 '22 06:11

elaich