本人準(zhǔn)備在將來的日子里發(fā)布一部Maven2 + Struts2 + EJB3的實(shí)例系列,希望對(duì)大家有用。
為什么不使用GWT,事實(shí)上我正準(zhǔn)備寫一篇關(guān)于“什么時(shí)候不要使用GWT”的博文,GWT能在某些應(yīng)用中表現(xiàn)在極其出色的性能,但始終有著它巨大的缺陷,在這里暫時(shí)不會(huì)對(duì)此發(fā)表長篇大論,如果大家有興趣,可在不久的將來在本博客中看到。
為什么要寫關(guān)于Struts2的文章,是因?yàn)镾truts1在大陸有著很成熟的應(yīng)用體系,相關(guān)的教程更是數(shù)不盡數(shù),Struts2雖然不是Struts1
的升級(jí)版,但在很多方面卻保留著Struts1的特性,但更引入了WebWork的優(yōu)秀性能,在此我絕不以貶義的態(tài)度看待Struts的借殼,反而我更贊
賞他們的態(tài)度,知道不行了就堅(jiān)決改正,不管是不是把自己的老本都丟掉,如果為了面子問題而知錯(cuò)不改,那才叫真正的死亡,類似的例子還有EJB3,與
EJB2有著天壤之別,將許多hibernate的持久化特性引入,雖然很多人都罵EJB3抄襲hibernate不是君子所為,但它所帶來的優(yōu)點(diǎn)卻是顯
而易見的,我依然十分欣賞它,雖然它己經(jīng)不是原來的它,進(jìn)化才是硬道理。
好了,不說費(fèi)話了,下面看Maven2下Strtus2的配置實(shí)例。
還是發(fā)揚(yáng)風(fēng)格,目錄結(jié)構(gòu)先貼出來
|--pom.xml
|--src
|--main
|--java
|--com
|--mydomain
|--HelloWorld.java
|--resources
|--struts.xml
|--webapp
|--index.jsp
|--WEB-INF
|--web.xml
在這里,我們只需寫一個(gè)java(HelloWorld.java)文件與一個(gè)簡單的jsp頁面,配置兩個(gè)文件(Struts.xml與web.xml),這幾個(gè)文件的代碼都將在后面貼出,同時(shí)還會(huì)先將pom.xml貼出
下面就是pom.xml
================================================
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.aidress</groupId>
- <artifactId>web</artifactId>
- <packaging>war</packaging>
- <version>1.0-SNAPSHOT</version>
- <name>AiDress Web Project</name>
- <url>http://maven.</url>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
-
-
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.4</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet.jsp</groupId>
- <artifactId>jsp-api</artifactId>
- <version>2.0</version>
- <scope>provided</scope>
- </dependency>
-
-
-
-
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-core</artifactId>
- <version>2.0.11.2</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>web</finalName>
- <plugins>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.0.2</version>
- <configuration>
- <source>1.5</source>
- <target>1.5</target>
- <encoding>UTF-8</encoding>
- </configuration>
- </plugin>
- <plugin>
- <artifactId>maven-resources-plugin</artifactId>
- <version>2.2</version>
- <configuration>
- <encoding>UTF-8</encoding>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
下面是HelloWorld.java的源碼
==============================
- import com.opensymphony.xwork2.ActionSupport;
-
-
-
-
-
- public class HelloWorld extends ActionSupport
- {
- private static final String MESSAGE = "Struts is up and running...";
- private String message;
-
- public HelloWorld()
- {
-
- }
-
- @Override
- public String execute() throws Exception
- {
- setMessage(MESSAGE);
-
- return SUCCESS;
- }
-
- public void setMessage(String message)
- {
- this.message = message;
- }
-
- public String getMessage()
- {
- return this.message;
- }
- }
下面是index.jsp
====================================
- <%@ page contentType="text/html;charset=utf-8" %>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <html>
- <head>
- <title>
- 第一個(gè)Struts2實(shí)例
- </title>
- </head>
- <body>
- <h2>
- <s:property value="message" />
- </h2>
- </body>
- </html>
下面是sturts.xml
====================================
- <?xml version="1.0" encoding="UTF-8"?>
-
- <!--
- Document : struts.xml
- Created on : September 17, 2008, 9:39 PM
- Author : Dao
- Description:
- Purpose of the document follows.
- -->
-
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts./dtds/struts-2.0.dtd">
- <struts>
- <package name="tutorial" extends="struts-default">
- <action name="HelloWorld" class="com.mydomain.HelloWorld">
- <result>/index.jsp</result>
- </action>
- </package>
- </struts>
-
下面是web.xml的內(nèi)容
=====================================
- <!DOCTYPE web-app PUBLIC
- "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java./dtd/web-app_2_3.dtd" >
-
- <web-app>
- <display-name>AiDress Web Project</display-name>
- <filter>
- <filter-name>Struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>Struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </web-app>
就這樣,運(yùn)行mvn install可獲得一個(gè)web.war包,將其放于jboss(4.2以上版本才支持EJB3,所以建議使用4.2以上的jboss版本)的server/default/deploy目錄下,運(yùn)行jboss
然后在網(wǎng)址上訪問
http://localhost:8080/web/HelloWorld.action
如果正常的話,你將會(huì)在網(wǎng)頁上看到下面一行字
Struts is up and running...
總結(jié):并未用到EJB。放在TOMCAT中應(yīng)該可以跑