Struts2中的类型转换器学习

来源:互联网 发布:sql语句里case when 编辑:程序博客网 时间:2024/05/21 06:25

类型转换器知识笔记。

知识补充:

在form表单提交的数据,会被params拦截器注入到相应的Action。因此,非对象级的,Action内部的变量,将会与form表单中的input[name]中的name相对应。

Action从表单取过来的数据都是String类型。struts会自动将一些值转换,并将值与对应的Action的变量对应。如:

Action中的Private int age;Struts将String类型自动转换为int类型。
Private Date birthday; String数据也会按照格式转换为Date。(格式默认是yyyy-MM-dd;表单中的输入是2011-08-01)。

而对于不能转换的类型,或则需要修改的格式(yyyy年MM月dd日)怎么办?

那… 我们就自己写一个类型转换器。

类型转换器继承StrutsTypeConvertr类。

类型转换器有 全局的转换器 和 局部的转换器。

先贴上文件代码:

Struts.xml:

    <package name="convert" extends="struts-default" namespace="/">        <action class="com.stu.UserAction" name="login" method="login">            <result name="success">/success.jsp</result>        </action>    </package>

login.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>My JSP 'login.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>  <body>    <form action="login" method="post">        用户名:<input type="text" name="id" /><br/>        密码:<input type="text" name="pw" /><br/>        年龄:<input type="text" name="age" /><br/>        生日:<input type="text" name="birth" /><br/>        <input type="submit" value="提交"/>    </form> <br>  </body></html>

Action:

package com.stu;import java.util.Date;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {    private String id;    private String pw;    private int age;    private Date birth;    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getPw() {        return pw;    }    public void setPw(String pw) {        this.pw = pw;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public Date getBirth() {        return birth;    }    public void setBirth(Date birth) {        this.birth = birth;    }    public String login(){        System.out.println("In login: ");        System.out.println("id: "+id);        System.out.println("pw: "+pw);        System.out.println("age: "+age);        System.out.println("brith: "+birth.toString());        return SUCCESS;    }}

转换器:

package com.stu;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Map;import org.apache.struts2.util.StrutsTypeConverter;public class Convert extends StrutsTypeConverter {    @Override    public Object convertFromString(Map arg0, String[] arg1, Class arg2) {        // TODO Auto-generated method stub        if(arg1 ==null || arg1.length==0 ){            return null;        }        if(Date.class !=arg2){            return null;        }        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");        try {            Date date = sdf.parse(arg1[0]);            return date;        } catch (ParseException e) {            // TODO Auto-generated catch block            //e.printStackTrace();            System.out.println("Error happened!");        }        return null;    }    @Override    public String convertToString(Map arg0, Object arg1) {        // TODO Auto-generated method stub        return null;    }}

success.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>My JSP 'success.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>  <body>    This is my Success page.!!!! <br>  </body></html>

于此还要新建一个名为ActionName-coversion.properties文件放在Action所在的package中。(ActionName是你Action的名字)
如图:
这里写图片描述

在文件ActionName-coversion.properties中加一句:

birth=com.stu.Convert

页面输入:
这里写图片描述

控制台结果:

信息: Server startup in 2186 msIn login: id: 111pw: 123age: 123brith: Mon Feb 16 00:00:00 CST 1998

而在页面输入:
这里写图片描述
控制台结果是:

In login: id: 111333pw: 123age: 123brith: Mon Feb 16 00:00:00 CST 1998

所以,在类型转换器中是兼容默认的数据格式,新的格式也能使用!!!

以上是局部类型转换器,只对一个Action起作用,接下来配置全局的转换器对所有的Action都有作用。

全局的转换器只需要在src目录中新建xwork-conversion.properties

并在xwork-conversion.properties文件中加入代码:

java.util.Date=com.stu.Convert

页面输入:
这里写图片描述
控制台结果:

In login: id: 111333pw: 123age: 123brith: Mon Feb 16 00:00:00 CST 1998

而在页面输入:
这里写图片描述

控制台结果:

Error happened!In login: id: 111333pw: 123age: 123

所以,配置全局会出现问题在于,只能匹配新定义(1998年2月16日)的格式,默认的格式(1998-2-16)都不能使用。

0 0
原创粉丝点击