java-jsp springmvc-controller 傳值到頁面

草青工作室發表於2017-03-30
java-jsp springmvc-controller 傳值到頁面


藉助 org.springframework.ui.Model 物件或 Map 物件將資訊傳到 springmvc 的頁面中


需要:jstl 支援 和 EL 表示式


1.maven 新增依賴:
    <!-- jstl(jsp 標準標籤庫) -->
	<dependency>
	    <groupId>jstl</groupId>
	    <artifactId>jstl</artifactId>
	    <version>1.2</version>
	</dependency>






2.controller 中的方法
/** 
*
*/
@Controller
@RequestMapping("/hello")
public class HelloController {
	/**
	 * 請求地址:專案名/hello/userinfo
	 * @return
	 */
	@RequestMapping("userinfo")
	public String userinfo(org.springframework.ui.Model model,Map<String, Object> map){
		//傳遞單個物件
		UserInfoModel info = new UserInfoModel("張三",99,"男");
		model.addAttribute("info",info);		
		
		List<UserInfoModel> list = new ArrayList<UserInfoModel>();
		list.add(new UserInfoModel("李四",20,"男"));
		list.add(new UserInfoModel("王五",21,"女"));
		list.add(new UserInfoModel("趙六",22,"男"));
		
		//傳遞多個物件
		map.put("user_list", list);
		
		return "hello/userinfo"; //servlet.xml 中定義的 字首(prefix)+返回值+字尾(suffix)拼接的實體地址
	}
}




3.測試實體物件

package demo.models.hello;


import java.util.Date;


/** 
*
*/
public class UserInfoModel {
	public UserInfoModel(String name,int age,String sex){
		this.name=name;
		this.age=age;
		this.sex=sex;
		this.date = new Date();
	}
	String name;
	int age;
	String sex;
	Date date;
	
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
}




4.jsp 頁面
<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!-- 引入jstl core庫 ,使用 迴圈標籤-->	
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!-- 引入jstl fmt庫,格式化時間標籤 -->
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!-- 開啟 EL 表示式支援 -->
<%@ page isELIgnored="false"%>
<!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>Insert title here</title>
	<script scr="/content/index.js"></script>
	<link href="/content/index.css"/>
</head>
<body>
	<h3>頁面資訊</h3>
	<div>url:${pageContext.request.contextPath}</div>
	<h3>使用者資訊</h3>
	<div>name:${info.name}</div>
	<div>age:${info.age}</div>
	<div>sex:${info.sex}</div>
	<div>date:<fmt:formatDate pattern="yyyy-MM-dd HH:mm:ss" value="${info.date}" /></div>
	<h3>使用者列表</h3>
	<table border="1">
	<tr>
		<td>name</td>
		<td>age</td>
		<td>sex</td>
		<td>date</td>
	</tr>
	<c:forEach items="${user_list}" var="p">
	<tr>
		<td>${p.name}</td>
		<td>${p.age}</td>
		<td>${p.sex}</td>
		<td><fmt:formatDate pattern="yyyy-MM-dd HH:mm:ss" value="${p.date}"/></td>
	</tr>
	</c:forEach>
	<tr>
		<td colspan="4" style="text-align: center;">jstl forEach</td>
	</tr>	
	<tr>
		<td>行號</td>
		<td>col1</td>
		<td>col2</td>
		<td>col3</td>
	</tr>
	<c:forEach begin="1" end="5" var="i">
	<tr>
		<td>${i}</td>
		<td>${i+2}</td>
		<td>${i+3}</td>
		<td>${i+4}</td>
	</tr>
	</c:forEach>
	</table>
</body>
</html>


相關文章