struts2中的类型转换

下面以输入“x,y”代表一个点的坐标,然后再在另一个页面中输出,来说明struts2中的类型转换的一般步骤
1.创建输入点的坐标的JSP页面,input.jsp,代码如下所示:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'input.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">
<s:textfield name="point" label="point"></s:textfield>
<s:submit label="submit"></s:submit>
</s:form>
</body>
</html>
这里需要注意的是,struts2中的标签库的导入用<%@ taglib prefix="s" uri="/struts-tags" %>
2.创建用来展示处理结果的JSP页面,output.jsp, 代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!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>
</body>
</html>
这里需要注意的是,value="point"中的point是与 input.jsp中的textfield中的name="point"中的point所对应的
3.创建用来表示Point的JavaBean类,Point.java, 代码如下:
package org.wuzsh.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;
}
}
4.创建用来处理表单请求的Action, PointConverterAction.java, 代码如下:
package org.wuzsh.struts;

import java.util.Date;

import org.wuzsh.bean.Point;

import com.opensymphony.xwork2.ActionSupport;

public class PointConverterAction extends ActionSupport
{
private Point point;
private int age;
private String username;
private Date date;
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 String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public Date getDate()
{
return date;
}
public void setDate(Date date)
{
this.date = date;
}
@Override
public String execute() throws Exception
{
return "success";
}
}
这里需要注意的是,PointConverterAction类继承了ActionSupport类,ActionSupport类是实现Action的一些
common的操作的
5.配置struts.xml,用来指明表单请求与相应的处理请求的Action类的对应,以及指定处理结果输出的页面:
<?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="struts" extends="struts-default">
<action name="pointConverter" class="org.wuzsh.struts.PointConverterAction">
<result name="success">/output.jsp</result>
</action>
</package>
</struts>
7 添加完成String与Point类之间的相互轮换的类PointConverter:
package org.wuzsh.converter;

import java.util.Map;

import org.wuzsh.bean.Point;

import ognl.DefaultTypeConverter;

public class PointConverter extends DefaultTypeConverter
{

@Override
public Object convertValue(Map context, Object value, Class toType)
{
if(Point.class == toType)//String 转换为Point
{
String[] str = (String[])value;//这里需要注意,这里要转换成一个String[], 而不是String
String[] paramValues = str[0].split(",");
Point point = new Point();
point.setX(Integer.parseInt(paramValues[0]));
point.setY(Integer.parseInt(paramValues[1]));
return point;
}
if(String.class == toType)//Point转换为String
{
Point point = (Point)value;
int x = point.getX();
int y = point.getY();
String result = "[x = " + x + ", y = " + y + "]";
return result;
}
return null;
}
}
这里继承了 DefaultTypeConverter,覆盖了其中的convertValue方法,实现了String类与Point类之间的相互转换
8.在Action类所在的包里添加一个名为PointConverterAction-conversion.properties的文件,用来指明转换类所在的地方:
point=org.wuzsh.converter.PointConverter
这里需要特别注意的是,这个文件的命名特别有讲究,该名字有两部分组成,前一部分为
PointConverterAction, 用来指定可能用到类型转换类的Action类,第二部分为conversion.properties,这一部分是固定的,是不能改变的,两部分之间要用'-'连接。
另外需要特别注意的是:如果一个对象在很多个Action类中需要类型转换,那么一个个的去写配置文件就显得比较麻烦,这里可以采用全局类型转换的策略,具体的做法是在struts.xml所在的文件夹中添加xwork-conversion.properties文件,文件的格式为:(比如说要把所有Action类中用的到Point类都进行转换)
org.wuzsh.bean.Point=org.wuzsh.converter.PointConverter
这里前一部分为要转换的类,后一部分仍然为具体实现转换的类

以上即是整个实现输入简单类型轮换的过程。

下面做一个简单的总结,一般类型转换的流程为:
(1)用户提交表单,这时处理此表单的相应的Action类会用相关属性的setter来保存属性的值。
(2)在sette赋值过程中,会检查有没有自定义的类型转换,如果有,则会根据Action-conversion.properties中的对应关系,来进行相应的类型转换,如果没有,则采用JAVA中默认的赋值方式,直接赋值。
(3)Action类执行 execute方法,处理表单请求
(4)处理完成后,然后根据struts.xml中,找到相应的输出处理结果的页面,输出相关结果。在输出结果时,会调用到Action类的getter,此时getter会检查有没用户自定义的类型转化,如果有,则根据
Action-conversion.properties中的对应关系,来进行相应的类型转换,如果没有,则采用JAVA中的默认方式。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值