I. Overview.
SPRINGMVC The interface request contorller1,contorller1 need to redirect to Contorller2 to show other pages or do some business logic, Spring provides this feature to use "redirect:/" for redirection.
second, the code demonstration.
1. Interface
login.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
login2.jsp
P>
<%@ page language=" java "contenttype=" text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
listusername.jsp
<%@ page language=" java "contenttype=" text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <% @taglib prefix=" C "uri=" Http://java.sun.com/jsp/jstl/core "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
2. Backstage Code
Listusername.java
Package Com.gaowei.controller;import Java.util.arraylist;import Java.util.list;import Org.springframework.stereotype.controller;import Org.springframework.ui.model;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import Org.springframework.web.bind.annotation.RequestParam; @Controllerpublic class Listusername {//no parameter @requestmapping (" /listusername ") Public String Listusername (model model) {list listuser=new ArrayList (), for (int i = 0; i <; i++) {list User.add ("username" + (i+1));} Model.addattribute ("Listusername", Listuser); return "listusername.jsp";} There is a parameter @requestmapping ("/listusername2") public string listUsername2 (@RequestParam ("username") string Username,model Model) {List listuser=new ArrayList (); for (int i = 0; i <; i++) {Listuser.add (username+ (i+1));} Model.addattribute ("Listusername", Listuser); return "listusername.jsp";}}
Login.java
Package Com.gaowei.controller;import Org.springframework.stereotype.controller;import Org.springframework.ui.model;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import Org.springframework.web.bind.annotation.RequestParam; @Controllerpublic class Login {@RequestMapping ("/login") public string Login (@RequestParam ("username") string username) {System.out.print ("username=" +username); return " Redirect:/listusername.spring ";} @RequestMapping ("/login2") public string login2 (@RequestParam ("username") string username) {System.out.print (" Username= "+username); return" Redirect:/listusername2.spring?username= "+username;}}
3.
Redirect with no parameters
Redirected with Parameters
Third, summary.
This feature is often used in our project to have a redirect with no parameters and another parameter redirection, which makes it easier for us to use.
Rookie Learning SPRING--SPRINGMVC Annotation version control layer redirection to the control layer