- 背景:
本章节主要以复杂属性、集合属性类型转化为例,来学习这两种情况下怎么使用。
- 复杂对象属性转换场景:
1、新建struts_04
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts 02</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.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="default" namespace="/" extends="struts-default">
<action name="abstractPropertyAction" class="com.dx.struts2app.ConversionAction" method="execute">
<result>/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
package com.dx.struts2app; import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; public class ConversionAction extends ActionSupport implements ModelDriven<Member> {
private static final long serialVersionUID = 1L;
private Member member; public String execute() {
System.out.println(this.member); return SUCCESS;
} @Override
public Member getModel() {
member = new Member();
return member;
}
}
com.dx.struts2app.Member.java(Member属性中包含了department属性)
package com.dx.struts2app; public class Member {
private String id;
private String name;
private Department department; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Department getDepartment() {
return department;
} public void setDepartment(Department department) {
this.department = department;
} @Override
public String toString() {
return "Member [id=" + id + ", name=" + name + ", department=" + department + "]";
} }
package com.dx.struts2app; import java.awt.Point;
import java.util.Date; public class Department {
private String name;
private Point position;
private Date createTime; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Point getPosition() {
return position;
} public void setPosition(Point position) {
this.position = position;
} public Date getCreateTime() {
return createTime;
} public void setCreateTime(Date createTime) {
this.createTime = createTime;
} @Override
public String toString() {
return "Department [name=" + name + ", position=" + position + ", createTime=" + createTime + "]";
} }
com.dx.converters.PointConverter.java
package com.dx.converters; import java.awt.Point;
import java.util.Map; import org.apache.struts2.util.StrutsTypeConverter; public class PointConverter extends StrutsTypeConverter {
public PointConverter() {
System.out.println("PointConverter's constructor...");
} @Override
public Object convertFromString(Map context, String[] values, Class toClass) {
System.out.println("convertFromString..."); if (toClass == Point.class) {
if (values != null && values.length > 0 && values[0].indexOf(",") > 0) {
try {
int x = Integer.valueOf(values[0].split(",")[0]);
int y = Integer.valueOf(values[0].split(",")[1]); return new Point(x, y);
} catch (Exception ex) {
ex.printStackTrace();
}
}
} return values;
} @Override
public String convertToString(Map context, Object o) {
System.out.println("convertToString..."); if (o instanceof Point) {
Point point = (Point) o;
return point.getX() + "," + point.getY();
}
return null;
} }
src下新建文件xwork-conversion.properties,并编辑内容:
java.awt.Point=com.dx.converters.PointConverter
index.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>My test page</title>
</head>
<body>
<s:debug></s:debug>
<s:form action="abstractPropertyAction" method="post">
<s:textfield name="name" label="Name"></s:textfield>
<s:textfield name="department.name" label="department.name"></s:textfield>
<s:textfield name="department.position" label="department.position"></s:textfield>
<s:textfield name="department.createTime"
label="department.createTime"></s:textfield>
<s:submit label="提交"></s:submit>
</s:form>
</body>
</html>
success.jsp
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
SUCCESS!
</body>
</html>
访问index.jsp,并提交:
成功提交了,成功的转换了java.awt.Point:
后台打印信息:
2017-04-04 15:01:20,259 INFO [localhost-startStop-1] log4j2.Log4j2Logger (Log4j2Logger.java:45) - Choosing bean (struts) for (com.opensymphony.xwork2.security.AcceptedPatternsChecker)
PointConverter's constructor...
四月 04, 2017 3:01:20 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Work\Java\apache-tomcat-7.0.75-windows-x64\apache-tomcat-7.0.75\webapps\docs
四月 04, 2017 3:01:20 下午 org.apache.catalina.startup.HostConfig deployDirectory
。。。。。。
信息: Starting ProtocolHandler ["ajp-bio-8009"]
四月 04, 2017 3:01:20 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 6488 ms
convertFromString...
Member [id=null, name=123, department=Department [name=234, position=java.awt.Point[x=5,y=6], createTime=Tue Apr 04 14:44:45 CST 2017]]
- 复杂对象属性转换场景:
修改Member.java类:
package com.dx.struts2app; import java.util.ArrayList;
import java.util.List; import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; public class ConversionAction extends ActionSupport implements ModelDriven<List<Member>> {
private static final long serialVersionUID = 1L;
private List<Member> members; public String execute() {
System.out.println(this.members); return SUCCESS;
} public List<Member> getMembers() {
return members;
} public void setMembers(List<Member> members) {
this.members = members;
} @Override
public List<Member> getModel() {
members = new ArrayList<Member>();
return members;
}
}
修改index.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>My test page</title>
</head>
<body>
<s:debug></s:debug>
<s:form action="abstractPropertyAction" method="post">
<s:textfield name="members[0].name" label="members[0].Name"></s:textfield>
<s:textfield name="members[0].department.name" label="members[0].department.name"></s:textfield>
<s:textfield name="members[0].department.position" label="members[0].department.position"></s:textfield>
<s:textfield name="members[0].department.createTime" label="members[0].department.createTime"></s:textfield>
<br/>
<s:textfield name="members[1].name" label="members[1].Name"></s:textfield>
<s:textfield name="members[1].department.name" label="members[1].department.name"></s:textfield>
<s:textfield name="members[1].department.position" label="members[1].department.position"></s:textfield>
<s:textfield name="members[1].department.createTime" label="members[1].department.createTime"></s:textfield>
<s:submit label="提交"></s:submit>
</s:form>
</body>
</html>
访问index.jsp页面:
提交表单成功,后台打印信息:
。。。。。。
2017-04-04 15:28:57,351 INFO [localhost-startStop-1] log4j2.Log4j2Logger (Log4j2Logger.java:45) - Choosing bean (struts) for (com.opensymphony.xwork2.security.AcceptedPatternsChecker)
PointConverter's constructor...
信息: Starting ProtocolHandler ["ajp-bio-8009"]
四月 04, 2017 3:28:59 下午 org.apache.catalina.startup.Catalina start
。。。。。。
信息: Server startup in 8302 ms
2017-04-04 15:29:06,546 WARN [http-bio-8080-exec-3] log4j2.Log4j2Logger (Log4j2Logger.java:57) - No configuration found for the specified action: 'abstractPropertyAction' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
convertFromString...
convertFromString...
[Member [id=null, name=12, department=Department [name=11, position=java.awt.Point[x=2,y=3], createTime=Tue Apr 04 15:13:20 CST 2017]], Member [id=null, name=121, department=Department [name=111, position=java.awt.Point[x=21,y=31], createTime=Tue Apr 04 15:13:21 CST 2017]]]
- 集合嵌套集合属性类型转换(基于集合属性类型转换修改):
Member.java
package com.dx.struts2app; import java.util.List; public class Member {
private String id;
private String name;
private List<Department> departments; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public List<Department> getDepartments() {
return departments;
} public void setDepartments(List<Department> departments) {
this.departments = departments;
} @Override
public String toString() {
return "Member [id=" + id + ", name=" + name + ", departments=" + departments + "]";
}
}
index.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>My test page</title>
</head>
<body>
<s:debug></s:debug>
<s:form action="abstractPropertyAction" method="post">
<s:textfield name="members[0].name" label="members[0].Name"></s:textfield>
<s:textfield name="members[0].departments[0].name" label="members[0].departments[0].name"></s:textfield>
<s:textfield name="members[0].departments[0].position" label="members[0].departments[0].position"></s:textfield>
<s:textfield name="members[0].departments[0].createTime" label="members[0].departments[0].createTime"></s:textfield> <s:textfield name="members[0].departments[1].name" label="members[0].departments[1].name"></s:textfield>
<s:textfield name="members[0].departments[1].position" label="members[0].departments[1].position"></s:textfield>
<s:textfield name="members[0].departments[1].createTime" label="members[0].departments[1].createTime"></s:textfield> <s:textfield name="members[1].name" label="members[1].Name"></s:textfield>
<s:textfield name="members[1].departments[0].name" label="members[1].departments[0].name"></s:textfield>
<s:textfield name="members[1].departments[0].position" label="members[1].departments[0].position"></s:textfield>
<s:textfield name="members[1].departments[0].createTime" label="members[1].departments[0].createTime"></s:textfield> <s:textfield name="members[1].departments[1].name" label="members[1].departments[1].name"></s:textfield>
<s:textfield name="members[1].departments[1].position" label="members[1].departments[1].position"></s:textfield>
<s:textfield name="members[1].departments[1].createTime" label="members[1].departments[1].createTime"></s:textfield>
<s:submit label="提交"></s:submit>
</s:form>
</body>
</html>
访问页面index.jsp
提交成功,后台打印信息
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
PointConverter's constructor...
2017-04-04 15:40:54,859 INFO [localhost-startStop-1] log4j2.Log4j2Logger (Log4j2Logger.java:45) - Parsing configuration file [struts-default.xml]
。。。。。。
2017-04-04 15:40:54,936 INFO [localhost-startStop-1] log4j2.Log4j2Logger (Log4j2Logger.java:45) - Choosing bean (struts) for (com.opensymphony.xwork2.security.AcceptedPatternsChecker)
PointConverter's constructor...
四月 04, 2017 3:40:55 下午 org.apache.catalina.startup.TldConfig execute
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
PointConverter's constructor...
2017-04-04 15:40:56,519 INFO [localhost-startStop-1] log4j2.Log4j2Logger (Log4j2Logger.java:45) - Parsing configuration file [struts-default.xml]
。。。。。。
2017-04-04 15:40:56,625 INFO [localhost-startStop-1] log4j2.Log4j2Logger (Log4j2Logger.java:45) - Choosing bean (struts) for (com.opensymphony.xwork2.security.AcceptedPatternsChecker)
PointConverter's constructor...
四月 04, 2017 3:40:56 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Work\Java\apache-tomcat-7.0.75-windows-x64\apache-tomcat-7.0.75\webapps\docs
。。。。。。
信息: Server startup in 9584 ms
四月 04, 2017 3:41:01 下午 org.apache.jasper.compiler.TldLocationsCache tldScanJar
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2017-04-04 15:41:02,939 WARN [http-bio-8080-exec-4] log4j2.Log4j2Logger (Log4j2Logger.java:57) - No configuration found for the specified action: 'abstractPropertyAction' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
2017-04-04 15:41:03,230 WARN [http-bio-8080-exec-4] log4j2.Log4j2Logger (Log4j2Logger.java:57) - No configuration found for the specified action: 'abstractPropertyAction' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
convertFromString...
convertFromString...
convertFromString...
convertFromString...
[Member [id=null, name=12,
departments=[
Department [name=121, position=java.awt.Point[x=1,y=2], createTime=Tue Apr 04 15:41:01 CST 2017],
Department [name=122, position=java.awt.Point[x=2,y=3], createTime=Tue Apr 04 15:41:02 CST 2017]
]
],
Member [id=null, name=121,
departments=[
Department [name=1211, position=java.awt.Point[x=11,y=21], createTime=Tue Apr 04 15:41:11 CST 2017],
Department [name=1221, position=java.awt.Point[x=21,y=31], createTime=Tue Apr 04 15:41:12 CST 2017]
]
]
]