2007-12-10
struts2启程3
struts2启程之类型转换
类型转换要考虑两点:从客户端向服务器、从服务器向客户端。
1、从客户端向服务器转换,肯定是String类型转换成javaBean类型
2、从服务器端向客户端转换,肯定是javaBean类型转换成String类型
所以,这个转换类就需要考虑这两点
类型转换的流程:
通过JSP页面将其中的一个元素值转换成javaBean值,在页面输出时,将取得的javaBean自动转换成了String类型,并输出。
要点文件:转换类、配置文件-维系转换类与Action类之间的纽带
一、写转换类的注意点:
1、继承ognl.DefaultTypeConverter类
2、重写convertValue(Map context, Object value, Class toType)方法
示例代码:
java 代码
- package com.test.convert;
- 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) {
- if(Point.class == toType)
- {
- Point point = new Point();
- String[] str = (String[]) value;
- String[] points = str[0].split(",");
- int x = Integer.parseInt(points[0]);
- int y = Integer.parseInt(points[1]);
- point.setX(x);
- point.setY(y);
- return point;
- }
- if(String.class == toType)
- {
- Point point = (Point) value;
- String str = "[x=" + point.getX() + " , y=" + point.getY() + "]";
- return str;
- }
- return null;
- }
- }
二、写配置文件的注意点:
1、 配置文件名的位置:必须与应用这个转换类的Action类在同一个包中
2、 配置文件名称命名:必须是要应用这个转换类的Action类的名字加"-conversion.properties"
3、配置文件内容:"="号的左边是Action类中要转换的Bean所对应的属性名字,"="号的右边是转换类的全称(包名加类名)
示例:
pointAction-conversion.properties
- point=com.test.convert.PointConverter
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;
- }
- }
java 代码
- 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 String username;
- private int age;
- private Date date;
- public Point getPoint() {
- return point;
- }
- public void setPoint(Point point) {
- this.point = point;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public Date getDate() {
- return date;
- }
- public void setDate(Date date) {
- this.date = date;
- }
- @Override
- public String execute() throws Exception {
- return SUCCESS;
- }
- }
java 代码
- "1.0" encoding="UTF-8" ?>
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <package name="wjl_struts" extends="struts-default">
- "login" class="com.test.action.LoginAction">
- "input">/login.jsp
- "success">/result.jsp
- "failed">/login.jsp
- "pointConvert" class="com.test.action.PointAction">
- "success">/output.jsp
- package>
xml 代码
- <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- >
- <html>
- <head>
- <base href="">
- <title>My JSP 'input.jsp' starting pagetitle>
- <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">
- head>
- <body>
- <s:form action="pointConvert">
- <s:textfield name="point" label="Point">s:textfield>
- <s:textfield name="username" label="username">s:textfield>
- <s:textfield name="age" label="age">s:textfield>
- <s:textfield name="date" label="date">s:textfield>
- <s:submit label="submit">s:submit>
- s:form>
- body>
- html>
xml 代码
- <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- >
- <html>
- <head>
- <base href="">
- <title>My JSP 'output.jsp' starting pagetitle>
- <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">
- head>
- <body>
- Point:<s:property value="point"/><br/>
- Username:<s:property value="username"/><br/>
- Age:<s:property value="age"/><br/>
- Date:<s:property value="date"/><br/>
- body>
- html>
发表评论
- 浏览: 29757 次
- 性别:

- 来自: 杭州

- 详细资料
搜索本博客
我的相册
ExtJs中关于grid和store的应用分析(一)
共 14 张
共 14 张
最近加入圈子
最新评论
-
ExtJs视频教程集锦
怎么ftp上还没有, 急死了!
-- by slmdyk -
EXTJS动态树的实现
不明白你说的意思
-- by yahaitt -
EXTJS动态树的实现
你好! 请问添加节点时, 提示需要验证: http://localhost:80 ...
-- by davy138 -
ExtJs想入门的请进-解读 ...
非常感谢!正在学extjs,正不知道该如何下手。对java script不熟悉, ...
-- by xt95 -
关于动态树的完整示例代码 ...
...
-- by yahaitt






评论排行榜