Struts2学习笔记4:Struts2的类型转换【2】
2008-09-09 17:29
471 查看
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
遗留任务:www.opensymphony.com/ognl
下载ognl-2.6.11.jar文件的源代码
1.建立converter.PointConverter.java文件,以下是源代码
import java.util.Map;
import model.Point;
import ognl.DefaultTypeConverter;
public class PointConverter extends DefaultTypeConverter {
public Object convertValue(Map context, Object value, Class toType) {
/* toType 决定转换目标类型
* 转换类型分为两种:(1)客户端-->服务器 String-->自定义类型转换
* (2)服务器-->客户端 自定义类型-->String转换
* value 为数组类型
* 在表单提交时,可以为多个表单元素设置相同的name属性值,在提
交之后,服务器端将得到一个Stirng类型的数组
*/
if ( Point.class == toType ){ //将得到的Object转换成Point类型
Point point = new Point();
String[] str = (String[])value; //value为数组
//取得数组中的第一个元素,其为要进行转换成点坐标的字符串,使用逗号
//分割字符串为数组。
String[] paramValues = str[0].split( "," );
//获取paramValues中的第一个字符串并强制转换成int类型成为X坐标值
int x = Integer.parseInt( paramValues[0] );
//获取paramValues中的第二个字符串并强制转换成int类型成为Y坐标值
int y = Integer.parseInt( paramValues[1] );
point.setX( x ); //设置point的X坐标
point.setY( y ); //设置point的Y坐标
return point; //返回类型为Object,所以直接返回point
}
if ( String.class == toType ){ //将得到的Object转换成String类型
//这些代码比较简单不再多加解释
Point point = (Point)value;
int x = point.getX();
int y = point.getY();
StringBuffer s = new StringBuffer();
s.append( "[x=" );
s.append( x );
s.append( " ,y=" );
s.append( y );
s.append( "]" );
return s.toString();
}
return null;
}
}
2.建立action.PointAction.java文件继承ActionSupport,以下是源代码
设置私有变量的get和set方法,重写execute()方法返回值 SUCCESS
package action;
import java.util.Date;
import model.Point;
import com.opensymphony.xwork2.ActionSupport;
public class PointAction extends ActionSupport {
private static final long serialVersionUID =
-719564524571937657L;
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;
}
public String execute() throws Exception {
return SUCCESS;
}
}
3.修改Struts.xml页面,在package中添加action元素
<action name="pointConverter" class="action.PointAction">
<result name="input">/output.jsp</result>
<result name="success">/output.jsp</result>
</action>
注意:以前我看视频修改Struts.xml是没有加入
<result name="input">/output.jsp</result>这句话,然后报错,百度下找到了解决方法。
4.新建output.jsp文件输入内容,代码如下
通过设置“属性名”<s:property value="属性名"/>来确定从服务器获取的参数
其中属性名对应的是PointAction中 get属性名() 方法
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=GB18030">
<title>返回值</title>
</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"/><br/>
</body>
</html>
5.在PointAction文件所在目录中新建属性文件,格式如下
类名-conversion.properties,
所以新建的属性文件名为PointAction-conversion.properties
在文件中写入内容
point=converter.PointConverter
6.运行tomcat,启动程序。
运行界面如下
提交表单,返回页面
第一次运行的时候URL地址为
http://localhost:8080/struts2/pointConverter.action;
jsessionid=EFF27571EB4B999A8C8D8A1FD449FDCE
第二次提交的时候URL地址为
http://localhost:8080/struts2/pointConverter.action
7.有一个疑问,运行tomcat以后在第一次运行input.jsp文件submit的连接中有一个
jessionid属性,倒是链接出错,但是第二次就没有了。
遗留任务:www.opensymphony.com/ognl
下载ognl-2.6.11.jar文件的源代码
1.建立converter.PointConverter.java文件,以下是源代码
import java.util.Map;
import model.Point;
import ognl.DefaultTypeConverter;
public class PointConverter extends DefaultTypeConverter {
public Object convertValue(Map context, Object value, Class toType) {
/* toType 决定转换目标类型
* 转换类型分为两种:(1)客户端-->服务器 String-->自定义类型转换
* (2)服务器-->客户端 自定义类型-->String转换
* value 为数组类型
* 在表单提交时,可以为多个表单元素设置相同的name属性值,在提
交之后,服务器端将得到一个Stirng类型的数组
*/
if ( Point.class == toType ){ //将得到的Object转换成Point类型
Point point = new Point();
String[] str = (String[])value; //value为数组
//取得数组中的第一个元素,其为要进行转换成点坐标的字符串,使用逗号
//分割字符串为数组。
String[] paramValues = str[0].split( "," );
//获取paramValues中的第一个字符串并强制转换成int类型成为X坐标值
int x = Integer.parseInt( paramValues[0] );
//获取paramValues中的第二个字符串并强制转换成int类型成为Y坐标值
int y = Integer.parseInt( paramValues[1] );
point.setX( x ); //设置point的X坐标
point.setY( y ); //设置point的Y坐标
return point; //返回类型为Object,所以直接返回point
}
if ( String.class == toType ){ //将得到的Object转换成String类型
//这些代码比较简单不再多加解释
Point point = (Point)value;
int x = point.getX();
int y = point.getY();
StringBuffer s = new StringBuffer();
s.append( "[x=" );
s.append( x );
s.append( " ,y=" );
s.append( y );
s.append( "]" );
return s.toString();
}
return null;
}
}
2.建立action.PointAction.java文件继承ActionSupport,以下是源代码
设置私有变量的get和set方法,重写execute()方法返回值 SUCCESS
package action;
import java.util.Date;
import model.Point;
import com.opensymphony.xwork2.ActionSupport;
public class PointAction extends ActionSupport {
private static final long serialVersionUID =
-719564524571937657L;
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;
}
public String execute() throws Exception {
return SUCCESS;
}
}
3.修改Struts.xml页面,在package中添加action元素
<action name="pointConverter" class="action.PointAction">
<result name="input">/output.jsp</result>
<result name="success">/output.jsp</result>
</action>
注意:以前我看视频修改Struts.xml是没有加入
<result name="input">/output.jsp</result>这句话,然后报错,百度下找到了解决方法。
4.新建output.jsp文件输入内容,代码如下
通过设置“属性名”<s:property value="属性名"/>来确定从服务器获取的参数
其中属性名对应的是PointAction中 get属性名() 方法
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=GB18030">
<title>返回值</title>
</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"/><br/>
</body>
</html>
5.在PointAction文件所在目录中新建属性文件,格式如下
类名-conversion.properties,
所以新建的属性文件名为PointAction-conversion.properties
在文件中写入内容
point=converter.PointConverter
6.运行tomcat,启动程序。
运行界面如下
提交表单,返回页面
第一次运行的时候URL地址为
http://localhost:8080/struts2/pointConverter.action;
jsessionid=EFF27571EB4B999A8C8D8A1FD449FDCE
第二次提交的时候URL地址为
http://localhost:8080/struts2/pointConverter.action
7.有一个疑问,运行tomcat以后在第一次运行input.jsp文件submit的连接中有一个
jessionid属性,倒是链接出错,但是第二次就没有了。
相关文章推荐
- Struts2学习笔记5:Struts2类型转换续二
- 尚硅谷学习笔记--Struts2 类型转换(2)
- Struts2 学习笔记——数据类型转换
- Struts2学习笔记之类型转换。
- struts2系列学习笔记(8)---------------类型转换的错误处理
- struts2系列学习笔记(8)---------------类型转换
- Struts2类型转换学习笔记
- 浪曦_Struts2应用开发系列_第2讲.Struts2的类型转换-学习笔记
- [原创]java WEB学习笔记67:Struts2 学习之路-- 类型转换概述, 类型转换错误修改,如何自定义类型转换器
- Struts2.x 学习笔记 之 类型转换与复杂属性
- Struts2.x 学习笔记 之 类型转换器 和 错误提示修改
- 黑马程序员——struts2学习笔记四(类型转换)
- [原创]java WEB学习笔记68:Struts2 学习之路-- 类型转换与复杂属性配合使用
- Struts2-学习笔记系列(13)-类型转换异常和校验器
- Struts2 学习笔记20 类型转换part2 写自己的转换器
- Struts2学习笔记之类型转换 二
- Struts2 学习笔记19 类型转换 Part1
- Struts2-学习笔记系列(9)-OGNL类型转换和类型绑定
- 尚硅谷学习笔记--Struts2 类型转换(1)
- 【js学习笔记-019】-- 类型转换