SPRINGMVC Request Foward and redirect

Source: Internet
Author: User

---restore content starts---

Recently in the realization of that student information entry, first in the Add Student page added, and then want to call Conroller layer to traverse the students ' method, I mean in contoller a method how to call another method directly,

This will need to use forward and redirect this two method, first look at my Studentcontroller inside the code

 PackageZizai.controller;Importjava.util.List;ImportJavax.annotation.Resource;Importjavax.servlet.http.HttpServletRequest;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.servlet.ModelAndView;Importzizai.model.Student;ImportZizai.service.StudentService, @Controller @RequestMapping ("/student")  Public classStudentcontroller {@ResourcePrivateStudentservice Studentservice; @RequestMapping ("/showallstudent")        PublicString showuser (httpservletrequest request,model Model) {List<Student> students = This. Studentservice.getallstudent (); Model.addattribute ("Students", students); return"Tables"; } @RequestMapping ("/index")           PublicModelandview Toindex (httpservletrequest request) {Modelandview View=NewModelandview ("Addstu"); returnview;} @RequestMapping ("/addstu")        PublicModelandview addstu (httpservletrequest request,model Model) {String name= Request.getparameter ("name"); String ID=request.getparameter ("id"); String Age=request.getparameter ("Age"); intid1=integer.valueof (ID); intage1=integer.valueof (age); String ClassName=request.getparameter ("ClassName"); String Sex=request.getparameter ("Sex"); Student Stu=NewStudent ();         Stu.setage (AGE1);         Stu.setclassname (classname);         Stu.setid (ID1);         Stu.setname (name);         Stu.setsex (Sex); intI= This. Studentservice.insertstu (Stu); String URL= "Addstu"; if(i>0) {URL= "Redirect:/student/showallstudent"; }         Else{URL= "Fail"; } Modelandview View=Newmodelandview (URL); returnview; }      }

Forward forwarding, such as return "forward:/student/showallstudent"; The address bar of the browser does not change, but a view is returned to

Redirect redirect, such as return "redirect:/student/showallstudent"; The browser's address bar will change.

1 First look at forward:/student/showallstudent can see with forward to jump, the URL will not change the address

2 redirect redirect, such as return "redirect:/student/showallstudent", this time the address inside the URL is changed

You can see that the address on the URL jumps from Ssm03/student/addstu to Ssm03/student/showallstudent

If you do not use forward forwarding and redirect redirection, directly through the URL assignment tables to access the tables.jsp, through the tables.jsp inside the form table forms access to the controller layer Showallstudent method, I can't adjust the method at this time.

Here is the code for my tables.jsp

<%@ page language= "Java"Import= "java.util.*" pageencoding= "Utf-8"%><%@ taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%> < HTML lang= "en" >class= "Tip-bottom" ><iclass= "Icon-home" ></i> home</a> <a href= "#"class= "Current" >Tables</a> </div> class= "Container-fluid" > class= "Row-fluid" > <divclass= "Span12" > <divclass= "Widget-box" > <divclass= "Widget-title" > <spanclass= "icon" > <iclass= "icon-th" ></i> </span> class= "Btn btn-success" > </div> <input type= "Submit" value= "Add" > <divclass= "Widget-content nopadding" > <tableclass= "Table table-bordered table-striped" > <thead> <tr> <th> School Number </th> <th> student name </th> <th> age </th> <th&              gt; sex </th> <th> classes </th> </tr> </thead> <tbody> <c:foreach var= "student" items= "${students}" > <trclass= "Even Gradec" > <td align= "center" >${student.id}</td> <td Ali                      gn= "center" >${student.name}</td> <td align= "center" >${student.age}</td> &LT;TD align= "center" >${student.sex}</td> <td align= "center" >${studen            t.classname}</td> </tr> </c:forEach> </tbody> </table> </div> </div> </div> </div> </div> < /div><!--footer-part--></body>

This is the Addstu method inside the Url= "tables" time, look,

This time can see the controller layer inside the Showallstudent method, tables.jsp can be called to Showallstudent method, because has jumped to the table to traverse the student information, just traverse the student's method does not work only,

This problem is also can be solved, there are two ways, a real controller inside change things, in Addstu this method directly using the Showallstudent method, meaning is to add the student information, directly call Showallstudent method,

