1. 程式人生 > >Maven和Spring mvc下的頁面的跳轉與取值

Maven和Spring mvc下的頁面的跳轉與取值

servle 提交 輸入 接收 -m title style ofo pri

(此處tomcat的端口設置為80)

例如:在testForm.jsp裏提交表單,在ok.jsp裏取值

testForm.jsp頁面代碼如下:

<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>頁面的跳轉與取值</title>
</head>
<body>
    <form action="../../api/ceshilei/ceshifangfa" method="post">
        門店編碼
<input name="num" /> 門店名稱<input name="name"/> <input id="btnSubmit" type="submit" value="提交"/> </form> </body> </html>

處理器類TestFormSubmit.java進行接收處理,代碼如下:

 1 package com.thinkgem.jeesite.modules.store.dao.ceshi1;
 2 
 3 import javax.servlet.http.HttpServletRequest;
4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 10 import com.thinkgem.jeesite.common.config.Global; 11 import com.thinkgem.jeesite.modules.store.entity.ceshi1.TestEntity;
12 import com.thinkgem.jeesite.modules.store.entity.daily.TStoresDaily; 13 14 @Controller //用於標識是處理器類 15 @RequestMapping(value="api/ceshilei") //請求到處理器類的綁定 16 public class TestFormSubmit { 17 18 @RequestMapping(value="ceshifangfa") 19 public String ceshi(TestEntity te, HttpServletRequest request, HttpServletResponse response, Model model){ 20 System.out.println("test ing "); 21 model.addAttribute("testentity",te); 22 return "modules/store/ceshi1/ok"; 23 } 24 25 // @RequestMapping(value="ceshifangfa") //請求到處理器功能方法的綁定 26 // public String ceshi(String num, String name, HttpServletRequest request, HttpServletResponse response, Model model){ 27 // System.out.println("test ing "); 28 //// model.addAttribute("testentity",te); 29 // model.addAttribute("num",num); 30 // model.addAttribute("name",name); 31 // System.out.println("num=="+num); 32 // System.out.println("name=="+name); 33 //// return "modules/store/ceshi1/ok"; 34 // return "redirect:ok"; //重定向,值在地址欄有顯示,但是頁面沒有 35 // } 36 37 @RequestMapping("toForm") 38 public String toForm() { 39 return "modules/store/ceshi1/testForm"; 40 } 41 42 @RequestMapping("ok") 43 public String ok() { 44 return "modules/store/ceshi1/ok"; 45 } 46 47 }

最終接收值的頁面ok.jsp,代碼如下:

<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>頁面的取值</title>
</head>
<body>

    提交的門店編碼是${testentity.num }<br>
    提交的門店名稱是${testentity.name }<br>
        
</body>
</html>

在地址欄輸入http://localhost/api/ceshilei/toForm,請求到處理器類value為api/ceshilei的類,並調用請求方法value值為toForm的方法。該toForm方法返回一個要訪問的目標文件路徑(去掉前綴和後綴的路徑,前綴和後綴在spring-mvc.xml中有說明),此處就是testForm.jsp。

在testForm.jsp頁面填寫表單並提交,action為"../../api/ceshilei/ceshifangfa"。即調用處理器類value為api/ceshilei的類,並調用請求方法value值為ceshifangfa的方法(此處為ceshi)。該方法接收提交的表單傳來的值並把值addAttribute給model,並返回一個要訪問的目標文件路徑。頁面跳轉到ok.jsp。

ok.jsp用el表達式取值。

Maven和Spring mvc下的頁面的跳轉與取值