`
yanglinumber1
  • 浏览: 5662 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
最近访客 更多访客>>
社区版块
存档分类
最新评论

Struts2第四篇:类型转换

 
阅读更多

大家好!新的一天,新的开始。加油哦!我今天要讲解的是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!

分享到:
评论
1 楼 rennuoting 2008-07-01  
这篇文章不是你原创的啊,都是风中叶老师讲的。希望能多有原创。

相关推荐

    Struts2实战

    4. Struts2框架的扩展:Struts2框架具有很高的可扩展性,开发者可以通过实现拦截器、类型转换器和结果类型等方式,对Struts2框架进行扩展。同时,Struts2也支持与Spring和Hibernate等框架的整合,可以方便的进行服务...

    struts1和struts2区别

    - **Struts2**:使用OGNL进行类型转换,提供更灵活的配置和基本及常见类型的转换器。 9. **校验**: - **Struts1**:校验可以在ActionForm的validate方法中完成,或使用Commons Validator扩展,对子对象的校验...

    struts2数据类型转换

    ### Struts2 数据类型转换详解 #### 一、引言 在Web开发中,特别是使用Struts2框架进行开发时,数据类型转换是一项至关重要的功能。由于Web应用程序的基础通信协议HTTP仅支持字符串形式的数据传输,因此,服务器端...

    struts2学习笔记四(第4讲.Struts2的类型转换续)

    在"Struts2学习笔记四(第4讲.Struts2的类型转换续)"中,我们将会深入探讨Struts2中的类型转换机制,这是一个核心特性,用于处理Action类属性与HTTP请求参数之间的数据类型转换。 在HTTP请求中,数据通常是字符串...

    Struts2讲义-作者:吴峻申

    - Struts2利用OGNL进行数据绑定和类型转换,使开发者能够更方便地操作模型对象。 - **进行校验**: - Struts2内置了丰富的验证机制,可以在服务器端进行数据的有效性检查。 - 开发者可以通过配置文件或注解的方式...

    struts2(1-7)源码 struts2学习入门 源码学习

    struts4 使用Struts2中内部类中的方法进行自定义类型转换,用到的类StrutsTypeConverter struts5 增加集合类型,使用范型 struts6 直接设置要进行类型转换的字段值 struts7 制作一个表单,对表单中的内容进行...

    转:struts1与struts2的区别

    - **Struts2**: 使用OGNL进行类型转换,为基本和常用对象提供了转换器。Struts2的类型转换机制更为灵活且易于配置。 #### 9. 数据校验机制 - **Struts1**: 支持在`ActionForm`的`validate`方法中手动校验数据,...

    Struts1与Struts2本质区别

    - **Struts 2**:使用OGNL进行类型转换,支持更多的数据类型转换,提高了灵活性。 #### 9. 数据校验的对比 - **Struts 1**:支持在`ActionForm`中重写`validate`方法进行手动校验,也可以整合`Commons-validator`...

    struts2 转换器

    7. **扩展性**:Struts2的转换器系统设计得非常灵活,可以轻松地与其他第三方库如Hibernate Validator集成,实现更复杂的验证和转换逻辑。 通过理解并熟练掌握Struts2转换器的使用,开发者可以有效地处理用户输入,...

    [Struts 2权威指南--基于WebWork核心的MVC开发(高清完整版) 1/12

    第1章 Struts 2概述,第2章 Struts 2下的HelloWorld,第3章 Struts 2基础,第4章 深入Struts 2,第5章 Struts 2的类型转换,第6章 文件的上传和下载.,第7章 Struts 2的拦截器,第8章 Struts 2的输入校验,9.2 ...

    struts1与struts2本质区别

    - **Struts2**:使用OGNL进行类型转换,支持基本数据类型和常用对象之间的转换,并且具有更高的灵活性。 #### 九、数据校验的对比 - **Struts1**:支持在`ActionForm`中重写`validate`方法进行手动校验,或者整合...

    struts2 ppt 教程 十章

    第一章:认识体验Struts 2 ...第四章:struts2中的OGNL 第五章:struts2标签库 第六章:struts2国际化 第七章:struts2验证 第八章:struts2文件上传下载 第九章:struts2拦截器 第十章:struts2类型转换

    struts2jar包

    2. **xwork-core.jar**:XWork是Struts2的基础,它提供了一些基础功能,如类型转换、Ognl表达式支持、拦截器机制等。很多Struts2的功能都是基于XWork实现的。 3. **ognl.jar**:OGNL(Object-Graph Navigation ...

    IBM-struts2内部培训资料

    4. **第四章:struts2中的OGNL**:OGNL(Object-Graph Navigation Language)是Struts2中的表达式语言,用于在视图和模型之间传递数据。本章将解释OGNL的基本语法,以及如何在Action和JSP中使用OGNL表达式。 5. **...

    浪曦\浪曦_Struts2应用开发系列_第1讲

    - **创建第一个Struts2应用**:创建一个简单的Hello World程序,了解Action类、配置文件(struts.xml)的作用以及结果类型映射。 - **配置Web.xml**:配置Struts2的核心过滤器`struts2-convention-plugin`或`...

    Struts2 demo2

    通过这些知识点,我们可以了解到"Struts2 demo2"可能涵盖了Struts2的核心特性,如Action配置、数据校验、类型转换、OGNL表达式、防止重复提交和自定义标签库的使用。这些内容对于理解和实践Java web开发中的MVC模式...

    精通struts2

    ### 精通Struts2:关键技术点解析 #### Struts2概述与MVC架构 - **背景**:随着计算机技术的进步,特别是互联网技术的飞速发展,软件系统的复杂度日益增加,传统的软件开发方式已经难以满足现代软件工程的需求。在...

    struts1和struts2的区别

    - **Struts2**:使用OGNL进行类型转换,框架内置了对常见类型和原始类型的转换器。 9. **验证** - **Struts1**:支持手动验证和扩展验证器,但验证子对象较困难。 - **Struts2**:提供手动验证和基于XWork的验证...

    struts2中文学习文档

    WebWork在被Struts2吸收之前,已经具备了许多先进的特性,如动态类型转换、拦截器和结果类型等。这些特性后来都被融入到了Struts2中,使得Struts2不仅继承了WebWork的优点,还在此基础上进行了改进和扩展,成为了...

Global site tag (gtag.js) - Google Analytics