詳解springmvc 中controller與jsp傳值
更新時間:2017年07月06日 11:07:04 作者:你的承諾早已氾黃
本篇文章主要介紹了springmvc 中controller與jsp傳值,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
在springmvc中的controller所對應的函數中,如果需要從*.jsp頁面中獲取數據,可以自行在函數括號中寫,springmvc會自動封裝傳過來的值。
spring-mvc.xml 中加入如下語句:
<!-- 自動掃描 --> <context:component-scan base-package="cn.itcast.springmvc.service,cn.itcast.springmvc.web.controller"/> <!-- 注解驅動 --> <mvc:annotation-driven/>
Controller.java 兩種形式都可以,但是第二種,jsp頁面中的參數是personList1
//列表
@RequestMapping("/listAll")
public String listAll(Map<String,Object> model){
List<Person> personList = ps.listAll();
model.put("personList", personList);
System.out.println(" listall hello");
return "person/jPersonList";
}
//列表
@RequestMapping("/listAllOther")
public String listAllOther(Model model){
List<Person> personList1 = ps.listAll();
model.addAttribute(personList1);
System.out.println(" listallother1 hello");
return "person/jPersonList";
}
jsp頁面中
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<h2>用戶列表</h2>
<div style="padding:10px;"><a href="${pageContext.request.contextPath}/person/tocreate.action" rel="external nofollow" >新增</a></div>
<table border="1">
<tr>
<td>photo</td>
<td>id</td>
<td>name</td>
<td>age</td>
<td>操作</td>
</tr>
<c:forEach items="${personList}" var="p">
<tr>
<td><img src="${pageContext.request.contextPath}"/></td>
<td>${p.id}</td>
<td>${p.name}</td>
<td>${p.age}</td>
<td>
<a href="${pageContext.request.contextPath}/person/toupdate.action?id=${p.id}" rel="external nofollow" >修改</a>
<a href="${pageContext.request.contextPath}/person/delete.action?delId=${p.id}" rel="external nofollow" >刪除</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
使用springMVC通過Filter實現(xiàn)防止xss注入
這篇文章主要介紹了使用springMVC通過Filter實現(xiàn)防止xss注入的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
Java索引越界異常Exception java.lang.IndexOutOfBoundsException
本文主要介紹了Java索引越界異常Exception java.lang.IndexOutOfBoundsException的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06
Java中使用WebUploader插件上傳大文件單文件和多文件的方法小結
這篇文章主要介紹了Java中使用WebUploader插件上傳大文件單文件和多文件的方法小結的相關資料,需要的朋友可以參考下2016-06-06

