Rookie Learning SPRING--SPRINGMVC Annotation version control layer redirection to the control layer

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.