标签:
先定义两个实体类User和Address
User
package cn.entity; public class User { private String name; private int age; private Address address; public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } 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; } }
Address
package cn.entity; public class Address { private String country; // 国家 private String city; // 城市 private String street; // 街道 public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } }
创建出一个action先创建出两个成员变量 然后在核心方法中实例化成员变量。
package cn.action; import cn.entity.Address; import cn.entity.User; import com.opensymphony.xwork2.ActionSupport; public class ShowOriginalAndJavaBeanAction extends ActionSupport { private User user; private String message; public String execute(){ setUser(new User()); getUser().setName("jbit"); getUser().setAge(23); Address address=new Address(); address.setCountry("中国"); address.setCity("北京"); address.setStreet("长安街"); getUser().setAddress(address); setMessage("展示原始类型和JavaBean"); return SUCCESS; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
核心配置:struts.xml
<?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> <package name="default" extends="struts-default"> <action name="showOriginalAndJavaBeanAction" class="cn.action.ShowOriginalAndJavaBeanAction"> <result>result.jsp</result> </action> </package> </struts>
result页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘result.jsp‘ starting page</title> </head> <body> 信息:<s:property value="message" default="展示数据" /><br/> 姓名:<s:property value="user.name" /><br/> 年龄:<s:property value="user.age" /><br/> 国家:<s:property value="user.address.country" /> <s:debug></s:debug> </body> </html>
property标签用于输出ValueStack中对象属性的值,其中value属性用来指定要输出对象的属性,如果没有value属性,则默认输出ValueStack栈顶的对象,类型为Object;default属性是属性值为空时默认的默认值,类型为String
效果展示:
二>>多值类型请求参数的处理
如果在请求中出现多个值对应同一个请求参数的情况,Struts2会将这样的数据转换为面向集合的数据类型,如集合,数组
1.数组
表单代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘index.jsp‘ starting page</title> </head> <body> <s:form action="ArraysDataTransfer.action"> <s:textfield name="hobbies" label="爱好:"/> <s:textfield name="hobbies" label="爱好:"/> <s:textfield name="hobbies" label="爱好:"/> <s:textfield name="numbers[0]" label="数字:"/> <s:textfield name="numbers[1]" label="数字:"/> <s:textfield name="numbers[2]" label="数字:"/> <s:submit value="提交"/> </s:form> </body> </html>
第一个数组属性是hobbies,接收提交的爱好信息;第二个数组属性是numbers,接收提交的数字,如果数据转移要正常工作,这些属性必须存在ValueStack中。在Action类中公开这些属性
ArraysDataTransferAction
package cn.action; import com.opensymphony.xwork2.ActionSupport; public class ArraysDataTransferAction extends ActionSupport { private String[] hobbies; private Double[] numbers=new Double[3]; public String execute(){ System.out.println("hobbies的长度是"+hobbies.length); for(int i=0;i<hobbies.length;i++){ System.out.println("hobbies数组中第"+i+"个元素是:"+hobbies[i]); } System.out.println("numbers的长度是"+numbers.length); for(int i=0;i<numbers.length;i++){ System.out.println("numbers数组中第"+i+"个元素是:"+numbers[i]); } return SUCCESS; } public String[] getHobbies() { return hobbies; } public void setHobbies(String[] hobbies) { this.hobbies = hobbies; } public Double[] getNumbers() { return numbers; } public void setNumbers(Double[] numbers) { this.numbers = numbers; } }
struts.xml
<action name="ArraysDataTransfer" class="cn.action.ArraysDataTransferAction"> <result name="success">result2.jsp</result> </action>
result2.jsp
<body> 请查看控制台的输出 </body>
2.集合
使用List与使用数组几乎相同,仅有不同的是集合类型可以借助泛型实现
表单代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘index.jsp‘ starting page</title> </head> <body> <s:form action="listDataTransfer"> <s:textfield name="hobbies" label="爱好:"/> <s:textfield name="hobbies" label="爱好:"/> <s:textfield name="hobbies" label="爱好:"/> <s:textfield name="numbers[0]" label="数字:"/> <s:textfield name="numbers[1]" label="数字:"/> <s:textfield name="numbers[2]" label="数字:"/> <s:textfield name="users.name" label="姓名:"/> <s:textfield name="users.name" label="姓名:"/> <s:textfield name="users.name" label="姓名:"/> <s:submit value="提交"/> </s:form> </body> </html>
相同名称参数有多个数据输入,可以将具有相同名称的参数看作一个集合,特殊说明的是users.name,当ognl解析这个表达式时,会首先定位user属性,与之不同的是,user是一个指定了元素为User对象的List。
ListDataTransferAction
package cn.action; import com.opensymphony.xwork2.ActionSupport; import cn.entity.User; import java.util.*; public class ListDataTransferAction extends ActionSupport { private List<String> hobbies; private List<Double> numbers; private List<User> users; public String execute(){ System.out.println("hobbies的长度是"+hobbies.size()); for(int i=0;i<hobbies.size();i++){ System.out.println("hobbies数组中第"+i+"个元素是:"+hobbies.get(i)); } System.out.println("numbers的长度是"+numbers.size()); for(int i=0;i<numbers.size();i++){ System.out.println("numbers数组中第"+i+"个元素是:"+numbers.get(i)); } System.out.println("users的长度是"+users.size()); for(int i=0;i<users.size();i++){ System.out.println("users数组中第"+i+"个元素是:"+users.get(i).getName()); } return SUCCESS; } public List<String> getHobbies() { return hobbies; } public void setHobbies(List<String> hobbies) { this.hobbies = hobbies; } public List<Double> getNumbers() { return numbers; } public void setNumbers(List<Double> numbers) { this.numbers = numbers; } public List<User> getUsers() { return users; } public void setUsers(List<User> users) { this.users = users; } }
struts.xml
<action name="listDataTransfer" class="cn.action.ListDataTransferAction"> <result>result2.jsp</result> </action>
result2.jsp
<body> 请查看控制台的输出 </body>
三>>访问集合对象
package cn.action; import java.util.ArrayList; import java.util.List; import cn.entity.Address; import cn.entity.User; import com.opensymphony.xwork2.ActionSupport; public class ShowArraysAndListAction extends ActionSupport { private String[] hobbies; private List<Double> numbers; private List<User> users; private User user; public String execute() throws Exception { users=new ArrayList<User>(); user=new User(); user.setUsername("火狐"); user.setPassword("11"); Address address=new Address(); address.setCountry("中国"); address.setCity("北京"); address.setStreet("长安街"); user.setAddress(address); users.add(user); user=new User(); user.setUsername("hh"); user.setPassword("10"); Address address2=new Address(); address2.setCountry("中国"); address2.setCity("北京"); address2.setStreet("海淀"); user.setAddress(address); users.add(user); return SUCCESS; } public String[] getHobbies() { return hobbies; } public void setHobbies(String[] hobbies) { this.hobbies = hobbies; } public List<Double> getNumbers() { return numbers; } public void setNumbers(List<Double> numbers) { this.numbers = numbers; } public List<User> getUsers() { return users; } public void setUsers(List<User> users) { this.users = users; } }
实体类
User
package cn.entity; public class User { //用户输入的姓名 private String username; //用户输入的密码 private String password; private Address address; public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } /* * 获取名字 * @return 名字 */ public String getUsername() { return username; } /* * 设置名字 * @para name名字 */ public void setUsername(String username) { this.username = username; } /* * 获取密码 * @return 密码 */ public String getPassword() { return password; } /* * 设置密码 * @para password密码 */ public void setPassword(String password) { this.password = password; } }
Address
package cn.entity; public class Address { private String country; // 国家 private String city; // 城市 private String street; // 街道 public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } }
struts.xml
<action name="showArrayAndListAction" class="cn.action.ShowArraysAndListAction"> <result name="success">result.jsp</result> </action>
result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘result.jsp‘ starting page</title> </head> <body> 迭代数组输出:<br/> <s:iterator value="hobbies"> <s:property /><br/> </s:iterator> 迭代集合(元素类型为Double):<br/> <s:iterator value="numbers"> <s:property /><br/> </s:iterator> 迭代集合(元素类型为User):<br/> <s:iterator value="users"> 用户名:<s:property value="username" /><br /> 密码:<s:property value="password" /><br /> 国家:<s:property value="address.country" /><br /> </s:iterator> <s:debug></s:debug> </body> </html>
效果:
标签:
原文地址:http://www.cnblogs.com/6zhi/p/5946433.html