To pass parameters to a page in a Spring controller, simply configure the view parser, add parameters to the Model, and fetch them from the page using an EL expression. However, this is done in forward mode and the browser’s address bar remains the same. If the browser F5 refreshes, the form will be submitted repeatedly. Therefore, we can use redirection to change the browser’s address bar to prevent the form from being submitted repeatedly because of the refresh.

The JSP file:

<% @ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"% >
<% @ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"% >

      
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>login</title>
</head>
<body>
	
	<form id="form1" action="/demo/user/login" method="post">Account:<input type="text" name="name" /></br>Password:<input type="password" name="password" /></br>
		<input type="submit" value="submit"/>
		
	</form>

</body>
</html>
Copy the code

Controller:

package com.demo.controller;

import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/ * * *@author lpj
 * @dateJuly 10, 2016 */
@Controller
@RequestMapping("/user")
public class DemoController {

	@RequestMapping("/login")
	public String login(@RequestParam Map<String, String> user, Model model) {
		System.out.println("The user submitted the form once.");
		String username;
		if (user.get("name").isEmpty()) {
			username = "Tom";
		} else {
			username = user.get("name");
		}
		model.addAttribute("msg", username);
// return "home"; // This way jump, page refresh will repeat the submission of the form
		return "redirect:/home.jsp"; }}Copy the code

Since a redirect is equivalent to two requests, you cannot pass the parameters in the model. In the above example, the page does not get the MSG parameter. To get the parameters, you can manually compose the URL with the parameters at the end. Spring 3.1 provides a handy class: RedirectAttributes. Using this class, we can redirect parameters to the page without having to compose the URL ourselves. Replace Model with DirectAttributes in the method parameter above, and the parameter automatically follows the URL.


However, the page cannot be retrieved with EL, so there is another way to retrieve parameters with EL, rather than spelling the URL, just like normal forwarding. Use directAttributes again, but instead of using the addAttribute method this time, Spring has a new method for us, addFlashAttribute (). This method works by putting it into a session, which removes the object as soon as it jumps to the page. So when you refresh it, it gets lost. Change the controller code to the following:

package com.demo.controller;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/ * * *@author lpj
 * @dateJuly 10, 2016 */
@Controller
@RequestMapping("/user")
public class DemoController {

	@RequestMapping("/login")
//	public String login(@RequestParam Map<String, String> user, Model model) {
	public String login(@RequestParam Map<String, String> user, RedirectAttributes model) {
		System.out.println("The user submitted the form once.");
		String username;
		if (user.get("name").isEmpty()) {
			username = "Tom";
		} else {
			username = user.get("name");
		}
		model.addFlashAttribute("msg", username);
// return "home"; // This way jump, page refresh will repeat the submission of the form
		return "redirect:/user/toHome";
	}
	
	@RequestMapping("/toHome")
	public String home(@ModelAttribute("msg") String msg, Model model) {
		System.out.println("Get the redirection parameter MSG :" + msg);
		model.addAttribute("msg", msg);
		return "home"; }}Copy the code

Here we use @modelAttribute to get the data that addFlashAttribute added earlier, and then we can use it.

For example code, click here to download: Demo