论坛首页 Java企业应用论坛

Struts2第四篇:类型转换

浏览 1164 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2008-06-26   最后修改:2008-11-18

大家好!新的一天,新的开始。加油哦!我今天要讲解的是struts2中的数据转换。由于今天我时间很紧。只能将示例展示给大家自学。如果有不懂的地方可以留言。

首先是view(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 '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>
  <h1>用逗号将点的两个坐标分开</h1>
    <s:form action="pointConverter">
    <s:textfield name="point" label="点"></s:textfield>
    <s:textfield name="age" label="年龄"></s:textfield>
    <s:textfield name="username" label="用户名"></s:textfield>
    <s:textfield name="date" label="日期"></s:textfield>
    <s:submit name="submit" label="submit"></s:submit>
    </s:form>
  </body>
</html>

 接下来是output.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>
		age:
		<s:property value="age" /><br>
		username:
		<s:property value="username" /><br>
		date:
		<s:property value="date" />
	</body>
</html>

 转换类的编写:

package com.newstar.struts2.pojo;

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

package com.newstar.struts2.converter;

import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

import com.newstar.struts2.pojo.Point;

public class PointConverter2 extends StrutsTypeConverter {

	@Override
	public Object convertFromString(Map map, String[] arg1, Class arg2) {
		String[] temp = arg1[0].split(",");
		int x = Integer.parseInt(temp[0]);
		int y = Integer.parseInt(temp[1]);
		Point point = new Point();
		point.setX(x);
		point.setY(y);
		return point;
	}

	@Override
	public String convertToString(Map map, Object object) {
		Point point = (Point) object;
		int x = point.getX();
		int y = point.getY();
		String result = x + "," + y;
		return result;
	}

}

 action编写:

package com.newstar.struts2.action;

import java.util.Date;

import com.newstar.struts2.pojo.Point;
import com.opensymphony.xwork2.ActionSupport;

public class PointConverterAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1825693787594339645L;
	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;
	}

}

 转换的资源文件配置:分两种:全局转换,局部转换。

全局转换的资源文件名必须以xwork-convertion.properties命名。存放位置:src下。

局部转换必须以转换类名+“-convertion.properties”命名。存放位置:和Action放在同一个目录下即可。

其实他编译后会在class目录下。大家可以在服务器下面找到。

下面说下方法:

这是全局转换的资源文件代码:com.newstar.struts2.pojo.Point=com.newstar.struts2.converter.PointConverter2

这是局部转换的资源文件爱你代码:point=com.newstar.struts2.converter.PointConverter

两者选其一而用之即可。

struts.xml配置文件和我讲解的Struts2第3期的方式是一样的。大家可以去看看。

web.xml文件没改变。

 

今天实在是对不住大家了,太忙了!这篇如果没有讲解真的有点不好懂。唉,真是很不好办啊。我也是没办法,现在日子不好过啊!看哪天我有时间了,在把这篇补回来吧!

goodbye!

   发表时间:2008-07-01  
这篇文章不是你原创的啊,都是风中叶老师讲的。希望能多有原创。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics