springmvc request foward 和 redirect


---恢復內容開始---

  最近在實現那個學生信息錄入的時候,先是在添加學生的頁面添加完,然后想直接調用Conroller層遍歷學生的方法,我的意思就是在contoller一個方法怎么直接調用另外一個方法,

這個就需要用到  forward 和redirect 這個兩個方法,先看我的StudentController 里面的代碼

package zizai.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import zizai.model.Student;

import zizai.service.StudentService;

@Controller  
@RequestMapping("/student") 
public class StudentController {
    @Resource
    private StudentService studentService;
     @RequestMapping("/showAllStudent")  
     public String showUser(HttpServletRequest request,Model model){  
            List<Student> students = this.studentService.getAllStudent();  
            model.addAttribute("students", students);  
            return "tables";  
     } 
     @RequestMapping("/index")  
        public ModelAndView toIndex(HttpServletRequest request){  
         ModelAndView view = new ModelAndView("addStu");
        return view;
} 
     @RequestMapping("/addStu")  
     public ModelAndView addStu(HttpServletRequest request,Model model){        
         String  name = request.getParameter("name");
         String id =request.getParameter("id");    
         String age=request.getParameter("age");
         int id1=Integer.valueOf(id);
         int age1=Integer.valueOf(age);
         String classname=request.getParameter("classname");
         String sex=request.getParameter("sex");
         Student stu=new Student();
         stu.setAge(age1);
         stu.setClassname(classname);
         stu.setId(id1);
         stu.setName(name);
         stu.setSex(sex);
         int i=this.studentService.insertStu(stu);
         String url="addStu";
         if(i>0){
             url="redirect:/student/showAllStudent";
         }
         else{
             url="fail";
             
         }        
         ModelAndView view = new ModelAndView(url);
         return view;
     } 
     

}

forward 轉發,如return "forward:/student/showAllStudent"; 瀏覽器的地址欄不會變,但是有視圖返回來

redirect 重定向,如return "redirect:/student/showAllStudent"; 瀏覽器的地址欄會變。

1 先看看forward:/student/showAllStudent  可以看見用 forward 來進行跳轉的話,url 的地址不會改變

 

 

2 redirect 重定向,如return "redirect:/student/showAllStudent",這個時候url 里面的地址是會發生變化的

可以看到url 上的地址從ssm03/student/addStu   跳轉到 ssm03/student/showAllStudent

 

 

如果不用forward 轉發和redirect 重定向 的,直接通過url 賦值tables 來訪問  tables.jsp,通過tables.jsp里面的form 表單來訪問Controller層的showAllStudent的方法,這個時候就調不動里面的方法

 

如下是我的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">
<head>
<title>天元學生列表</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" 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/uniform.css" />
<link rel="stylesheet" href="/ssm03/resources/matrix-admin00/css/select2.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/css/font-awesome.css" rel="stylesheet" />
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,700,800' rel='stylesheet' type='text/css'>
</head>
<body>

<!--sidebar-menu-->


<div id="content">
  <div id="content-header">
    <div id="breadcrumb"> <a href="#" title="Go to Home" class="tip-bottom"><i class="icon-home"></i> Home</a> <a href="#" class="current">Tables</a> </div>
    <h1>天工信息</h1>
  </div>
  <div class="container-fluid">
    <hr>
    <div class="row-fluid">
      <div class="span12">
        <div class="widget-box">
          <div class="widget-title"> <span class="icon"> <i class="icon-th"></i> </span>
            <h5>信息列表</h5>
             <form action="/ssm03/student/index"  class="btn btn-success" >
          </div>
         
        <input type="submit" value="添加">
          <div class="widget-content nopadding">
            <table class="table table-bordered table-striped">
              <thead>
                <tr>
                  <th>學號</th>
                  <th>學生姓名</th>
                  <th>年齡</th>
                  <th>性別</th>
                  <th>班級</th>
                </tr>
              </thead>
              <tbody>
              <c:forEach var="student" items="${students}" >
                    <tr class="even gradeC">
                      <td align="center" >${student.id}</td>
                      <td align="center" >${student.name}</td>
                      <td align="center" >${student.age}</td>
                      <td align="center" >${student.sex}</td>
                      <td align="center" >${student.classname}</td>
                    </tr>
                </c:forEach>
              </tbody>
            </table>
          </div>
        </div>
        </div>
      </div>
    </div>
  </div>

<!--Footer-part-->

</body>
</html>

這是將 addStu 方法里面的url="tables" 時候,看看效果圖,

 

