• sturct2类型转化


    第三种struts2类型转换方式实例
    1.convert.jsp

    Jsp代码  收藏代码
    1. <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>  
    2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
    3. <html>  
    4.   <head>  
    5.     <title>类型转换</title>  
    6.   </head>  
    7.   <body>  
    8.     <form action="convert.action" method="post">  
    9.         point:<input name="point" type="text"></br>  
    10.               <input type="submit" value="submit">  
    11.     </form>  
    12.   </body>  
    13. </html>  


    2.convertResult.jsp

    Jsp代码  收藏代码
    1. <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>  
    2. <%@ taglib uri="/struts-tags" prefix="s" %>  
    3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
    4. <html>  
    5.   <head>  
    6.     <title>类型转换结果页面</title>  
    7.   </head>  
    8.   <body>  
    9.         point:<s:property value="point"/>  
    10.   </body>  
    11. </html>  


    3.Point.java(POJO类)

    Java代码  收藏代码
    1. package com.hitsoft.model;  
    2. public class Point {  
    3.     private int x;  
    4.     private int y;  
    5.     public int getX() {  
    6.         return x;  
    7.     }  
    8.     public void setX(int x) {  
    9.         this.x = x;  
    10.     }  
    11.     public int getY() {  
    12.         return y;  
    13.     }  
    14.     public void setY(int y) {  
    15.         this.y = y;  
    16.     }  
    17.     @Override  
    18.     public String toString() {  
    19.         String result = "x = " + x + " , y = " +y;  
    20.         return result;  
    21.     }  
    22.       
    23. }  



    4.struts.xml

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8" ?>  
    2.   
    3. <!DOCTYPE struts PUBLIC  
    4.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
    5.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
    6. <struts>  
    7.     <package name="struts2"  extends="struts-default">  
    8.     <action name="convert" class="com.hitsoft.action.ConvertAction">  
    9.         <result name="success">/convertResult.jsp</result>  
    10.     </action>  
    11.     </package>  
    12. </struts>  


    5.ConvertAction.java

    Java代码  收藏代码
    1. package com.hitsoft.action;  
    2. import java.util.Date;  
    3. import com.hitsoft.model.Point;  
    4. import com.opensymphony.xwork2.ActionSupport;  
    5.   
    6. public class ConvertAction extends ActionSupport{  
    7.     private Point point;  
    8.           
    9.     public Point getPoint() {  
    10.         return point;  
    11.     }  
    12.     public void setPoint(Point point) {  
    13.         this.point = point;  
    14.     }  
    15.       
    16.     public String execute() throws Exception{  
    17.         return "success";  
    18.     }  
    19. }  


    6.xwork-conversion.properties(必须与struts.xml同一级包下定义)

    Properties代码  收藏代码
    1. point=com.hitsoft.convert.PointConverter  


    7.PointConverter.java

    Java代码  收藏代码
    1. package com.hitsoft.convert;  
    2. import java.util.Map;  
    3. import org.apache.struts2.util.StrutsTypeConverter;  
    4. import com.hitsoft.model.Point;  
    5. public class PointConverter extends StrutsTypeConverter{  
    6.   
    7.     @SuppressWarnings("unchecked")  
    8.     @Override  
    9.     public Object convertFromString(Map context, String[] values, Class toClass) {  
    10.         Point point = new Point();  
    11.         String value = values[0];  
    12.         String[] result = value.split(",");  
    13.         point.setX(Integer.parseInt(result[0]));  
    14.         point.setY(Integer.parseInt(result[1]));  
    15.         return point;  
    16.     }  
    17.   
    18.     @SuppressWarnings("unchecked")  
    19.     @Override  
    20.     public String convertToString(Map context, Object o) {  
    21.         Point point = (Point) o;  
    22.         int x = point.getX();  
    23.         int y = point.getY();  
    24.         String result = "x: " + x + "y:" + y;  
    25.         return result;  
    26.     }  
    27.   
    28. }  



    8.访问地址:
    http://localhost:8080/struts2/convert.jsp
    输入:
    1,2
    输出:
    point:1,2

    说明:这种类型转换的优点是,对于PointConvert转换器,只有项目中用到Point对象转换,就可以自动转换

                                              来源:http://wanglihu.iteye.com/blog/1405647

  • 相关阅读:
    MVC实战之排球计分(六)—— 使用EF框架,创建Controller,生成数据库。
    MVC实战之排球计分(五)—— Controller的设计与实现
    MVC实战之排球计分(四)—— View设计与实现
    MVC实战之排球计分(三)—— 模型类的设计与实现
    MVC实战之排球计分(二)—— 构架概要设计
    MVC实战之排球计分(一)—— 需求分析与数据库设计
    连接虚机中的mysql服务
    mongodb3集群搭建
    spring-data-redis配制
    redis4集群
  • 原文地址:https://www.cnblogs.com/kongxc/p/6202626.html
Copyright © 2020-2023  润新知