类型转换

struts2的自定义类型转换机制为复杂类型的输入输出处理提供了便捷.struts2已经为我们提供了几乎所有的primitive类型以及常用类型(如Date)的类型转换器,我们也可以为我们自定义类添加自定义类型转化器.

下面来看一下自定义类型转换

先定义实体类

复制代码
package entity;

import java.util.Date;

public class User {
private String name;//姓名
private Integer age;//年龄
private Date birthday;//出生日期
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public Date getBirthday() {
return birthday;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}


}
复制代码
接下来我们创建一个AddUserAction

复制代码
package action;

 

import com.opensymphony.xwork2.ActionSupport;

import entity.User;

 

public class AddUserAction extends ActionSupport {
//调用实体类
private User user;
public String execute() throws Exception{
//在此我们就用出生日期来实现一下
System.out.println(user.getBirthday());
return SUCCESS;

}


public User getUser() {
return user;
}


public void setUser(User user) {
this.user = user;
}

}
复制代码

接下来我们创建出自己的类型转换器

复制代码
package converter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

import com.opensymphony.xwork2.conversion.TypeConversionException;

public class Dateconverter extends StrutsTypeConverter{
//将前台获取到的值转换成Date类型
private final DateFormat[] dfs={
new SimpleDateFormat("yyyy/MM/dd"),
new SimpleDateFormat("yyyy年MM月dd日"),
new SimpleDateFormat("yyyy-MM-dd"),
new SimpleDateFormat("yyyy.MM.dd"),
new SimpleDateFormat("yyyyMMdd"),
new SimpleDateFormat("MM/dd/yyyy"),
};


/**
* String类转换成特定的类
* 用于前台传值到后台
*/
@Override //context里的map对象 前台创来的值
public Object convertFromString(Map context, String[] values, Class toType) {
String value=(String)values[0];
for (int i = 0; i < dfs.length; i++) {
try {
return dfs[i].parse(value);
} catch (ParseException e) {
// TODO Auto-generated catch block
continue;
}
}
throw new TypeConversionException();
}
/**
* 前台取值
*
*/
@Override
public String convertToString(Map context, Object obj) {


return new SimpleDateFormat("yyyy-MM-dd").format(obj);
}

}
复制代码
创建AddUserAction-conversion.properties关联关系

 

当我们用到user.birthday属性时会自动调用converter.Dateconverter.java类型转换器

接下来写配置adduser.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>
<constant name="struts.devMode" value="true"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="AddUser" class="action.AddUserAction">
<result name="success">/success.jsp</result>
</action>
</package>

</struts>
复制代码

接下来配置struts.xml文件并引用adduser.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>
<constant name="struts.ui.theme" value="simple"></constant>
<constant name="struts.devMode" value="false"></constant>
<constant name="struts.enable.DynamicMethodInvoaction" value="true"></constant>
<package name="main" namespace="/" extends="struts-default">

</package>
<include file="action/adduser.xml"></include>
</struts>
复制代码

接下来编写输入与展示页面

复制代码
<%@ 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 'index.jsp' starting page</title>

</head>

<body>
<s:fielderror/>
<form action="AddUser" method="post">
姓名:<input type="text" name="user.name"/><br/>
年龄:<input type="text" name="user.age"/><br/>
出生日期:<input type="text" name="user.birthday"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
复制代码

展示页面

复制代码
<%@ 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 'success.jsp' starting page</title>

</head>

<body>
<s:date name="user.birthday" format="yyyy-MM-dd"/>
</body>
</html>
复制代码

效果展示

 

 

补充

分类

struts2自定义类型转换从大的方面来讲分两种:

u 局部类型转换

u 全局类型转换

局部类型转换又分为三种:

² 普通实体bean的自定义类型转换

² 基于领域模型的自定义类型转换

² 基于模型驱动的自定义类型转换

无论是全局类型转换还是局部类型转换,转换器与Action之间是用properties文件来关联的,properties文件指明了转换规则

全局类型转换规则:

在classpath下新建文件xwork-conversion.properties(固定名称)

其内容为:目标转换对象=转换器类(包名+类名)

局部类型转换规则:

在对应的Action的同级目录下新建Action名-conversion.properties(一定要与Action类名对应)

其内容为: 目标转换对象=转换器类(包名+类名)

在局部类型转换中又存在一种特殊情况

基于领域模型的自定义类型转换

它不但要在对应的Action的同级目录下新建Action名-conversion.properties(一定要与Action类名对应)文件,还需在引用模型同级目录下建properties文件取名规则为引用名- conversion.properties

posted @ 2016-10-14 17:14  吴玄坤  阅读(168)  评论(0编辑  收藏  举报