还是上一个例子,为Bean添加一个属性——地址,他是一个List;
package com.xh.struts2.bean; import java.util.List; public class User { private String userName; private String password; private int age; private List<String> addrs; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<String> getAddrs() { return addrs; } public void setAddrs(List<String> addrs) { this.addrs = addrs; } }
在注册页面要求输入多个地址:
<%@ 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>注册</title> </head> <body> <form action="rigest" method="post"> <s:textfield name="user.userName" label="name"></s:textfield> <s:textfield name="user.password" label="password"></s:textfield> <s:textfield name="user.age" label="age"></s:textfield> <s:textfield name="user.addrs" label="addrs"></s:textfield> <s:textfield name="user.addrs" label="addrs"></s:textfield> <s:textfield name="user.addrs" label="addrs"></s:textfield> <button type="submit">注 册</button> </form> </body> </html>
并且在成功页面打印出来:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>欢迎</title> </head> <body> <h2>欢迎</h2> <h3><s:property value="user.userName"/></h3> <h3><s:property value="user.password"/></h3> <h3><s:property value="user.age"/></h3> <h3><s:property value="user.addrs[0]"/></h3> <h3><s:property value="user.addrs[1]"/></h3> <h3><s:property value="user.addrs[2]"/></h3> </body> </html>