Another way is to make a fuss in the JSP, a number of one-step ha, show the addition of students, give him a middle display page, the middle of the display of the JSP page and then call Showallstudent method, Then the Showallstudent method can successfully traverse the student's information.

Here's a way to write the following

Add student Success, url= "list" of the JSP go, let in: list.jsp inside to call showallstudent method, this time can be the student list of information successfully traversed out

The code for LIST.JSP is as follows

<%@ 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" >href= "/ssm03/resources/matrix-admin00/css/bootstrap.min.css"/><link rel= "stylesheet"href= "/ssm03/resources/matrix-admin00/css/bootstrap-responsive.min.css"/><link rel= "stylesheet"href= "/ssm03/resources/matrix-admin00/css/matrix-style.css"/><link rel= "stylesheet"href= "/ssm03/resources/matrix-admin00/css/matrix-media.css"/><link href= "Font-awesome//ssm03/resources/matrix-admin00/css/font-awesome.css"rel= "stylesheet"/><link href= ' http://fonts.googleapis.com/css?family=Open+Sans:400,700,800 'rel= ' stylesheet ' type= ' text/css ' >class= "Tip-bottom" ><Iclass= "Icon-home" ></i> home</a> <a href= "#"class= "Current" > The original system </a> </div> 

class= "Row-fluid" > <divclass= "Span4" > <divclass= "Widget-box" > <divclass= "Widget-title" > <spanclass= "icon" > <iclass= "Icon-list" ></i> </span> <%--class= "Widget-content" > <table> <tr> <td> Student Information List </td> <td> <formclass= "Form-horizontal" method= "POST"Action= "/ssm03/student/showallstudent" > <divclass= "Form-actions" > <input type= "Submit" value= "View"class= "Btn btn-success" > </div> </form&gt ; </td> </tr> <tr> &L T;td> Building Information List </td> <td> <formclass= "Form-horizontal" method= "POST"Action= "/ssm03/product/getallproduct" > <divclass= "Form-actions" > <input type= "Submit" value= "View"class= "Btn btn-success" > </div> </form&gt ; </td> </tr> <tr> &L t;td> Tiangong Information list </td> <td> <formclass= "Form-horizontal" method= "POST"Action= "/ssm03/tiangong/showalltiangong" > <divclass= "Form-actions" > <input type= "Submit" value= "View"class= "Btn btn-success" > </div> </form&gt ; </td> </tr> <tr> &L T;td> Job Listings </td> <td> <formclass= "Form-horizontal" method= "POST"Action= "/ssm03/work/showallwok" > <divclass= "Form-actions" > <input type= "Submit" value= "View"class= "Btn btn-success" > </div> </form&gt ; </td> </tr> </table> </div> </div> </div> </div></div></div> <!--footer-part-- > <divclass= "Row-fluid" > <div id= "Footer"class= "Span12" > &copy; Matrix Admin. Brought to <a href= "http://themedesigner.in/" >Themedesigner.in</a> </div> </div> <!--end-fo oter-part--> <script src= "/ssm03/resources/matrix-admin00/js/jquery.min.js" ></script> &LT;SCR IPT src= "/ssm03/resources/matrix-admin00/js/jquery.ui.custom.js" ></script> <script src= "/ssm03/ Resources/matrix-admin00/js/bootstrap.min.js "></script> <script src="/ssm03/resources/matrix-admin00 /js/matrix.js "></script></body>

Let's look at the whole

1. This is the information of the newly added students

2 After adding the student's information, go to the list.jsp page

3 Click the View button on the right of the Student information list at this time to see all the students ' information, and at the end, we can see the information of the student we just added.

Finally, after all, if you use the forward and redirect above

Forward forwarding, such as return "forward:/student/showallstudent";

Redirect redirect, such as return "redirect:/student/showallstudent";

To call the Showallstudent method directly, there's no such thing as a problem.

So forward and redirect these two methods are very useful, today's harvest on this!

12:49:20

REDIRECT Redirection

---restore content ends---

Today, the noon bombing, the network a bomb, a computer card, those pages on the content seems to have disappeared, but now at night back to see, blog Park to help me automatically saved, thank you blog Park, did not let me two hours of painstaking efforts, there are plans to push to re-start, now good!

SPRINGMVC Request Foward and redirect

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.