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?
@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.
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.
model.asMap().clear(); return "redirect:" + news.getUrl();
:)
Since spring 3.1 the RequestMappingHandlerAdapter
provides a flag called ignoreDefaultModelOnRedirect
that you can use to prevent using the content of the defautl model if the controller redirects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With