Spring MVC – Code ví dụ Spring MVC Page Redirect, chuyển tiếp trang

Spring MVC – Code ví dụ Spring MVC Page Redirect, chuyển tiếp trang

Page Redirection

Trong các ví dụ trước chúng ta thường dùng link, form để gọi 1 controller sau đó controller trả về view, đây là 1 flow đơn giản trong Spring MVC.

Nhiều trường hợp controller nhận request nhưng nó không trả về view mà gửi request tới 1 controller khác để thực hiện request – page redirection

Spring MVC - Code ví dụ Spring MVC Page Redirect, chuyển tiếp trang

Ví dụ:

Thư viện sử dụng:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>stackjava.com</groupId>
  <artifactId>SpringRedirect</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <properties>
    <spring.version>5.0.2.RELEASE</spring.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
  </dependencies>
</project>

Spring Config:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

  <context:component-scan base-package="stackjava.com.springmvchello" />

  <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
      <value>/WEB-INF/views/jsp/</value>
    </property>
    <property name="suffix">
      <value>.jsp</value>
    </property>
  </bean>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  version="2.5">
  <display-name>SpringRedirect</display-name>

  <servlet>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>spring-mvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

Controller:

method redirect()không trả về view mà thực hiện redirect sang url “/page2” bằng cách thêm từ “redirect” trước kết quả trả về.

Bạn cũng có thể thực hiện redirect bằng cách return new RedirectView("page2"); thay vì "redirect:page2";

package stackjava.com.springmvchello.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class BaseController {

  @RequestMapping("/")
  public String page1() {
    return "page1";
  }

  @RequestMapping("/redirect")
  public String redirect() {
    return "redirect:page2";
  }

// @RequestMapping("/redirect")
// public RedirectView redirect() {
// return new RedirectView("page2");
// }

  @RequestMapping("/page2")
  public String page2() {
    return "page2";
  }

}

File view:

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring MVC Redirect</title>
</head>

<body>
  <h2>Page1</h2>
  <form:form method="POST" action="redirect">
    <input type="submit" value="Redirect" />
  </form:form>
</body>
</html>
<html>
<head>
<title>Spring MVC Redirect</title>
</head>
<body>
  <h2>Page2</h2>
</body>
</html>

Demo:

button Redirection ở page1 gọi tới controller redirect, controller redirect lại chuyển request tới controller page2, controller page2 trả về trang page2.jsp

Page Redirect, chuyển tiếp trang trong Spring MVC

Spring MVC – Code ví dụ Spring MVC Page Redirect, chuyển tiếp trang

Okay, Done!

Download code ví dụ trên tại đây

 

References:

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/view/RedirectView.html

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/support/RedirectAttributes.html

stackjava.com