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 代码
  1. package com.test.convert;   
  2.   
  3. import java.util.Map;   
  4.   
  5. import com.test.bean.Point;   
  6.   
  7. import ognl.DefaultTypeConverter;   
  8.   
  9. public class PointConverter extends DefaultTypeConverter {   
  10.   
  11.     @Override  
  12.     public Object convertValue(Map context, Object value, Class toType) {   
  13.         if(Point.class == toType)   
  14.         {   
  15.             Point point = new Point();   
  16.             String[] str = (String[]) value;   
  17.             String[] points = str[0].split(",");   
  18.                
  19.             int x = Integer.parseInt(points[0]);   
  20.             int y = Integer.parseInt(points[1]);   
  21.                
  22.             point.setX(x);   
  23.             point.setY(y);   
  24.                
  25.             return point;   
  26.         }   
  27.         if(String.class == toType)   
  28.         {   
  29.             Point point = (Point) value;   
  30.                
  31.             String str = "[x=" + point.getX() + " , y=" + point.getY() + "]";   
  32.             return str;   
  33.         }   
  34.            
  35.         return null;   
  36.     }   
  37.        
  38. }   

 

二、写配置文件的注意点:

1、 配置文件名的位置:必须与应用这个转换类的Action类在同一个包中

2、 配置文件名称命名:必须是要应用这个转换类的Action类的名字加"-conversion.properties"

3、配置文件内容:"="号的左边是Action类中要转换的Bean所对应的属性名字,"="号的右边是转换类的全称(包名加类名)

 示例:

pointAction-conversion.properties
  1. point=com.test.convert.PointConverter  

 

java 代码
  1. package com.test.bean;   
  2.   
  3. public class Point {   
  4.     private int x;   
  5.     private int y;   
  6.     public int getX() {   
  7.         return x;   
  8.     }   
  9.     public void setX(int x) {   
  10.         this.x = x;   
  11.     }   
  12.     public int getY() {   
  13.         return y;   
  14.     }   
  15.     public void setY(int y) {   
  16.         this.y = y;   
  17.     }   
  18.        
  19. }   

 

java 代码
  1. package com.test.action;   
  2.   
  3. import java.util.Date;   
  4.   
  5. import com.opensymphony.xwork2.ActionSupport;   
  6. import com.test.bean.Point;   
  7.   
  8. public class PointAction extends ActionSupport {   
  9.     private Point point;   
  10.     private String username;   
  11.     private int age;   
  12.     private Date date;   
  13.     public Point getPoint() {   
  14.         return point;   
  15.     }   
  16.     public void setPoint(Point point) {   
  17.         this.point = point;   
  18.     }   
  19.     public String getUsername() {   
  20.         return username;   
  21.     }   
  22.     public void setUsername(String username) {   
  23.         this.username = username;   
  24.     }   
  25.     public int getAge() {   
  26.         return age;   
  27.     }   
  28.     public void setAge(int age) {   
  29.         this.age = age;   
  30.     }   
  31.     public Date getDate() {   
  32.         return date;   
  33.     }   
  34.     public void setDate(Date date) {   
  35.         this.date = date;   
  36.     }   
  37.        
  38.     @Override  
  39.     public String execute() throws Exception {   
  40.         return SUCCESS;   
  41.     }   
  42.        
  43.        
  44. }   

 

java 代码
  1. "1.0" encoding="UTF-8" ?>   
  2.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  3.     "http://struts.apache.org/dtds/struts-2.0.dtd">   
  4.   
  5.        
  6.     <package name="wjl_struts" extends="struts-default">   
  7.        
  8.         "login" class="com.test.action.LoginAction">   
  9.             "input">/login.jsp   
  10.             "success">/result.jsp   
  11.             "failed">/login.jsp   
  12.            
  13.            
  14.         "pointConvert" class="com.test.action.PointAction">   
  15.             "success">/output.jsp   
  16.            
  17.            
  18.     package>   
  19.        
  20.   

 

xml 代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3. <%   
  4. String path = request.getContextPath();   
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   
  6. %>  
  7.   
  8. >  
  9. <html>  
  10.   <head>  
  11.     <base href="">  
  12.        
  13.     <title>My JSP 'input.jsp' starting pagetitle>  
  14.        
  15.     <meta http-equiv="pragma" content="no-cache">  
  16.     <meta http-equiv="cache-control" content="no-cache">  
  17.     <meta http-equiv="expires" content="0">       
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.     <meta http-equiv="description" content="This is my page">  
  20.       
  21.   
  22.   head>  
  23.      
  24.   <body>  
  25.     <s:form action="pointConvert">  
  26.         <s:textfield name="point" label="Point">s:textfield>  
  27.         <s:textfield name="username" label="username">s:textfield>  
  28.         <s:textfield name="age" label="age">s:textfield>  
  29.         <s:textfield name="date" label="date">s:textfield>  
  30.         <s:submit label="submit">s:submit>  
  31.     s:form>  
  32.   body>  
  33. html>  

 

xml 代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3. <%   
  4. String path = request.getContextPath();   
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   
  6. %>  
  7.   
  8. >  
  9. <html>  
  10.   <head>  
  11.     <base href="">  
  12.        
  13.     <title>My JSP 'output.jsp' starting pagetitle>  
  14.        
  15.     <meta http-equiv="pragma" content="no-cache">  
  16.     <meta http-equiv="cache-control" content="no-cache">  
  17.     <meta http-equiv="expires" content="0">       
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.     <meta http-equiv="description" content="This is my page">  
  20.       
  21.   
  22.   head>  
  23.      
  24.   <body>  
  25.     Point:<s:property value="point"/><br/>  
  26.     Username:<s:property value="username"/><br/>  
  27.     Age:<s:property value="age"/><br/>  
  28.     Date:<s:property value="date"/><br/>  
  29.   body>  
  30. html>  

 

 

评论
freepig 2008-02-24
[img][/img][/url][url][flash=200,200][/flash][url][/url]
引用
[u][/u]
发表评论

您还没有登录,请登录后发表评论

yahaitt
搜索本博客
我的相册
24165a36-4704-3d78-a4aa-aa66ca678e03-thumb
ExtJs中关于grid和store的应用分析(一)
共 14 张
最近加入圈子
存档
最新评论