日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

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

發(fā)布時間:2023/11/27 生活经验 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义的类型转换器中怎样自定义错误消息?(待解答) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.HTTP沒有“類型”的概念,每一項表單輸入只可能是一個字符串或一個字符串?dāng)?shù)組。從HTML表單到服務(wù)器端,必須把String轉(zhuǎn)換為特定的數(shù)據(jù)類型。

2.字符串和基本數(shù)據(jù)類型之間的類型轉(zhuǎn)換由Parameters攔截器自動完成。當(dāng)類型轉(zhuǎn)換出錯時由ConversionError攔截器負責(zé)添加與類型轉(zhuǎn)換有關(guān)的錯誤消息(前提是:Action類必須實現(xiàn)了ActionAware接口)

如果字段標(biāo)簽使用的不是simple主題,則輸入非法字段將導(dǎo)致有一條出錯消息:Invalid field value for field fieldName

覆蓋默認的出錯消息的方式:

? ?1)在對應(yīng)的Action類所在的包中新建一個ActionClassName.properties文件

? ?2)在屬性文件中添加鍵值對:invalid.fieldvalue.fieldName="自己定義的出錯消息"

? ?例如:

? ?index.jsp

??

? ?ConversionAction.properties

? ?

3.如果是自己定義的類型轉(zhuǎn)換器,該如何自定義顯示錯誤消息?

? 1)首先說說怎樣自定義類型轉(zhuǎn)換器

? ? ?1>自定義一個類型轉(zhuǎn)換器的類繼承StrutsTypeConverter,重寫

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

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

?

? ? 例如:以下index.jsp里的birth類型為Date,需要自定義類型轉(zhuǎn)換器來轉(zhuǎn)換。

? ? ?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             //獲取當(dāng)前WEB應(yīng)用的初始化參數(shù)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         //若沒有轉(zhuǎn)換成功,則返回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         //若轉(zhuǎn)換失敗,返回null
67         return null;
68     }
69 
70 }

?運行結(jié)果:

可看到,當(dāng)age輸入錯誤時,顯示的是自定義的出錯消息。當(dāng)birth格式輸入錯誤時,直接類型轉(zhuǎn)換器的程序里報錯了。

怎樣才能讓birth也能顯示自定義的出錯消息呢?

?

?

?

?

?

? ? ??

?

轉(zhuǎn)載于:https://www.cnblogs.com/TTTTT/p/6091053.html

總結(jié)

以上是生活随笔為你收集整理的自定义的类型转换器中怎样自定义错误消息?(待解答)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。