自定义类型转换器

这里只讲日期类型转换器的自定义

Struts2中有一些自定义类型转化器位置:

但是此处的日期类型转换器只是将yyyy-MM-dd日期格式转化成为Tue Jun 06 00:00:00 CST 2017

而对于产品来说可能涉及到yyyy/MM/dd和yyyyMMdd等,接下来就看例子吧!

首先定义表单界面:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="/struts-tags" prefix="s"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>My First Struts2 Project</title>
 9 </head>
10 <body>
11 
12 <!-- struts2的标签能够实现数据的回显,变返回那条数据出现了错误 -->
13 <s:form action="system/login1_login.action" method="post">
14     <s:textfield name="birthday" label="生日"/>
15     <s:textfield name="age" label="年龄"/>
16     <s:submit value="登录"></s:submit>
17 </s:form>
18 <!-- 
19 <form action="system/login1_login.action" method="post">
20         生日<input type="text" name="birthday"><br>
21         年龄<input type="text" name="age"><br>
22         <input type="submit" value="登录">
23 </form> -->
24 </body>
25 </html>

定义Action方法,这里用的是属性驱动进行的参数的请求和接收:

 1 package com.bjyinfu.struts.actions;
 2 
 3 import java.util.Date;
 4 
 5 import com.opensymphony.xwork2.ActionSupport;
 6 
 7 //当Action方法继承这个ActionSupport类的时候,若输入的值不符合规定会自动跳到视图结果为input的视图上
 8 //即在struts.xml中在定义一个<result name="input">/输入视图.jsp</result>
 9 //这样做是为了当数据出现失误的时候能够进行数据回显
10 
11 public class LoginAction1 extends ActionSupport{
12 
13     private Date birthday;
14     private int age;
15     
16 
17     public Date getBirthday() {
18         return birthday;
19     }
20 
21     public void setBirthday(Date birthday) {
22         this.birthday = birthday;
23     }
24 
25     public int getAge() {
26         return age;
27     }
28 
29     public void setAge(int age) {
30         this.age = age;
31     }
32 
33     public String login(){
34         System.out.println("执行了登录方法");
35         return "success";
36     }
37 }

将Action注册到Struts2中:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6 
 7     <package name="login" namespace="/system" extends="struts-default">
 8         <action name="login1_*" class="com.bjyinfu.struts.actions.LoginAction1" method="login">
 9             <result name="success">/welcome04.jsp</result>
10             <result name="input">/login1.jsp</result>
11         </action>    
12     </package>
13 </struts>

自定义日期类型转换器:

 1 package com.bjyinfu.typeConvertr;
 2 
 3 import java.text.ParseException;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Date;
 6 import java.util.regex.Pattern;
 7 
 8 import com.opensymphony.xwork2.ActionContext;
 9 import com.opensymphony.xwork2.conversion.TypeConversionException;
10 import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
11 
12 public class MyTypeConverter extends DefaultTypeConverter {
13 
14     //在局部类型转换定义完后要进行注册,在Action类的同包下创建后缀为properties的文件
15     //文件命名格式:ActionClassName-conversion.properties
16     //ActionClassName是Action方法的类名,conversion.properties为固定写法
17 //    value:将要被转化的数据
18 //    toType:被转换成的目标类型
19 //    页面--->服务器端  String[]-->Date,为了兼顾复选框
20 //    服务器端-->页面  Date-->String
21     @Override
22     public Object convertValue(Object value, Class toType) {
23         
24         SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
25         try {
26             if(toType == Date.class){//实现从页面到服务器端的String-->Date
27                 String dateStr=((String[])value)[0];
28                 sdf=getSimpleDateFormat(dateStr);
29                 //把数据放到session中然后会显得时候再从session中去取
30                 ActionContext.getContext().getSession().put("sdf", sdf);
31                 /*if(!Pattern.matches("^\d{4}/\d{2}/\d{2}", dateStr)){
32                     throw new TypeConversionException();
33                 }*/
34                 
35                 return sdf.parse(dateStr);
36             }else {//实现服务端到页面的转换 Date-->String
37                 Date date=(Date)value;
38                 sdf=(SimpleDateFormat)ActionContext.getContext().getSession().get("sdf");
39                 return sdf.format(date);
40             }
41             
42         } catch (ParseException e) {
43             
44             e.printStackTrace();
45         }
46         
47         return super.convertValue(value, toType);
48     }
49 
50     private SimpleDateFormat getSimpleDateFormat(String dateStr) {
51         SimpleDateFormat sdf=null;
52         if(Pattern.matches("^\d{4}/\d{2}/\d{2}$", dateStr)){
53             sdf=new SimpleDateFormat("yyyy/MM/dd");
54         }else if(Pattern.matches("^\d{4}-\d{2}-\d{2}$", dateStr)){
55             sdf=new SimpleDateFormat("yyyy-MM-dd");
56         }else if(Pattern.matches("^\d{4}\d{2}\d{2}$", dateStr)){
57             sdf=new SimpleDateFormat("yyyyMMdd");
58         }else{
59             throw new TypeConversionException();
60         }
61         return sdf;
62     }
63 
64     
65 }

将自定义的类型转换器注册到struts2中:(在Action类的同包下创建后缀为properties的文件,文件名定义规范:ActionClassName-conversion.properties;其中ActionClassName对应的是Action的方法名,而-conversion.properties是固定的,将需要被转换的属性进行定义,值为自定义转换器的权限类名)

1 birthday = com.bjyinfu.typeConvertr.MyTypeConverter

Action执行完毕后展现的视图,即接收数据的页面:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3     <%@taglib uri="/struts-tags" prefix="s" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>My First Struts2 Project</title>
 9 </head>
10 <body>
11 <s:debug></s:debug><br>
12     birthday=<s:property value="birthday"/><br>
13     age=<s:property value="age"/><br>
14     <!-- 下面显示的日期是Tue Jun 06 00:00:00 CST 2017这种格式,上面显示的日期是输入的原值 -->
15     birthday=${birthday }<br>
16     age=${age }<br>
17 </body>
18 </html>

如果输入值不符合情况,会将数据进行回显,并且有跟随的回显错误内容,下面将回显内容进行自定义:

(在Action类的同包下创建后缀为properties的文件,文件名定义规范:ActionClassName.properties;

其中ActionClassName对应的是Action的方法名;用invalid.fieldvalue.带出错误属性,值为自定义)

1 invalid.fieldvalue.age=u5E74u9F84u5E94u8BE5u4E3Au6574u6570
2 invalid.fieldvalue.birthday=u65E5u671Fu683Cu5F0Fu4E0Du6B63u786E

 注:一定要将这两个properties文件放到与对应的Action方法类相同包下,否则会找不到文件;

原文地址:https://www.cnblogs.com/lubolin/p/7264463.html