小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Maven2下第一個Struts2簡單實例

 eeason 2011-06-21

       本人準備在將來的日子里發(fā)布一部Maven2 + Struts2 + EJB3的實例系列,希望對大家有用。

       為什么不使用GWT,事實上我正準備寫一篇關(guān)于“什么時候不要使用GWT”的博文,GWT能在某些應(yīng)用中表現(xiàn)在極其出色的性能,但始終有著它巨大的缺陷,在這里暫時不會對此發(fā)表長篇大論,如果大家有興趣,可在不久的將來在本博客中看到。

       為什么要寫關(guān)于Struts2的文章,是因為Struts1在大陸有著很成熟的應(yīng)用體系,相關(guān)的教程更是數(shù)不盡數(shù),Struts2雖然不是Struts1 的升級版,但在很多方面卻保留著Struts1的特性,但更引入了WebWork的優(yōu)秀性能,在此我絕不以貶義的態(tài)度看待Struts的借殼,反而我更贊 賞他們的態(tài)度,知道不行了就堅決改正,不管是不是把自己的老本都丟掉,如果為了面子問題而知錯不改,那才叫真正的死亡,類似的例子還有EJB3,與 EJB2有著天壤之別,將許多hibernate的持久化特性引入,雖然很多人都罵EJB3抄襲hibernate不是君子所為,但它所帶來的優(yōu)點卻是顯 而易見的,我依然十分欣賞它,雖然它己經(jīng)不是原來的它,進化才是硬道理。

       好了,不說費話了,下面看Maven2下Strtus2的配置實例。

       還是發(fā)揚風格,目錄結(jié)構(gòu)先貼出來

|--pom.xml

|--src

    |--main

        |--java

            |--com

                |--mydomain

                    |--HelloWorld.java

        |--resources

            |--struts.xml

        |--webapp

            |--index.jsp

            |--WEB-INF

                |--web.xml

在這里,我們只需寫一個java(HelloWorld.java)文件與一個簡單的jsp頁面,配置兩個文件(Struts.xml與web.xml),這幾個文件的代碼都將在后面貼出,同時還會先將pom.xml貼出

下面就是pom.xml

================================================

Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven./POM/4.0.0" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://maven./POM/4.0.0 http://maven./maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.aidress</groupId>  
  5.     <artifactId>web</artifactId>  
  6.     <packaging>war</packaging>  
  7.     <version>1.0-SNAPSHOT</version>  
  8.     <name>AiDress Web Project</name>  
  9.     <url>http://maven.</url>  
  10.     <dependencies>  
  11.         <dependency>  
  12.             <groupId>junit</groupId>  
  13.             <artifactId>junit</artifactId>  
  14.             <version>3.8.1</version>  
  15.             <scope>test</scope>  
  16.         </dependency>  
  17.   
  18.         <!--這里下面的兩個dependency, servlet-api與jsp-api所需-->  
  19.         <dependency>  
  20.             <groupId>javax.servlet</groupId>  
  21.             <artifactId>servlet-api</artifactId>  
  22.             <version>2.4</version>  
  23.         </dependency>  
  24.         <dependency>  
  25.             <groupId>javax.servlet.jsp</groupId>  
  26.             <artifactId>jsp-api</artifactId>  
  27.             <version>2.0</version>  
  28.             <scope>provided</scope>  
  29.         </dependency>  
  30.   
  31.         <!--要使用Struts2,這里只需struts2-core一個包即可,至于另外幾個所需的jar包,由于Struts2-->  
  32.   
  33.         <!--依賴于它們,所以不用在pom中顯示聲明,maven會自動幫你下載其它所需的包-->  
  34.         <dependency>  
  35.             <groupId>org.apache.struts</groupId>  
  36.             <artifactId>struts2-core</artifactId>  
  37.             <version>2.0.11.2</version>  
  38.         </dependency>  
  39.     </dependencies>  
  40.     <build>  
  41.         <finalName>web</finalName>  
  42.         <plugins>  
  43.             <plugin>  
  44.                 <artifactId>maven-compiler-plugin</artifactId>  
  45.                 <version>2.0.2</version>  
  46.                 <configuration>  
  47.                     <source>1.5</source>  
  48.                     <target>1.5</target>  
  49.                     <encoding>UTF-8</encoding>  
  50.                 </configuration>  
  51.             </plugin>  
  52.             <plugin>  
  53.                 <artifactId>maven-resources-plugin</artifactId>  
  54.                 <version>2.2</version>  
  55.                 <configuration>  
  56.                     <encoding>UTF-8</encoding>  
  57.                 </configuration>  
  58.             </plugin>  
  59.         </plugins>  
  60.     </build>  
  61. </project>  

 

 

