struts2学习笔记三(第3讲.Struts2的类型转换)

[b][size=xx-large]struts2中的局部类型转换[/size][/b]

其中一个主要的就是使用逗号将点的两个坐标分隔开:

一、创建一个输入页面input.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ 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 '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:textfield name="age" label="age"></s:textfield>
<s:textfield name="username" label="username"></s:textfield>
<s:textfield name="date" label="birthday"></s:textfield>

<s:submit label="submit"></s:submit>
</s:form>

</body>
</html>

二、在src下面新建一个包com.test.bean,在这个包中创建一个类Point.java类:

package com.test.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;
}
}

三、在src下创建一个包com.test.converter,在这个包下创建一个类PointConverter.java要继承DefaultTypeConverter类,用于Point的类型转换:

package com.test.converter;

import java.util.Map;

import com.test.bean.Point;

import ognl.DefaultTypeConverter;

public class PointConverter extends DefaultTypeConverter
{

@Override
public Object convertValue(Map context, Object value, Class toType)
{
// 从字符串数组到Point类型的转换,返回一个Point对象
if(Point.class == toType)
{
Point point = new Point();

String[] str = (String[])value;

String[] paramValues = str[0].split(",");// 根据逗号分割成两个包含字符串的数组

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

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

return point;
}
// 从Point类型转换到字符串类型,返回一个字符串类型值
if(String.class == toType)
{
Point point =(Point)value;

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

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

return result;
}
return null;
}
}

四、在src的com.test.action的包下创建一个Action类PointAction.java继承自ActionSupport:

package com.test.action;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.Point;

public class PointAction 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;
}
}

五、在struts.xml中再创建一个Action,用于处理input.jsp发送过来的请求:

<action name="pointConverter" class="com.test.action.PointAction">
<result name="success">/output.jsp</result>
</action>

六、创建output.jsp页面:

<body>
point:<s:property value="point"/><br>
age:<s:property value="age"/><br>
username:<s:property value="username"/><br>
date:<s:property value="date"/>
</body>

七、需要创建定义一个属性文件,用来让struts2来知道所要使用的转换类PointAction在哪里,同时也是创建在同一个包com.test.action的目录下(注意:这个属性文件的命名是有规范的PointAction-conversion.properties,前面是要对哪个Action类里面的属性进行转换,后面的是固定的名称):

point=com.test.converter.PointConverter


类型转换效果图:
未输入之前:

[img]http://dl.iteye.com/upload/attachment/383938/191b5f65-a6f0-3ef7-815d-07b2c6de5bd0.jpg[/img]
输入时:

[img]http://dl.iteye.com/upload/attachment/383940/a243ead2-b6ca-3a14-a6e9-7d0962c6de30.jpg[/img]
结果页面:

[img]http://dl.iteye.com/upload/attachment/383942/f0f75b45-7e8a-31df-aa5e-1d78d00065bb.jpg[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值