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>
- Struts2_局部类型转换器
- Struts2_全局类型转换器
- Struts2_类型转换器
- 27.Struts2_自定义类型转换器(承接26)
- Struts2_自定义转换器
- 32-35. Struts2_类型转换错误消息的显示和定制&自定义类型转换器&类型转换器与复杂属性&集合协同使用
- 类型转换器
- 类型转换器
- 类型转换器
- 类型转换器
- 类型转换器
- 19.struts2_ 自定义类型转换
- mybatis 类型类型转换器
- Struts2类型转换器配置
- C#通用类型转换器
- Struts2的类型转换器
- Struts 2 类型转换器
- Struts2的类型转换器
- 每日英语阅读(三)
- chrome DevTools之黑箱大法(Blackbox )
- Anroid init.rc基本知识
- Barbells(三进制)
- AI:IPPR的数学表示-CNN方法
- Struts2_类型转换器
- 性能优化的一些经验与教训
- 每日英语阅读(四)
- ThinkPHP之__construct()和__initialize()
- x86汇编语言(一)
- Kotlin使用简要笔记
- ubantu的虚拟机的网络配置
- zookeeper入门(1)在单机上实现ZooKeeper伪机群/伪集群部署
- Spring第三天