自定义的类型转换器中怎样自定义错误消息?(待解答)
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
總結
以上是生活随笔為你收集整理的自定义的类型转换器中怎样自定义错误消息?(待解答)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 草字开头的成语有哪些?
- 下一篇: 开博客啦