詳解springmvc 中controller與jsp傳值
在springmvc中的controller所對(duì)應(yīng)的函數(shù)中,如果需要從*.jsp頁(yè)面中獲取數(shù)據(jù),可以自行在函數(shù)括號(hào)中寫,springmvc會(huì)自動(dòng)封裝傳過來的值。
spring-mvc.xml 中加入如下語(yǔ)句:
<!-- 自動(dòng)掃描 --> <context:component-scan base-package="cn.itcast.springmvc.service,cn.itcast.springmvc.web.controller"/> <!-- 注解驅(qū)動(dòng) --> <mvc:annotation-driven/>
Controller.java 兩種形式都可以,但是第二種,jsp頁(yè)面中的參數(shù)是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頁(yè)面中
<%@ 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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringMVC的簡(jiǎn)單傳值(實(shí)現(xiàn)代碼)
- springMvc請(qǐng)求的跳轉(zhuǎn)和傳值的方法
- 詳解SpringMVC注解版前臺(tái)向后臺(tái)傳值的兩種方式
- jQuery+SpringMVC中的復(fù)選框選擇與傳值實(shí)例
- java-jsp springmvc-controller 傳值到頁(yè)面的方法
- springmvc前臺(tái)向后臺(tái)傳值幾種方式總結(jié)(從簡(jiǎn)單到復(fù)雜)
- springMVC中RestTemplate傳值接值方法
- SpringMVC的ModelAndView傳值方法
- springMVC向Controller傳值出現(xiàn)中文亂碼的解決方案
- SpringMVC前后端傳值的幾種實(shí)現(xiàn)方式
相關(guān)文章
使用springMVC通過Filter實(shí)現(xiàn)防止xss注入
這篇文章主要介紹了使用springMVC通過Filter實(shí)現(xiàn)防止xss注入的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
spring對(duì)JDBC和orm的支持實(shí)例詳解
這篇文章主要介紹了spring對(duì)JDBC和orm的支持實(shí)例詳解,需要的朋友可以參考下2017-09-09
Java索引越界異常Exception java.lang.IndexOutOfBoundsException
本文主要介紹了Java索引越界異常Exception java.lang.IndexOutOfBoundsException的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
java利用反射實(shí)現(xiàn)動(dòng)態(tài)代理示例
這篇文章主要介紹了java利用反射實(shí)現(xiàn)動(dòng)態(tài)代理示例,需要的朋友可以參考下2014-04-04
Java中使用WebUploader插件上傳大文件單文件和多文件的方法小結(jié)
這篇文章主要介紹了Java中使用WebUploader插件上傳大文件單文件和多文件的方法小結(jié)的相關(guān)資料,需要的朋友可以參考下2016-06-06

