package com.model;
public class User {
//name属性
private String name;
//age属性
private int age;
//tel属性
private String tel;
//各属性的getter和setter()方法
}
package com.action;
import java.util.List;
import com.model.User;
import com.opensymphony.xwork2.ActionSupport;
public class ListAction extends ActionSupport{
private static final long serialVersionUID = 1L;
//users属性,为List类型
private List<User> users;
//getter方法
public List<User> getUsers() {
return users;
}
//setter方法
public void setUsers(List<User> users) {
this.users = users;
}
//重载execute()方法
public String execute() throws Exception {
for (User user : users) {
System.out.println("Name:"+user.getName()+" Age:"+user.getAge()+" Tel:"+user.getTel());
}
return SUCCESS;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- default包-->
<package name="default" namespace="/" extends="struts-default">
<!-- list Action返回success.jsp页面 -->
<action name="list" class="com.action.ListAction">
<result>/success.jsp</result>
</action>
</package>
</struts>
<%@ 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>
<center>
<!-- 表单 -->
<s:form action="list">
<!-- 表格 -->
<table >
<tr>
<td></td>
<td>first:</td>
<td>second:</td>
</tr>
<tr>
<td>Name:</td>
<!-- 设置list元素-->
<td><s:textfield name="users[0].name" theme="simple"/></td>
<td><s:textfield name="users[1].name" theme="simple"/></td>
</tr>
<tr>
<td>Age:</td>
<td><s:textfield name="users[0].age" theme="simple"/></td>
<td><s:textfield name="users[1].age" theme="simple"/></td>
</tr>
<tr>
<td>Tel:</td>
<td><s:textfield name="users[0].tel" theme="simple"/></td>
<td><s:textfield name="users[1].tel" theme="simple"/></td>
</tr>
<tr>
<td colspan="3">
<s:submit></s:submit>
</td>
</tr>
</table>
</s:form>
</center>
</body>
</html>
<%@ 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>
<center>
Name:<s:property value="users[0].name"/>
Age:<s:property value="users[0].age"/>
Tel:<s:property value="users[0].tel"/><br/>
Name:<s:property value="users[1].name"/>
Age:<s:property value="users[1].age"/>
Tel:<s:property value="users[1].tel"/>
</center>
</body>
</html>
<%@ taglib uri="/struts-tags" prefix="s"%>
<body>
<!-- 使用ognl+struts2标签实现计算字符串长度
value属性值:ognl表达式
-->
<s:property value="'haha'.length()"/>
</body>