Struts2_类型转换器

来源:互联网 发布:interface php 编辑:程序博客网 时间:2024/06/18 10:26

一知半解的人,多不谦虚;见多识广有本领的人,一定谦虚。 —— 谢觉哉

  Struts在接受表单传递过来的参数时,会根据表单元素名称自动将值赋给action中相应的属性。那么,属性的类型显然有很多种,,比如:int,double以及Date,但是表单提交过来的数据通通是字符串,意味着Struts2默认给我们提供了各种类型转换。
  在实际开发中,往往还需要自定义类型转换,来完成实际的功能或者补充默认所不能实现的需求。

1.局部类型转换

定义一个类继承“DefaultTypeConverter”,然后重写“public Object convertValue(Object value,Class toType)”

注意:

1.value是字符串数组。

2.如果要转换的类型是Date,需手动导入”java.util.Date”包。

转换器代码:

package com.converter;import java.util.Date;import java.text.ParseException;import java.text.SimpleDateFormat;import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;public class DateConverter extends DefaultTypeConverter {    @Override    public Object convertValue(Object value, Class toType) {        String str=((String[])value)[0];        System.out.println("value"+((String[])value)[0]);        System.out.println("toType"+toType);        System.out.println(Date.class);        if(toType==Date.class){            try {                SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");                return sdf.parse(str);            } catch (ParseException e) {                    e.printStackTrace();            }        }        return null;    }}

定义好转换器后,需要在Action同目录下创建与该Action对应的配置文件“Action类名-conversion.properties”。

LoginAction-conversion.properties代码:

birthday=com.converter.DateConverter

通过以上演示,只能转换指定的Action中的数据类型,通常需要定义全局的类型转换器。

2.全局类型转换

全局类型转换和局部类型转换仅仅就是配置文件不同而已。全局类型转换的配置文件名称应该为”xwork-conversion.properties”,而且放在src的根目录下。

xwork-conversion.properties代码:

java.util.Date=com.converter.DateConverter

3.转换器中的异常消息的处理

在处理转换的异常时,抓住异常要抛:TypeConversionException。
注意正则表达式的写法。

1.login.jsp代码:

<%@ page language="java" pageEncoding="UTF-8" %><%@ page contentType="text/html; charset=UTF-8" %><!DOCTYPE html ><html><head><meta  charset="UTF-8"><title>title</title></head><body>${fieldErrors.birthday[0] }    <form action="${pageContext.request.contextPath }/demo">        生日:<input type="text" name="birthday"><br>        年龄:<input type="text" name="age"><br>        <input type="submit" value="提交">    </form></body></html>

2.LoginAction代码:

package com.action;import java.util.Date;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport{    private Integer age;    private Date birthday;    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    public Date getBirthday() {        return birthday;    }    public void setBirthday(Date birthday) {        this.birthday = birthday;    }    public String execute(){        System.out.println(birthday);        System.out.println(age);        return "success";    }}

3.DateConverter代码:

package com.converter;import java.util.Date;import java.text.ParseException;import java.text.SimpleDateFormat;import com.opensymphony.xwork2.conversion.TypeConversionException;import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;public class DateConverter extends DefaultTypeConverter {    @Override    public Object convertValue(Object value, Class toType) {        String str=((String[])value)[0];        System.out.println("value"+((String[])value)[0]);        System.out.println("toType"+toType);        System.out.println(Date.class);        if(toType==Date.class){            try {                System.out.println("123123");                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");                if(!str.matches("^\\d{4}-\\d{2}-\\d{2}$")){                    System.out.println("123");                    throw new TypeConversionException();                }                System.out.println("123");                return sdf.parse(str);            }catch (ParseException e) {                    e.printStackTrace();            }        }        return null;    }}

4.xwork-conversion.properties代码:

java.util.Date=com.converter.DateConverter

5.struts.xml代码:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"    "http://struts.apache.org/dtds/struts-2.5.dtd">    <struts>        <package name="root" namespace="/" extends="struts-default">            <action name="demo" class="com.action.LoginAction" >                <result>/welcome.jsp</result>                <result name="input">/login.jsp</result>            </action>        </package>    </struts>

这里写图片描述