Struts2中的类型转换器学习

类型转换器知识笔记。

知识补充:

在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 ms
In login: 
id: 111
pw: 123
age: 123
brith: Mon Feb 16 00:00:00 CST 1998

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

In login: 
id: 111333
pw: 123
age: 123
brith: 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: 111333
pw: 123
age: 123
brith: Mon Feb 16 00:00:00 CST 1998

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

控制台结果:

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值