zoukankan      html  css  js  c++  java
  • struts中的类型转换(2)

    第一步:(这一步和其它一样,这里从简) 
    依然是新建一个web project,命名为struts2,导入struts2必须的包。在src文件夹下新建struts.xml。改动web.xml文件。 

    第二步: 
    将index.jsp改名为input.jsp(这个不是必须的,其实也没有必要,此处仅仅是为了便于称呼)。Input.jap的代码例如以下 
    Java代码  收藏代码
    1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
    2. <%  
    3. String path = request.getContextPath();  
    4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
    5. %>  
    6. <%@ taglib prefix="s"  uri="/struts-tags" %>  
    7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
    8. <html>  
    9.   <head>  
    10.     <base href="<%=basePath%>">  
    11.       
    12.     <title>My JSP 'index.jsp' starting page</title>  
    13.     <meta http-equiv="pragma" content="no-cache">  
    14.     <meta http-equiv="cache-control" content="no-cache">  
    15.     <meta http-equiv="expires" content="0">      
    16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
    17.     <meta http-equiv="description" content="This is my page">  
    18.     <!--  
    19.     <link rel="stylesheet" type="text/css" href="styles.css">  
    20.     -->  
    21.   </head>  
    22.     
    23.   <body>  
    24.   <h1><font color='red'> 请输入坐标,用英文半角逗号隔开</font></h1>  
    25.    <s:form action="pointconverter">  
    26.         <s:textfield name="point1" label="point1"></s:textfield>  
    27.         <s:textfield name="point2" label="point2"></s:textfield>  
    28.         <s:textfield name="point3" label="point3"></s:textfield>  
    29.           
    30.         <s:submit name="submit"> </s:submit>  
    31.    </s:form>  
    32.   </body>  
    33. </html>  


    该文件有两个要注意的地方 
    1.使用了struts2的标签库 <%@ taglib prefix="s"  uri="/struts-tags" %> 
    2.f注意form中的action属性 

    第三步: 
    在src下新建包com.beam,当中定义point类 point.java 代码例如以下: 
    Java代码  收藏代码
    1. package com.bean;  
    2.   
    3. public class Point {  
    4.   
    5.       
    6.   
    7.         public int getX() {  
    8.             return x;  
    9.         }  
    10.         public void setX(int x) {  
    11.             this.x = x;  
    12.         }  
    13.         public int getY() {  
    14.             return y;  
    15.         }  
    16.         public void setY(int y) {  
    17.             this.y = y;  
    18.         }  
    19.           
    20.           
    21.           
    22.         private int x;  
    23.         private int y;  
    24. }  


    Action 
    在src下新建包com.action 
    当中新建类PointAction.java 代码例如以下 
    Java代码  收藏代码
    1. package com.action;  
    2.   
    3. import com.opensymphony.xwork2.ActionSupport;  
    4. import com.bean.Point;  
    5. public class PointAction extends ActionSupport  
    6. {  
    7.   
    8.         public Point getPoint1() {  
    9.             return point1;  
    10.         }  
    11.         public void setPoint1(Point point1) {  
    12.             this.point1 = point1;  
    13.         }  
    14.         public Point getPoint2() {  
    15.             return point2;  
    16.         }  
    17.         public void setPoint2(Point point2) {  
    18.             this.point2 = point2;  
    19.         }  
    20.         public Point getPoint3() {  
    21.             return point3;  
    22.         }  
    23.         public void setPoint3(Point point3) {  
    24.             this.point3 = point3;  
    25.         }  
    26.   
    27.         public String execute() throws Exception  
    28.         {  
    29.             return SUCCESS;  
    30.         }  
    31.           
    32.                        
    33.         private Point point1;  
    34.         private Point point2;  
    35.         private Point point3;  
    36.           
    37. }  


    第五步:配置struts.xml文件 代码例如以下: 
    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="utf-8" ?>  
    2. <!DOCTYPE struts PUBLIC  
    3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
    4.     "struts.apache.org/dtds/struts-2.0.dtd">  
    5.       
    6.     <struts>  
    7.       
    8.             <package name="struts2"  extends="struts-default">  
    9.                     <action name="pointconverter" class="com.action.PointAction">  
    10.                                 <result name="success">/output.jsp</result>  
    11.                                 <result name="input">/input.jsp</result>  
    12.                     </action>  
    13.             </package>  
    14.     </struts>  

    第六步: 
    在WebRoot下新建视图output.jsp 依然运用struts2的标签库 代码例如以下 
    Jsp代码  收藏代码
    1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
    2. <%  
    3. String path = request.getContextPath();  
    4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
    5. %>  
    6. <%@ taglib prefix="s" uri="/struts-tags" %>  
    7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
    8. <html>  
    9.   <head>  
    10.     <base href="<%=basePath%>">  
    11.       
    12.     <title>My JSP 'output.jsp' starting page</title>  
    13.       
    14.     <meta http-equiv="pragma" content="no-cache">  
    15.     <meta http-equiv="cache-control" content="no-cache">  
    16.     <meta http-equiv="expires" content="0">      
    17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
    18.     <meta http-equiv="description" content="This is my page">  
    19.     <!--  
    20.     <link rel="stylesheet" type="text/css" href="styles.css">  
    21.     -->  
    22.   
    23.   </head>  
    24.     
    25.   <body>  
    26.   
    27.    point1:<s:property value="point1"/><br>  
    28.    point2: <s:property value="point2"/><br>  
    29.    point3 <s:property value="point3"/>  
    30.   </body>  
    31. </html>  

    第七步:类型转化器 
    在src文件夹下新建com.converter包 当中新建类PointConverter.java 代码例如以下 
    Java代码  收藏代码
    1. package com.converter;  
    2.   
    3. import java.util.Map;  
    4.   
    5. import org.apache.struts2.util.StrutsTypeConverter;  
    6. import com.bean.Point;  
    7. public class PointConverter extends StrutsTypeConverter {  
    8.   
    9.     @Override  
    10.     public Object convertFromString(Map arg0, String[] arg1, Class arg2) {  
    11.           
    12.         Point point = new Point();  
    13.          String[] values= arg1[0].split(",");  
    14.            
    15.          int x = Integer.parseInt(  values[0].trim() );  
    16.          int y = Integer.parseInt(  values[1].trim()  );  
    17.            
    18.          point.setX(x);  
    19.          point.setY(y);  
    20.            
    21.          return point;  
    22.           
    23.           
    24.     }  
    25.   
    26.     @Override  
    27.     public String convertToString(Map arg0, Object arg1) {  
    28.   
    29.                 Point  point = (Point) arg1;  
    30.                   
    31.                 int x = point.getX();  
    32.                 int y  = point.getY();  
    33.                   
    34.                 String result = "<x= "+x+" ,  y="+y+" >";  
    35.                   
    36.                 return result;  
    37.     }  
    38.   
    39. }  


    第八步: 
    使类型转化器和action中的相应point属性关联起来新建一个properties文件 
    这里有两种方法: 
    第一种是在com.converter包中新建一个PointAction-conversion.properties文件 
    代码例如以下: 
    Properties代码  收藏代码
    1. point1=com.converter.PointConverter  
    2. point2=com.converter.PointConverter  
    3. point3=com.converter.PointConverter  


    另外一种:是在src文件夹下直接新建一个文件 xwork-conversion.properties 
    代码例如以下 
    Properties代码  收藏代码
    1. com.bean.Point=com.converter.PointConverter  

    ok
  • 相关阅读:
    JDK1.8中对hashmap的优化
    ShutdownHook作用
    【工作相关】常用Shell命令
    [技术学习]收藏技术博客
    【技术学习】Mongo Administration
    【技术学习】saltstack 笔记(一) --匹配Minion
    【工作相关】个人常用脚本及代码
    【工作相关】常用工具
    【现场问题】add trust cert into JAVA_HOME
    【工作相关】替换Rancher证书
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/6747745.html
Copyright © 2011-2022 走看看