用户通过查询,后台得到查询数据,那么struts2这么把查询的数据,在页面显示?做个小测试。
创建项目
导入jar包(如果不知道导入哪些jar,参考前面章节)测试jar包为2.5
配置web.xml
写测试
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>struts2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 搭建struts2前端控制器,用来过滤用户的请求,提供统一的接口 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3.ReturnValue Action
package com.struts.action;
import java.util.ArrayList;
import java.util.List;
public class RetunValue {
private User user;
private List<User> users;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public String listUser(){
List<User> list = new ArrayList<User>();
list.add(new User("刘备", "liubei", "三国蜀国君主"));
list.add(new User("关羽", "guanyu", "关二爷"));
list.add(new User("张飞", "zhangfei", "张翼德"));
list.add(new User("诸葛亮", "zhugeliang", "卧龙"));
this.users = list;
return "user";
}
}
4. User测试对象
package com.struts.action;
public class User {
private String userName;
private String passWorld;
private String address;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWorld() {
return passWorld;
}
public void setPassWorld(String passWorld) {
this.passWorld = passWorld;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [userName=" + userName + ", passWorld=" + passWorld + ", address=" + address + "]";
}
public User(String userName, String passWorld, String address) {
super();
this.userName = userName;
this.passWorld = passWorld;
this.address = address;
}
public User() {
super();
}
}
5.写过上面的,配置下struts.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name="ac" extends="struts-default" namespace="/uu">
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="*_*" class="com.struts.action.{1}" method="{2}">
<result name="user">/user.jsp</result>
</action>
</package>
</struts>
6. index.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path;
%>
<!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>
</head>
<body>
<a href="<%=basePath %>/uu/RetunValue_listUser">遍历查询用户</a><br/>
</body>
</html>
7.user.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!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>
</head>
<body>
<table style="widows: 300px;" cellspacing="0" border="1px" bordercolor="red" align="center">
<caption>----user遍历测试----</caption>
<tr>
<th>序号</th>
<th>姓名</th>
<th>密码</th>
<th>地址</th>
</tr>
<s:iterator value="users" status="e">
<!-- even为偶数 odd为奇数-->
<s:if test="#e.even">
<tr bgcolor="red;">
<td><s:property value="#e.count"/></td>
<td><s:property value="userName"/></td>
<td><s:property value="passWorld"/></td>
<td><s:property value="address"/></td>
</tr>
</s:if>
<s:else>
<tr bgcolor="blue">
<td><s:property value="#e.count"/></td>
<td><s:property value="userName"/></td>
<td><s:property value="passWorld"/></td>
<td><s:property value="address"/></td>
</tr>
</s:else>
</s:iterator>
</table>
<h3>分页数字测试</h3>
<s:iterator begin="1" end="10" var="i">
<s:property value="#i"/>
</s:iterator>
<table style="widows: 300px;" cellspacing="0" border="1px" bordercolor="red" align="center">
<caption>----user遍历测试----</caption>
<tr>
<th>序号</th>
<th>姓名</th>
<th>密码</th>
<th>地址</th>
</tr>
<s:iterator value="users" status="e">
<!-- even为偶数 odd为奇数-->
<tr <s:if test="#e.even">bgcolor="blue"</s:if>
<s:else>bgcolor="red"</s:else>
>
<td><s:property value="#e.count"/></td>
<td><s:property value="userName"/></td>
<td><s:property value="passWorld"/></td>
<td><s:property value="address"/></td>
</tr>
</s:iterator>
</table>
</body>
</html>
8.测试结果页面
转载于:https://blog.51cto.com/zhuws/1927797