public classUser {PrivateString name; PrivateInteger age; publicString getName () {returnname; } public voidsetName (String Name) { this. Name =name; } publicInteger getage () {returnage ; } public voidsetage (Integer Age) { this. Age =age ; } publicUser (String name, Integer age) {Super(); this. Name =name; this. Age =age ; } publicUser () {Super(); } @Override publicString toString () {return"User [name=" + name + ", age=" + age + "]"; }}
User entity class
<%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"%><%StringPath=Request.getcontextpath ();StringBasePath=Request.getscheme ()+"://"+request.getservername ()+":"+Request.getserverport ()+Path+"/";%><!DOCTYPE HTML public "-//w3c//dtd HTML 4.01 transitional//en"><HTML> <Head> <Basehref= "<%=basePath%>"> <title>My JSP ' index.jsp ' starting page</title> <Metahttp-equiv= "pragma"content= "no-cache"> <Metahttp-equiv= "cache-control"content= "no-cache"> <Metahttp-equiv= "expires"content= "0"> <Metahttp-equiv= "keywords"content= "keyword1,keyword2,keyword3"> <Metahttp-equiv= "description"content= "this is my page"> <!--<link rel= "stylesheet" type= "text/css " href= "styles.css" > - </Head> <Body> <formAction= "user/add"Method= "post"> <!--must be the corresponding property name in the user class -User Name:<inputtype= "text"name= "name">age:<inputtype= "text"name= "age"> <Buttontype= "submit">Submit</Button> </form> </Body></HTML>
index.jsp Page
@Controller @requestmapping ("/user") public classMycontroller {/*** Forward default format @RequestMapping (value = "/add") public modelandview add (user user) {System.out.prin TLN ("entered Add ..."); Modelandview mv = new Modelandview (); Mv.addobject ("name", user.getname ()). addobject ("age", user.getage ()). setviewname ("forward:/web-inf/jsp/suc Cess.jsp ");//set The returned view//. setviewname ("/web-inf/jsp/success.jsp ");//the default is to forward the return mv; }*/ /*** redirect: * 01. Client behavior! Cannot access the resource under/web-inf/* 02. The access path is the same, so there will be a user/* 03 in the previous 09 Project. can also carry the last data * 04.redirect:/success.jsp plus/is based on the Project's root directory*/@RequestMapping (value= "/add") publicmodelandview Add (user user) {System.out.println ("into the Add ..."); Modelandview MV=NewModelandview (); Mv.addobject ("name", user.getname ()). addobject ("age", user.getage ())//Carry Data. Setviewname ("redirect:/success.jsp");//set the returned view redirection returnmv; }}
Controller Code
< body > <!-- Param.name code that executes at the bottom is Request.getparameter ("name") requestscope.nage The underlying code is reques T.getattribute ("name") scope Query --> << /span>h1 > webroot Success page </ h1 > Span style= "color: #000000" > ${param.name} < br /> ${param.age} </ body >
webroot Create success.jsp page below
================== Redirect to Method ====================
@Controller @requestmapping ("/user") public classMycontroller {/*** Jump To/list * Model: The data to be carried when jumping the list method*/@RequestMapping (value= "/add") publicString Add (user user, Model mv) {System.out.println ("into the Add ..."); //add data to the modelMv.addattribute ("name", user.getname ()). addattribute ("age", User.getage ()); //jump to List can not add/this is the background path return"redirect:list"; } @RequestMapping (value= "/list") publicString list (user user) {System.out.println ("into the list ..."); System.out.println (user.getname ()); System.out.println (user.getage ()); return"/success.jsp";//forward to success.jsp under Webroot }}
Change the controller code in the example above
SpringMVC08 Forwarding and redirection