springmvc 中controller與jsp傳值


在springmvc中的controller所對應的函數中,如果需要從*.jsp頁面中獲取數據,可以自行在函數括號中寫,springmvc會自動封裝傳過來的。

spring-mvc.xml

中加入

1   <!-- 自動掃描 -->
2 <context:component-scan base-package="cn.itcast.springmvc.service,cn.itcast.springmvc.web.controller"/>
3  <!-- 注解驅動 -->
4 <mvc:annotation-driven/>

 

 

 

Controller.java   兩種形式都可以,但是第二種,jsp頁面中的參數是personList1

 1 //列表
 2     @RequestMapping("/listAll")
 3     public String listAll(Map<String,Object> model){
 4         List<Person> personList = ps.listAll();
 5         model.put("personList", personList);
 6         
 7         System.out.println(" listall hello");
 8         
 9         return "person/jPersonList";
10     }
11     
12     //列表
13     @RequestMapping("/listAllOther")
14     public String listAllOther(Model model){
15         List<Person> personList1 = ps.listAll();
16         model.addAttribute(personList1);
17         
18         System.out.println(" listallother1 hello");
19         
20         return "person/jPersonList";
21     }

 

jsp頁面中

 1 <%@ page language="java" pageEncoding="UTF-8"%>
 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 3 <html>
 4   <head>
 5     <title>My JSP 'index.jsp' starting page</title>
 6   </head>
 7   
 8   <body>
 9 <h2>用戶列表</h2>
10     
11         <div style="padding:10px;"><a href="${pageContext.request.contextPath}/person/tocreate.action">新增</a></div>
12     
13 <table border="1">
14 <tr>
15     <td>photo</td>
16     <td>id</td>
17     <td>name</td>
18     <td>age</td>
19     <td>操作</td>
20 </tr>
21 
22 <c:forEach items="${personList}" var="p">
23 <tr>
24     <td><img src="${pageContext.request.contextPath}"/></td>
25     <td>${p.id}</td>
26     <td>${p.name}</td>
27     <td>${p.age}</td>
28     <td>
29         <a href="${pageContext.request.contextPath}/person/toupdate.action?id=${p.id}">修改</a>
30         <a href="${pageContext.request.contextPath}/person/delete.action?delId=${p.id}">刪除</a>
31     </td>
32 </tr>
33 </c:forEach>
34 
35 </table>
36   </body>
37 </html>

 


免責聲明!

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



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