自定义类型转换器的作用就是将struts无法识别的类型转换成自己所需要的.
比如输入:广东-东莞-虎门,对应的输出时能输出:广东省 东莞市 虎门(镇/区)
这里涉及到的知识点即是将String转换为任意的JavaBean类型.
一.实现代码
程序配置入口: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> --> <!-- 加载其他配置文件 --> <!-- <include file="config/upload-interceptor.xml"></include> --> <!-- 加载属性文件-国际化 --> <!-- <constant name="struts.custom.i18n.resources" value="message"></constant> --> <!-- 结果集 --> <!-- <include file="config/result_struts.xml"></include> --> <!-- 类型转换 --> <include file="config/type_struts.xml"></include> <!-- 文件下载 --> <!-- <include file="config/download_struts.xml"></include> --> </struts>
注:struts.xml是在项目运行的过程中被加载进入内存的,是项目配置的总入口,这里使用<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是具体负责类型转换时的配置,这里配置了对应的type.TypeSelfAction,以及成功后的跳转页面type_self_success.jsp和程序出错时
的跳转页面type_self.jsp,并将消息回显.
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="用户名" 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 是用户访问项目的入口,效果图如下所示:
其中使用POST请求方式是为了避免中文乱码等问题,使用struts自带的标签,如s:password,s:textfield等是为了简化开发,并且使错误消息方便回显.
TypeSelfAction.java
package type; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; /** * @ClassName: TypeSelfAction * @Description: TODO * @author: amosli * @email:amosli@infomorrow.com * @date Feb 13, 2014 11:24:40 PM */ public class TypeSelfAction extends ActionSupport { private static final long serialVersionUID = -4007160582981636173L; private String username;// 用户名 private String password;// 密码 private Double salary;// 薪水 private Date birthday;// 生日 private Address address;// 地址 public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Address getAddress() { return address; } public void setAddress(Address address) { System.out.println("setaddress:"+address.toString()); this.address = address; } public String execute() throws Exception { return SUCCESS; } }