這個時候可以看見Controller層里面的showAllStudent 的方法,tables.jsp是可以調用到showAllStudent的方法的,因為已經跳轉到遍歷學生信息的表格去了,只是遍歷學生的方法不起作用而已,

這個問題也是可以解決,有兩種方法,一個實在Controller 里面改東西,在addStu這個方法直接使用showAllStudent方法,意思是添加完學生信息后,直接調用showAllStudent的方法,

另外一個方法就是在 jsp 里面做文章,多條一步的哈,顯示添加完學生后,給他一個中間顯示頁面,那個中間的顯示的jsp頁面再調用showAllStudent的方法,那么showAllStudent的方法就可以成功將學生的信息遍歷出來了

這里給寫出后面一種方法的截圖

添加學生成功后,url="List" 的jsp 中去,讓后在:List.jsp 里面來調用showAllStudent 的方法,這個時候便可以將學生列表的信息成功的遍歷出來

 

List.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">
<head>
<title>建築產品信息</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet"
    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'>
</head>
<body>


    <div id="content">
        <div id="content-header">
            <div id="breadcrumb">
            
                <a href="#" title="Go to Home" class="tip-bottom"><i
                    class="icon-home"></i> Home</a> 
                    <a href="#" class="current">天原系統</a>
            </div>
            
        
        <h2 ailgn="center">歡迎來到強大的天原系統</h2>
        <div class="row-fluid">
            <div class="span4">
                <div class="widget-box">
                    <div class="widget-title">
                        <span class="icon"> <i class="icon-list"></i>
                        </span>
                        <%--     <h5>${product.name }</h5> --%>
                    </div>
                    <div class="widget-content">
                        <table>
                            <tr>
                                <td>學生信息列表</td>

                                <td>
                                    <form class="form-horizontal" method="post"
                                        action="/ssm03/student/showAllStudent">
                                        <div class="form-actions">
                                            <input type="submit" value="查看" class="btn btn-success">
                                        </div>
                                    </form>
                                </td>
                            </tr>
                            <tr>
                                <td>建築信息列表</td>
                                <td>
                                    <form class="form-horizontal" method="post"
                                        action="/ssm03/product/getAllProduct">
                                        <div class="form-actions">
                                            <input type="submit" value="查看" class="btn btn-success">
                                        </div>
                                    </form>
                                </td>
                            </tr>
                            <tr>
                                <td>天工信息列表</td>
                                <td>
                                    <form class="form-horizontal" method="post"
                                        action="/ssm03/tiangong/showAllTianGong">
                                        <div class="form-actions">
                                            <input type="submit" value="查看" class="btn btn-success">
                                        </div>
                                    </form>
                                </td>
                            </tr>
                            <tr>
                                <td>招聘信息列表</td>
                                <td>
                                    <form class="form-horizontal" method="post"
                                        action="/ssm03/work/showAllWok">
                                        <div class="form-actions">
                                            <input type="submit" value="查看" class="btn btn-success">
                                        </div>
                                    </form>
                                </td>
                            </tr>

                        </table>
                    </div>
                </div>
            </div>
        </div>

</div>
</div>


        <!--Footer-part-->
        <div class="row-fluid">
            <div id="footer" class="span12">
                2013 &copy; Matrix Admin. Brought to you by <a
                    href="http://themedesigner.in/">Themedesigner.in</a>
            </div>
        </div>
        <!--end-Footer-part-->
        <script src="/ssm03/resources/matrix-admin00/js/jquery.min.js"></script>
        <script 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>
</html>

 

我們再看看整體的效果圖

1.這個是新添加學生的信息

2 添加完學生的信息后,就轉到List.jsp頁面

3 這個時候再點擊查看學生信息列表 右邊那個查看按鈕就可以查看到學生的全部信息了,在最后面,可以看到我們剛才添加的那個學生的信息

最后,說來說去,最后如果會用上面的 forward 和redirect

forward 轉發,如return "forward:/student/showAllStudent";

redirect 重定向,如return "redirect:/student/showAllStudent";

來直接調用 showAllStudent 方法的話,就不會有這么麻煩事了

所以forward 和redirect 這兩個方法還是挺好用的,今天的收獲就於此!

12:49:20

 

redirect 重定向

---恢復內容結束---

今天中午炸了,網一炸,電腦一卡,那些頁面上的內容好像都不見了,但是現在晚上回來一看,博客園幫我自動保存了,謝謝博客園,沒有讓我兩個小時的心血白費,本來有打算推到重來,現在好啦!

喜歡呼呼的文章的朋友,可以關注呼呼的個人公眾號:

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM