struts的数据类型转换器,把表单提交的参数映射到bean对象对应属性的数据类型,但对于一些特殊数据类型还无法转换,比如坐标,解决的方法如下:
第一步:对于无法转化的数据类型,单独设计一个继承了ognl.DefaultTypeConverter类的子类,在这个子类中覆盖它的“public Object convertValue(Map context, Object value, Class toType){ } ”方法,把具体转化该类型的操作写在该方法中,注意,有两个转换动作,一个是把表单接收到的参数转化成bean对象对应属性的数据类型,另一个是把bean对象对应属性的数据类型转换成String类型,用于返回给显示页面。
第二步:为该类型转换类配置信息,让struts知道,当表单提交该属性名称的参数时,自动调用已设置好的类型转化类进行转换动作。需要在当前action类的根目录下新建一个文件,文件名是有固定格式的:“action的名称-conversion.properties”,在该文件中配置如下信息:“需要转换的属性名称=转换类的class路径”。
这样,在表单提交该属性名参数的时候,struts会自动调用自定义设置的数据转换类对其进行转化成对应属性的数据类型。
简单实例(处理坐标的数据类型转换,struts2.2.1版本下):
①坐标实体bean
package com.hzp.bean;
public class Point {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
②当前表单业务处理(action)类
package com.hzp.action;
import com.hzp.bean.Point;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private Point point;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public String execute() throws Exception {
return "success";
}
}
③自定义point(坐标)类型转换类
package com.hzp.converter;
import java.util.Map;
import com.hzp.bean.Point;
import ognl.DefaultTypeConverter;
public class ConvertPoint extends DefaultTypeConverter {
public Object convertValue(Map context, Object value, Class toType) {
//从表单转换成对应属性类型
if (Point.class == toType) {
String[] str = (String[]) value;
String p = str[0];
String[] strPoint = p.split(",");
Point point = new Point();
point.setX(Integer.parseInt(strPoint[0]));
point.setY(Integer.parseInt(strPoint[1]));
return point;
}
//从对应属性类型转换成字符串给显示页面
else if(String.class == toType) {
Point point = (Point) value;
String x = Integer.toString(point.getX());
String y = Integer.toString(point.getY());
String result = "(" + x + "," + y + ")";
return result;
} else {
return null;
}
}
}
④配置文件LoginAction-conversion.properties的设置(LoginAction是我当前表单提交到的业务处理类名称)
point=com.hzp.converter.ConvertPoint
⑤struts.xml配置文件内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org//dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="login" class="com.hzp.action.LoginAction">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
⑥web.xml配置文件内容
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
分享到:
相关推荐
struts2学习笔记3数据类型转换struts2学习笔记3数据类型转换struts2学习笔记3数据类型转换struts2学习笔记3数据类型转换struts2学习笔记3数据类型转换struts2学习笔记3数据类型转换struts2学习笔记3数据类型转换
struts2.1数据类型转换2struts2.1数据类型转换2struts2.1数据类型转换2struts2.1数据类型转换2struts2.1数据类型转换2struts2.1数据类型转换2struts2.1数据类型转换2struts2.1数据类型转换2
适合struts2入门使用,主要是struts拦截到的数据类型转换的问题
struts2 类型转换 数据校验
struts2数据类型转换
NULL 博文链接:https://hanazawakana.iteye.com/blog/1681564
SSH之Struts2数据类型转换国际化及文件上传下载
自己用struts2写的一个简单的局部数据类型转换实例,适合初学者
软件硕士张龙老师,对Struts2类型转换器的说明源代码,以及批量插入数据的源代码,不改动任何代码的情况下进行类型转换。
使用struts2进行数据类型转换(附图)非常详细
自己初学struts2做的类型转换方式,有局部类型转换,全局类型转换,和大量数据类型转换,供初学者学习,共同讨论
使用struts2提供的转化器,实现Date类型的数据转换
对于基本数据类型 strus2可以自动的将其转换成所需要的类型,但是我们自己定义的引用数据类型无法转换,本文详细介绍了strus2的自定义类型转换器
服务器可以接收到的来自用户的数据只能是字符串或者是字符串数组,而在服务器上的对象中 ,这些数据往往有多种不同的类型,如日期(Date) 整数(int) 浮点数(float)或者是自定义类型 。所以我们要通过某种方式将特定...
基于struts2的类型转换,对于熟悉struts2的流程也许会有些帮助
struts1框架中没有自动转换器,每次都得自己部署,所以自己写个,源码分享给大家。
主要介绍了关于Struts2类型转换的相关资料,文中主要介绍了Struts2的类型转换器和自定义类型转换器的实现,有需要的朋友可以参考借鉴,下面来一起看看吧。
struts2 将要进行自定义类型转换的点数据从1个增加到3个 struts3 进行全局的类型转换 struts4 使用Struts2中内部类中的方法进行自定义类型转换,用到的类StrutsTypeConverter struts5 增加集合类型,使用范型 ...
1、struts2概述 Struts2 软件下载 Struts2 比较重要的类 操作步骤及框架配置 struts运行机制 2、简单的struts2程序 ...9 struts2类型转换 ........ 10struts2标签库 ........ 11、访问数据库 ........
新手必备,看完绝对思路清晰。醍醐灌顶啊! 一. Struts2介绍 1.... 数据类型转换器 11. 实现文件上传 12. Struts2的拦截器 13. 拒绝表单重复提交 四. OGNL表达式 五. Struts2中常用标签