Struts2.2 Type Conversion

 

Struts2 的转换器:

    如果ActionPO 类(PO :就是Action 没有继承或实现任何父类或接口),即使转换过程发生了错误,Struts 还会继续执行,跳转配置页面

    只要实现了ValidationAware 接口,转换过程出错后才会返回配置的输入页面,需要实现哪些接口方法?这里只需要继承ActionSupport 这个类,如果必要只要实现Validate 这个方法即可,因为此类已经实现了ValidationAware 接口

 

    实现TypeConvert 接口:接口参数较多,需要手动判断转换类型;

    继承DefaultTypeConvert :需要手动判断转换类型;

    继承StrutsTypeConvert :针对不同转换提供转换接口;

 

局部注册:

    注册文件

        1、名称: Action类名-conversion.properties

        2、以key=value的方式指定属性的转换类,fullName=com.iflytek.convert. MyPointConverter,注意这里的fullName是需要转换的类的全名称,加包名,value是对转换类进行处理的Action

        3、 将此文件放置在与Action同目录的包路径中

全局注册:

    注册文件

        1、名称: xwork-conversion.properties

        2、以key-value的方式指定属性的转换类,例;

            org.lxh.convert.Point = com.iflytek.convert.MyPointConverter

            key 不能是属性 而是一个全限定名称的类 可以放置多个配置值对

        3 将此文件放置 web-inf/classes 目录下

 

Ok ,直接上代码:

TestAction.java:

package com.iflytek.action;

import java.awt.Point;
import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author xudongwang 2011-10-31
 * 
 */
public class TestAction extends ActionSupport {
//http://localhost:8080/Struts2.2_TypeConversion/test?name=2&age=1&date=1988-09-09&p=3,2
	private String name;
	private int age;
	private Date date;
	private Point p;

	@Override
	public String execute() throws Exception {
		return super.execute();
	}

	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 Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Point getP() {
		return p;
	}

	public void setP(Point p) {
		this.p = p;
	}

}

 

MyPointConverter.java( 继承StrutsTypeConverter)

package com.iflytek.conversion;

import java.awt.Point;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

/**
 * @author xudongwang 2011-10-31
 * 
 */
public class MyPointConverter extends StrutsTypeConverter {

	@Override
	public Object convertFromString(Map context, String[] values, Class toClass) {

		Point p = new Point();
		String[] strs = (String[]) values;
		String[] xy = strs[0].split(",");
		p.x = Integer.parseInt(xy[0]);
		p.y = Integer.parseInt(xy[1]);
		return p;
	}

	@Override
	public String convertToString(Map context, Object o) {
		return o.toString();
	}

}

 

or

 

MyPointConverter02.java( 继承DefaultTypeConverter)

package com.iflytek.conversion;

import java.awt.Point;
import java.util.Map;

import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;

/**
 * @author xdwang 2012-6-25
 * 
 */
public class MyPointConverter02 extends DefaultTypeConverter {

	public Object convertValue(Map<String, Object> context, Object value,
			Class toType) {
		if (toType == Point.class) {
			Point point = new Point();
			String[] temps = (String[]) value;
			String[] params = temps[0].split(",");
			int x = Integer.parseInt(params[0]);
			int y = Integer.parseInt(params[1]);

			point.x = x;
			point.y = y;
			return point;
		} else if (String.class == toType) {
			Point point = (Point) value;
			return point.getX() + "," + point.getY();
		}

		return null;
	}

}

 

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>
	<constant name="struts.devMode" value="true" />
	<constant name="struts.ui.theme" value="simple" />
	<package name="tags" extends="struts-default">
		<action name="test" class="com.iflytek.action.TestAction">
			<result>/test.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">
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<!-- 过滤器 -->
	<filter>
		<filter-name>encoding</filter-name>
		<filter-class>com.iflytek.filter.EncodingFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>encoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<filter>
		<filter-name>struts2</filter-name>
		<!-- 新的 -->
		<!--
			<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
		-->
		<!-- 旧的 -->
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

 

xwork-conversion.properties (放在src 下)

java.awt.Point=com.iflytek.conversion.MyPointConverter02

 

test.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'test.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>
		name:
		<s:property value="name" />
		<br>
		age:
		<s:property value="age" />
		<br>
		date:
		<s:property value="date" />
		<br>
		<s:date name="date" format="yyyy/MM/dd HH:mm:ss"/><br/>
		<s:property value="p"/><br/>
	</body>
</html>

 

启动服务后,直接在地址栏输入:

http://localhost:8080/Struts2.2_TypeConversion/test?name=2&age=1&date=1988-09-09&p=3,2

页面显示:

name: 2
age: 1
date: 88-9-9
1988/09/09 00:00:00
3.0,2.0

 

 

 

你可能感兴趣的:(转换器,struts2.2)