自定义的类型转换器中怎样自定义错误消息?(待解答)

1.HTTP没有“类型”的概念,每一项表单输入只可能是一个字符串或一个字符串数组。从HTML表单到服务器端,必须把String转换为特定的数据类型。

2.字符串和基本数据类型之间的类型转换由Parameters拦截器自动完成。当类型转换出错时由ConversionError拦截器负责添加与类型转换有关的错误消息(前提是:Action类必须实现了ActionAware接口)

如果字段标签使用的不是simple主题,则输入非法字段将导致有一条出错消息:Invalid field value for field fieldName

覆盖默认的出错消息的方式:

   1)在对应的Action类所在的包中新建一个ActionClassName.properties文件

   2)在属性文件中添加键值对:invalid.fieldvalue.fieldName="自己定义的出错消息"

   例如:

   index.jsp

  

   ConversionAction.properties

   

3.如果是自己定义的类型转换器,该如何自定义显示错误消息?

  1)首先说说怎样自定义类型转换器

     1>自定义一个类型转换器的类继承StrutsTypeConverter,重写

         public Object convertFromString(Map arg0, String[] arg1, Class arg2) 

        和public String convertToString(Map arg0, Object arg1)方法

 

    例如:以下index.jsp里的birth类型为Date,需要自定义类型转换器来转换。

     index.jsp

   

    Customer.java

 1 package com.tt.strust2.model;
 2 
 3 import java.util.Date;
 4 
 5 public class Customer {
 6     
 7     private int age;
 8     private Date birth;
 9  
10 
11     public int getAge() {
12         return age;
13     }
14 
15     public void setAge(int age) {
16         this.age = age;
17     }
18 
19     public Date getBirth() {
20         return birth;
21     }
22 
23     public void setBirth(Date birth) {
24         this.birth = birth;
25     }
26     
27     @Override
28     public String toString() {
29         return "Customer [age=" + age + ", birth=" + birth + "]";
30     }
31     
32 

      ConversionAction.java

 1 package com.tt.strust2.app;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 import com.opensymphony.xwork2.ModelDriven;
 5 import com.tt.strust2.model.Customer;
 6 
 7 public class ConversionAction extends ActionSupport implements ModelDriven<Customer>{
 8         
 9     /**
10      * 
11      */
12     private static final long serialVersionUID = 1L;
13 
14     public String execute(){
15         
16         System.out.println("model: "+ model);
17     
18         return "success";
19     }
20 
21     private Customer model; 
22     
23     @Override
24     public Customer getModel() {
25         
26         model = new Customer();
27         return model;
28     }
29 
30 }

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 3   <display-name>20161113-Struts2-5</display-name>
 4   <context-param>
 5     <param-name>pattern</param-name>
 6     <param-value>yyyy-MM-dd hh:mm:ss</param-value>
 7   </context-param>
 8   <filter>
 9     <filter-name>struts2</filter-name>
10     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
11   </filter>
12   <filter-mapping>
13     <filter-name>struts2</filter-name>
14     <url-pattern>/*</url-pattern>
15   </filter-mapping>
16 </web-app>

DateConverter.java

 1 package com.tt.strust2.app.converters;
 2 
 3 import java.util.Date;
 4 import java.text.DateFormat;
 5 import java.text.ParseException;
 6 import java.text.SimpleDateFormat;
 7 import java.util.Map;
 8 
 9 import javax.servlet.ServletContext;
10 
11 import org.apache.struts2.ServletActionContext;
12 import org.apache.struts2.util.StrutsTypeConverter;
13 
14 public class DateConverter extends StrutsTypeConverter {
15 
16     private DateFormat dateFormat;
17     
18     public DateConverter() {
19         
20         System.out.println("DateConverter's constructor...");
21         
22         
23     }
24     
25     public DateFormat getDateFormat(){
26         
27         if(dateFormat == null){
28             //获取当前WEB应用的初始化参数pattern
29             ServletContext servletContext = ServletActionContext.getServletContext();
30             
31             String pattern = servletContext.getInitParameter("pattern");
32             
33             dateFormat = new SimpleDateFormat(pattern);
34         }
35         return dateFormat;
36     }
37     
38     @Override
39     public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
40         
41         System.out.println("convertFromString...");
42         if(arg2 == Date.class){
43             if(arg1 != null && arg1.length >0){
44                 String value = arg1[0];
45                 try {
46                     return getDateFormat().parseObject(value);
47                 } catch (ParseException e) {
48                     // TODO Auto-generated catch block
49                     e.printStackTrace();
50                 }
51             }
52         }
53         //若没有转换成功,则返回arg1。
54         return arg1;
55     }
56 
57     @Override
58     public String convertToString(Map arg0, Object arg1) {
59         
60         System.out.println("convertToString...");
61         if(arg1 instanceof Date){
62             
63             Date date = (Date)arg1;
64             return getDateFormat().format(date);
65         }
66         //若转换失败,返回null
67         return null;
68     }
69 
70 }

 运行结果:

可看到,当age输入错误时,显示的是自定义的出错消息。当birth格式输入错误时,直接类型转换器的程序里报错了。

怎样才能让birth也能显示自定义的出错消息呢?

 

 

 

 

 

      

 

原文地址:https://www.cnblogs.com/TTTTT/p/6091053.html