Tuesday, August 28, 2018

Spring MVC Redirect Example

In this post we’ll see a Spring MVC page redirect example with redirect to URL with parameters.

Spring MVC Project structure using Maven

Spring MVC page redirect example

In this example there is a form where user data is entered. On submitting the form the form data is saved and then using the user ID there is a redirect to a URL where userId is passed as parameter- “/showUser/" +userId where using that userId a User object is created.

This is the handler method in the controller class, where redirect is used to transfer to another page.

@RequestMapping(value = "/saveUser", method = RequestMethod.GET)
public String saveUser(@ModelAttribute("user") User user, Model model) {
 String userId = saveUser(user);
 // redirecting
 return "redirect:/showUser/" +userId;
}

This is the handler method which handles the request path which is created using the Spring MVC redirect.

@RequestMapping(value = "/showUser/{userId}", method = RequestMethod.GET)
public String showUser(@PathVariable("userId") String userId, Model model) {
 model.addAttribute("User", findUserById(userId));
 return "user"; 
}

Spring MVC page redirect example - Views

There are three JSPs used in this page rediredt example.

home.jsp

This is the JSP page with a link to registration form page.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Spring MVC tutorial - Home JSP</title>
</head>
<body>
    <div>${message}</div>
    <a href='<s:url value="/registerUser"></s:url>'>Register User</a>
</body>
</html>

userRegister.jsp

This is the registration form page which is opened after clicking the “Register User” link. Once this page is submitted the user data is saved and then the page is redirected to show the user details for the saved User. Note that in the handler method @PathVariable annotation is used to retrieve the parameter from the request path.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>User Registration</title>
</head>
<body>
    <form:form action="saveUser" modelAttribute="user" method="GET">
    <table>
         <tr>
             <td>
                 <form:label path="firstName">First Name</form:label>
             </td>
             <td>
                 <form:input path="firstName" id="firstname" />
             </td>
         </tr>
         <tr>
             <td>
                 <form:label path="lastName">Last Name</form:label>
             </td>
             <td>
                 <form:input path="lastName" id="lastname" />
             </td>
         </tr>
         <tr>
             <td><input type="submit" value="Submit"></td>
         </tr>
     </table>
    </form:form>
</body>
</html>

user.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Spring MVC tutorial - User</title>
</head>
<body>

<h4>User ID</h4><span>${User.userId}</span>
<h4>First Name</h4><span>${User.firstName}</span>
<h4>Last Name</h4><span>${User.lastName}</span>
</body>
</html>

Spring MVC page redirect example - Controller class

Here is the complete Controller class.

@Controller
public class MessageController {
 @RequestMapping(value = "/", method = RequestMethod.GET)
 public String showHome(Model model) {
  model.addAttribute(new User());
  model.addAttribute("message", "Spring MVC redirect example");
  return "home";
 }
 
 @RequestMapping(value = "/registerUser", method = RequestMethod.GET)
 public String showUserRegForm(@ModelAttribute("user") User user, Model model) {
  model.addAttribute("User", user);
  return "userRegister";
 }
 
 @RequestMapping(value = "/saveUser", method = RequestMethod.GET)
 public String saveUser(@ModelAttribute("user") User user, Model model) {
  String userId = saveUser(user);
  // redirecting
  return "redirect:/showUser/" +userId;
 }
 
 @RequestMapping(value = "/showUser/{userId}", method = RequestMethod.GET)
 public String showUser(@PathVariable("userId") String userId, Model model) {
  model.addAttribute("User", findUserById(userId));
  return "user"; 
 }
 
 // Dummy method to save user
 private String saveUser(User user) {
  System.out.println("Saving User");
  return "101";
 }
 // Dummy method to find user
 private User findUserById(String userId) {
  System.out.println("User ID " + userId);
  User user = new User();
  user.setUserId(userId);
  user.setFirstName("Leonard");
  user.setLastName("Nimoy");
  return user;
 }
}

That's all for this topic Spring MVC Redirect Example. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Spring Tutorial Page


Related Topics

  1. Spring MVC Example With @RequestParam Annotation
  2. Spring MVC Form Example With Bean Validation
  3. Spring Batch Processing Using JDBCTemplate batchUpdate() Method
  4. Select Query Using JDBCTemplate in Spring Framework
  5. @Resource Annotation in Spring Autowiring

You may also like-

  1. Excluding Bean From Autowiring in Spring
  2. ApplicationContextAware And BeanNameAware Interfaces in Spring Framework
  3. Transaction Management in Spring
  4. Internationalization (i18n) Using MessageSource in Spring
  5. Java Collections Interview Questions
  6. Switch-Case Statement in Java
  7. Primitive Type Streams in Java Stream API
  8. Converting String to Enum Type - Java Program