future2012lg

博客园 首页 新随笔 联系 订阅 管理

一个简单的小类型转换方便以后参考

1 先建2个JSP网页

input.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.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>
   <h3><font color="red">使用逗号将点的两个坐标分割开</font></h3>
   <s:form action="pointConverter" method="post"><!-- 此处如果不写method方法,则Struts2将其默认为post方法,和以前的表单提交方法有些区别 -->
     <s:textfield name="point" label="point"></s:textfield>
     <s:textfield name="age" label="age"></s:textfield>
     <s:textfield name="username" label="username"></s:textfield>
     <s:textfield name="birthday" label="birthday"></s:textfield>
    
     <s:submit label="submit"></s:submit>
    </s:form>
  
  </body>
</html>

out.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'output.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>
    point:<s:property value="point"/><br><!-- 相当于调用了PointAction中的getPoint()方法 -->
    age:<s:property value="age"/><br/>
    username:<s:property value="username"/><br/>
    birthday:<s:property value="birthday"/>

  </body>
</html>

2 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.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
 <filter-name>struts2</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>
  </web-app>

3 创建一个Point

package com.zlc.entity;

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;
 }
}

4 创建Action

package com.zlc.action;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;
import com.zlc.entity.Point;

public class PointAction extends ActionSupport {
 private Point point;;// 点的坐标point(x,y)
 private int age;
 private Date birthday;

 public Point getPoint() {
  return point;
 }

 public void setPoint(Point point) {
  this.point = point;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public Date getBirthday() {
  return birthday;
 }

 public void setBirthday(Date birthday) {
  this.birthday = birthday;
 }

 @Override
 public String execute() throws Exception {

   return "success";

 }

}

同时在此类下添加 PointAction-conversion.properties文件

point=com.zlc.converter.PointConverter

 配置类型转换文件(包括两种)

  1. 局部类型转换

         别忘了配置PointAction-conversion.properties文件(放在com.zlc.action下,即必须和PointAction在同一目录下)

        (PointAction-conversion.properties是局部类型转换配置文件,指只对当前的PointAction起作用)

         此配置如下:

         point=com.zlc.converter.PointConverter

         这句话的意思是对PointAction中的属性point进行类型转换是通过com.zlc.converter.PointConverter转换器类进行的

    2.   全局类型转换

         说明: 这种情况是针对有多个Action,并且每个Action当中都有相同的属性point(坐标)需要转换的情况下配置的

         使用全局类型转换省去了对每个Action都要进行转换的麻烦(即配置了多个ActionName-conversion.properties).

         

          全局类型转换配置的文件名字:xwork-conversion.properties是固定写法,要放在src目录下面

          配置如下:

             com.zlc.entity.Point=com.zlc.converter.PointConverter

         表示对每个Action当中拥有的Point类的公共属性point通过PointConverter类型转换器 进行类型转换

        

 注意:PointAction-conversion.properties命名规范:

ActionName+" -conversion.properties"。其中" -conversion.properties"是固定写法。

5 创建转化器的类

package com.zlc.converter;

import java.util.Map;

import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
import com.zlc.entity.Point;

public class PointConverter extends DefaultTypeConverter {

 @Override
 public Object convertValue(Map<String, Object> context, Object value,
   Class toType) {

  if (Point.class == toType) {// 表示要转换的目标类型是Point类型(从字符串String向Point类型转换)
   Point point = new Point();

   String[] str = (String[]) value;// Object是所有类型的父类,在此Object是一个数组形式,要进行向下类型转换

   String[] paramValues = str[0].split(",");// 表示将接收到的第一个字符串str[0](如"20,30"用逗号分开成两个元素20,30放在数组中去

   int x = Integer.parseInt(paramValues[0]);
   int y = Integer.parseInt(paramValues[1]);

   point.setX(x);
   point.setY(y);

   return point;
  }// 表示客户端到自定义类的转换

  if (String.class == toType) {// 表示目标类型是String(就是从Point类型向String转换)

   Point point = (Point) value;

   int x = point.getX();
   int y = point.getY();

   String result = "[x=" + x + " ,y=" + y + "]";

   return result;
  }
  return null;

 }

}

最后配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
   <package name="default" extends="struts-default">
       <action name="pointConverter" class="com.zlc.action.PointAction">
         <result name="success">/output.jsp</result>
         <result name="input">/input.jsp</result>
        </action>
    </package>
</struts>   

posted on 2012-10-26 16:48  future2012lg  阅读(203)  评论(0编辑  收藏  举报