下面是HelloWorld.java的源碼

==============================

Java代碼  收藏代碼
  1. import com.opensymphony.xwork2.ActionSupport;  
  2.   
  3. /** 
  4.  * 
  5.  * @author Dao 
  6.  */  
  7. public class HelloWorld extends ActionSupport  
  8. {  
  9.     private static final String MESSAGE = "Struts is up and running...";  
  10.     private String message;  
  11.       
  12.     public HelloWorld()  
  13.     {  
  14.           
  15.     }  
  16.       
  17.     @Override  
  18.     public String execute() throws Exception  
  19.     {  
  20.         setMessage(MESSAGE);  
  21.           
  22.         return SUCCESS;  
  23.     }  
  24.       
  25.     public void setMessage(String message)  
  26.     {  
  27.         this.message = message;  
  28.     }  
  29.       
  30.     public String getMessage()  
  31.     {  
  32.         return this.message;  
  33.     }  
  34. }  

 

 

下面是index.jsp

====================================

Java代碼  收藏代碼
  1. <%@ page contentType="text/html;charset=utf-8" %>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3. <html>  
  4.     <head>  
  5.         <title>  
  6.             第一個Struts2實例  
  7.         </title>  
  8.     </head>  
  9.     <body>  
  10.         <h2>  
  11.             <s:property value="message" />  
  12.         </h2>  
  13.     </body>  
  14. </html>   

 


 

下面是sturts.xml

====================================

Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <!--  
  4.     Document   : struts.xml  
  5.     Created on : September 17, 2008, 9:39 PM  
  6.     Author     : Dao  
  7.     Description:  
  8.         Purpose of the document follows.  
  9. -->  
  10.   
  11. <!DOCTYPE struts PUBLIC  
  12. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  13. "http://struts./dtds/struts-2.0.dtd">  
  14. <struts>  
  15.     <package name="tutorial" extends="struts-default">  
  16.         <action name="HelloWorld" class="com.mydomain.HelloWorld">  
  17.             <result>/index.jsp</result>  
  18.         </action>  
  19.     </package>  
  20. </struts>  
  21.       

 


下面是web.xml的內(nèi)容

=====================================

Xml代碼  收藏代碼
  1. <!DOCTYPE web-app PUBLIC  
  2.  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
  3.  "http://java./dtd/web-app_2_3.dtd" >  
  4.   
  5. <web-app>  
  6.   <display-name>AiDress Web Project</display-name>  
  7.   <filter>  
  8.       <filter-name>Struts2</filter-name>  
  9.       <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  10.   </filter>  
  11.   <filter-mapping>  
  12.       <filter-name>Struts2</filter-name>  
  13.       <url-pattern>/*</url-pattern>  
  14.   </filter-mapping>  
  15. </web-app>  

 

 

就這樣,運行mvn install可獲得一個web.war包,將其放于jboss(4.2以上版本才支持EJB3,所以建議使用4.2以上的jboss版本)的server/default/deploy目錄下,運行jboss

然后在網(wǎng)址上訪問

http://localhost:8080/web/HelloWorld.action

如果正常的話,你將會在網(wǎng)頁上看到下面一行字

Struts is up and running...



總結(jié):并未用到EJB。放在TOMCAT中應(yīng)該可以跑

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多