• 背景:

  本章节主要以复杂属性、集合属性类型转化为例,来学习这两种情况下怎么使用。

  • 复杂对象属性转换场景:

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>
com.dx.struts2app.ConversionAction.java
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 + "]";
} }
com.dx.struts2app.Department.java
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]
]
]
]
 
 

Struts(二十一):类型转换与复杂属性、集合属性配合使用的更多相关文章

  1. 二十一、JavaScript之访问对象属性

    一.代码如下 二.执行效果如下 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" cont ...

  2. Citrix 服务器虚拟化之二十一 桌面虚拟化之部署Provisioning Services

    Citrix 服务器虚拟化之二十一  桌面虚拟化之部署Provisioning Services Provisioning Services 是Citrix 出品的一系列虚拟化产品中最核心的一个组件, ...

  3. 云计算设计模式(二十一)——Sharding分片模式

    云计算设计模式(二十一)——Sharding分片模式 将一个数据存储到一组水平分区或碎片.存储和访问大量数据时,这个模式可以提高可扩展性. 背景和问题 由一个单一的服务器托管的数据存储区可能会受到以下 ...

  4. MongoDB学习笔记~对集合属性的操作

    回到目录 $unset清除元素 请注意在单个数组元素上使用$unset的结果可能与你设想的不一样.其结果只是将元素的值设置为null,而非删除整个元素.要想彻底删除某个数组元素,可以用$pull 和$ ...

  5. 无废话ExtJs 入门教程二十一[继承:Extend]

    无废话ExtJs 入门教程二十一[继承:Extend] extjs技术交流,欢迎加群(201926085) 在开发中,我们在使用视图组件时,经常要设置宽度,高度,标题等属性.而这些属性可以通过“继承” ...

  6. [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  7. WCF技术剖析之二十一:WCF基本异常处理模式[下篇]

    原文:WCF技术剖析之二十一:WCF基本异常处理模式[下篇] 从FaultContractAttribute的定义我们可以看出,该特性可以在同一个目标对象上面多次应用(AllowMultiple = ...

  8. WCF技术剖析之二十一:WCF基本异常处理模式[中篇]

    原文:WCF技术剖析之二十一:WCF基本异常处理模式[中篇] 通过WCF基本的异常处理模式[上篇], 我们知道了:在默认的情况下,服务端在执行某个服务操作时抛出的异常(在这里指非FaultExcept ...

  9. WCF技术剖析之二十一: WCF基本的异常处理模式[上篇]

    原文:WCF技术剖析之二十一: WCF基本的异常处理模式[上篇] 由于WCF采用.NET托管语言(C#和NET)作为其主要的编程语言,注定以了基于WCF的编程方式不可能很复杂.同时,WCF设计的一个目 ...

随机推荐

  1. [JLOI2012] 树

    Description 在这个问题中,给定一个值S和一棵树.在树的每个节点有一个正整数,问有多少条路径的节点总和达到S.路径中节点的深度必须是升序的.假设节点1是根节点,根的深度是0,它的儿子节点的深 ...

  2. Redis set集合结构及命令详解

    set 无序集合 集合的性质: 唯一性,无序性,确定性 注: 在string和link的命令中,可以通过range 来访问string中的某几个字符或某几个元素 但,因为集合的无序性,无法通过下标或范 ...

  3. Redis 中的数据类型及基本操作

    Redis 内置的数据类型有 5种:字符串String.哈希Hash.列表List.集合Set.有序集合ZSet 字符串类型 String 是 Redis 中最基本的类型,一个 key 对应着一个 v ...

  4. 【Python】 Selenium 模拟浏览器 寻路

    selenium 最开始我碰到SE,是上学期期末,我们那个商务小组做田野调查时发的问卷的事情.当时在问卷星上发了个问卷,但是当时我对另外几个组员的做法颇有微词,又恰好开始学一些软件知识了,就想恶作剧( ...

  5. Algorithm --> 字母重排

    字母重排 输入一个字典(用***结尾),然后再输入若干单词.没输入一个单词w,都需要在字典中找出所有可以用w的字幕重排后得到的单词,并按照字典序从小到大的顺序在一行中输出,如果不存在,输出“:(”.单 ...

  6. CAS简介和无锁队列的实现

    Q:CAS的实现 A:gcc提供了两个函数 bool __sync_bool_compare_and_swap (type *ptr, type oldval, type newval, ...)// ...

  7. 【最全】经典排序算法(C语言)

    算法复杂度比较: 算法分类 一.直接插入排序 一个插入排序是另一种简单排序,它的思路是:每次从未排好的序列中选出第一个元素插入到已排好的序列中. 它的算法步骤可以大致归纳如下: 从未排好的序列中拿出首 ...

  8. java排序算法(八):希尔排序(shell排序)

    java排序算法(八):希尔排序(shell排序) 希尔排序(缩小增量法)属于插入类排序,由shell提出,希尔排序对直接插入排序进行了简单的改进,它通过加大插入排序中元素之间的间隔,并在这些有间隔的 ...

  9. alpha冲刺第五天

    一.合照 二.项目燃尽图 三.项目进展 调整了一些界面的布局 细化了部分小功能的界面 注册界面和服务器响应了,但是在insert数据库方面出现了错误 四.明日规划 继续研究如何将注册的内容插入数据库 ...

  10. 听翁恺老师mooc笔记(13)--类型定义和联合

    typedef 虽然我们知道使用struct这个关键字定义一个结构类型,然后可以使用该结构类型定义变量.但是每次要使用的时候都需要带着struct这个关键字,那么如何摆脱这个关键字哪?C语言提供了一个 ...