struts2的類型轉換——Struts2第二講

類型轉換css

注:本文系做者在看了浪曦的風中葉老師的struts2視頻的我的總結,但願能幫助廣大struts2的初學者。 
第一步:(這一步和其餘同樣,這裏從簡) 
依舊是新建一個web project,命名爲struts2,導入struts2必須的包。在src目錄下新建struts.xml,修改web.xml文件。 

第二步: 
將index.jsp更名爲input.jsp(這個不是必須的,事實上也沒有必要,此處只是爲了便於稱呼)。Input.jap的代碼以下 
html

Java代碼  收藏代碼java

  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  web

  2. <%  apache

  3. String path = request.getContextPath();  jsp

  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  ide

  5. %>  ui

  6. <%@ taglib prefix="s"  uri="/struts-tags" %>  this

  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  spa

  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

struts2下的helloworld(如何讓第一個stru ... | 輸入校驗(1)——struts2第三講

相關文章
相關標籤/搜索