?!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
自定义类型{换器的作用就是将struts无法识别的类型{换成自己所需要的.
比如输入:q东-东莞-虎门,对应的输出时能输?q东?东莞?虎门(??
q里涉及到的知识点即是将String转换ZQ意的JavaBeancd.
一.实现代码
E序配置入口: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>
<!--<include file="config/upload.xml"></include> -->
<!-- 加蝲其他配置文g -->
<!-- <include file="config/upload-interceptor.xml"></include> -->
<!-- 加蝲属性文Ӟ国际?-->
<!-- <constant name="struts.custom.i18n.resources" value="message"></constant> --> <!-- l果?-->
<!-- <include file="config/result_struts.xml"></include> -->
<!-- cd转换 -->
<include file="config/type_struts.xml"></include>
<!-- 文g下蝲 -->
<!-- <include file="config/download_struts.xml"></include> -->
</struts>
复制代码
?struts.xml是在目q行的过E中被加载进入内存的,是项目配|的d?q里使用<include>属性加载外部的type_struts.xml
type_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="type" extends="struts-default">
<action name="TypeAction" class="type.TypeAction" method="execute">
<result name="success" type="dispatcher">
/WEB-INF/type_success.jsp
</result>
<result name="input" type="dispatcher">
type.jsp
</result>
</action>
</package> -->
<package name="type" extends="struts-default">
<action name="TypeSelfAction" class="type.TypeSelfAction" method="execute">
<result name="success" type="dispatcher">
/WEB-INF/type_self_success.jsp
</result>
<result name="input" type="dispatcher">
/type_self.jsp
</result>
</action>
</package>
</struts>
复制代码
?type_struts.xml是具体负责类型{换时的配|?q里配置了对应的type.TypeSelfAction,以及成功后的跌{面type_self_success.jsp和程序出错时
的蟩转页面type_self.jsp,q将消息回显.
type_self.jsp
复制代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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>
<s:form action="TypeSelfAction" type="POST">
<s:textfield label="用户?quot; name="username" />
<s:password label="密码" name="password" />
<s:textfield label="薪水" name="salary" />
<s:textfield label="生日" name="birthday"/>
<s:textfield label="地址" name="address"/>
<s:submit value="提交" name="submit"/>
<s:reset value="重置"/>
</s:form>
</body>
</html>
复制代码
?type_self.jsp 是用戯问项目的入口,效果囑֦下所C?
其中使用POSTh方式是ؓ了避免中文ؕ码等问题,使用struts自带的标{?如s:password,s:textfield{是Z化开?q且佉K误消息方便回?
TypeSelfAction.java
View Code
?TypeSelfAction是一个与配置文gtype_struts.xml相对应的JavaBean文g.
Address.java
View Code
?Address是关于地址的JavaBean,其分为省??区域三.
Z实现自定义类型{换还必须实现StrutsTypeConverter的扩?q里要手动写两个ҎJavaBean转ؓString和将String转ؓJavaBean.
TypeAddressConverter.java
复制代码
package type;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
/**
* @ClassName: TypeAddressConverter
* @Description: 自定义类型{换器,l承自StrutsTypeConverter,可以String转换成Q意javaBean对象
* @author: amosli
* @email:amosli@infomorrow.com
* @date Feb 13, 2014 11:42:42 PM
*/
public class TypeAddressConverter extends StrutsTypeConverter {
public TypeAddressConverter() {
System.out.println("constructor!");
}
//String转ؓ对象q回
public Object convertFromString(Map context, String[] values, Class toClass) {
System.out.println("convertFromString");
// System.out.println("context:"+context);
// System.out.println("class:"+toClass);
// System.out.println("values:"+Arrays.toString(values));
if (Address.class == toClass) {// 判断cd是否为将要{成的JavaBean
String[] split = values[0].toString().split("-");//面中的字符?下标?表示W一个字W串,q里如果有多个可以依此类?
String province = split[0];// 省䆾
String city = split[1];// ?/span>
String area = split[2];// ?/span>
Address address = new Address(province, city, area);
return address;
}
return null;
}
//对象{为Stringq回
public String convertToString(Map context, Object o) {
System.out.println("convertToString");
if (o instanceof Address) {// 判断对象cd是否为Adress
Address adr = (Address) o;
return adr.toString();
}
return null;
}
}
复制代码
?q里是自定义cd转换的核心代?卛_何将一个JavaBean转ؓString和将String转ؓJavaBean.
Z调用上面的方法还必须加一个配|?
xwork-conversion.properties
#JavaBean=self defined typeconverter
type.Address=type.TypeAddressConverter