正文
Struts2(六.用标签显示用户列表及Value Stack和Stack Context)
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
一.用Struts2标签显示用户列表
原理:
在struts中可以通过在action中将所有用户的信息存入到某个范围中,然后转向userlist.jsp,进行访问
原则:
在jsp网页上,尽量不要出现Java代码
1.main.jsp
<a href="${pageContext.request.contextPath}/user/list">用户列表</a>
2.user.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"><struts>
<package name="user" namespace="/user" extends="all"> <!-- 通配符方式配置action,解决action太多的问题 -->
<action name="*" class="myuser.UserAction" method="{1}">
<!-- 控制action中局部跳转 -->
<result name="main">${pageContext.request.contextPath}/main.jsp</result>
<result name="login">${pageContext.request.contextPath}/login.jsp</result>
<!--
加了type="redirect"代表,容器外跳转
和跳转到的页面不是同一次服务器请求,不共享request
在这里容器外跳转是跳不到userlist.jsp的,因为通过浏览器无法访问web-inf目录下的文件夹,所以不能用容器外跳转
-->
<result name="list">/WEB-INF/user/userlist.jsp</result>
<result name="modify">/WEB-INF/user/modify.jsp</result>
</action>
</package>
</struts>
3.UserAciton
/**
* 用户列表
* @return
* @throws ClassNotFoundException
* @throws SQLException
* @throws NamingException
*/
public String list() throws ClassNotFoundException, SQLException, NamingException
{
UserDAO dao=new UserDAO();
ArrayList<User> list=dao.getUsers();
//存入ActionContext范围之内
ActionContext ctx=ActionContext.getContext();
ctx.put("USERLIST", list);
return "list";
}
4.UserDAO
/**
* 得到用户列表
* @return
* @throws SQLException
*/
public ArrayList<User> getUsers() throws SQLException
{
ArrayList<User> list=new ArrayList<User>();
sql="select * from users";
ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while(rs.next())
{
User user=new User();
user.setId(rs.getInt(1));
user.setUserName(rs.getString(2));
user.setPwd(rs.getString(3));
list.add(user);
}
conn.close();
return list;
}
5.userlist.jsp
把userlist.jsp放入WEB/INF/目录下,让用户不能直接访问,因为如果直接访问,不走action取不到数据
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/style.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/jslib/jquery-1.11.1.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/layer/layer.min.js"></script>
</head>
<body>
用户列表
<br>
<br>
<table class="bordered">
<thead>
<tr><th>序号</th><th>用户名</th><th>密码</th><th>照片</th><th>删除</th><th>修改</th></tr>
</thead>
<!-- USERLIST,cuser,s存入的Stack Context -->
<s:iterator value="#USERLIST" id="cuser" status="s">
<tr>
<td><s:property value="#s.index+1"/></td>
<td><s:property value="#cuser.userName"/></td>
<td><s:property value="#cuser.pwd"/></td>
</tr>
</s:iterator>
</table>
<br>
<br>
<a href="${pageContext.request.contextPath}/main.jsp">返回主页面</a>
</body>
<!-- 调试
Value Stack 访问时不用加#
Stack Context 访问时要加#
-->
<s:debug></s:debug>
</html>
二.Value Stack和Stack Context
利用struts2的标签可以进行查看
<!-- 调试
Value Stack 访问时不用加#
Stack Context 访问时要加#
-->
<s:debug></s:debug>
1.Value Stack
值栈
里面存放的数据访问时不用加#
2.Stack Context
里面存放的数据访问时要加#
用法:
在Action中把userlist存入了ActionContext范围之内
<!-- USERLIST,cuser,s存入的Stack Context -->
<s:iterator value="#USERLIST" id="cuser" status="s">
<tr>
<td><s:property value="#s.index+1"/></td>
<td><s:property value="#cuser.userName"/></td>
<td><s:property value="#cuser.pwd"/></td>
</tr>
</s:iterator>
</table>
<br>
<br>
<a href="${pageContext.request.contextPath}/main.jsp">返回主页面</a>
</body>