學(xué)習(xí)筆記 Eclipse Struts2 Examples-創(chuàng)建web項(xiàng)目

文章摘要
1、如何創(chuàng)建一個(gè)Struts 2 Web應(yīng)用程序
2、配置struts 2 jar
3、添加Struts 2 Servlet過濾器
4、配置struts.xml,配置URL以及java類等的關(guān)系
5、構(gòu)建并運(yùn)行


英文文獻(xiàn)請(qǐng)點(diǎn)擊此處~

一、前提

Struts 2需要Servlet API 2.4或更高版本,JSP 2.0或更高版本,以及Java 7或更高版本。

二、創(chuàng)建第一個(gè)Maven程序

要開始使用Struts 2,我們將使用Maven創(chuàng)建一個(gè)Web應(yīng)用程序來管理工件依賴關(guān)系。
您可以通過struts-examples從Struts 2 GitHub倉庫中檢出所有示例應(yīng)用程序。

  • 1、點(diǎn)擊新建-選擇Maven項(xiàng)目:
  • 2、選定webapp
  • 3、設(shè)定Group Id、Artifact Id等信息。


  • 4、創(chuàng)建完畢,工程目錄結(jié)構(gòu)如下


  • 5、配置pom.xml
    要使用maven運(yùn)行應(yīng)用程序,請(qǐng)將jetty maven-plugin添加到您的pom.xml中

<build>
    <finalName>basic-struts</finalName>
    <plugins>
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>8.1.16.v20140903</version>
            <configuration>
                <webApp>
                    <contextPath>/${build.finalName}</contextPath>
                </webApp>
                <stopKey>CTRL+C</stopKey>
                <stopPort>8999</stopPort>
                <scanIntervalSeconds>10</scanIntervalSeconds>
                <scanTargets>
                    <scanTarget>src/main/webapp/WEB-INF/web.xml</scanTarget>
                </scanTargets>
            </configuration>
        </plugin>
    </plugins>
  </build>

以上插件將使您能夠運(yùn)行應(yīng)用程序 mvn jetty:run
至此,web工程創(chuàng)建完畢。

三、配置index.jsp

我們的下一步是index.jsp,為此Web應(yīng)用程序添加一個(gè)簡(jiǎn)單的內(nèi)容。創(chuàng)建index.jsp下src/main/webapp 用的標(biāo)題基本的Struts 2的應(yīng)用-歡迎,并在body的H1標(biāo)題添加歡迎的Struts 2!
此處我們配置為HelloWorld。

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

運(yùn)行mvn jetty:run運(yùn)行應(yīng)用程序。
在瀏覽器瀏覽:http://localhost:8080/basic-struts/index.jsp,可以看到結(jié)果。

四、將Struts 2 Jar文件添加到類路徑

現(xiàn)在我們知道我們有一個(gè)工作的Java Web應(yīng)用程序,可以將最少的必需Struts 2框架Jar文件添加到Web應(yīng)用程序的類路徑中。在pom.xml添加下面的依賴節(jié)點(diǎn):

  <dependencies>
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-core</artifactId>
        <version>2.5.12</version>
    </dependency>
  </dependencies>

當(dāng)然,用當(dāng)前的Struts 2版本替換XXXX。Maven將獲取struts2-corejar和其他jar文件struts2-core所需要的(傳遞依賴項(xiàng))。

五、添加日志記錄

<dependencies>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.8.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.8.2</version>
    </dependency>
  </dependencies>

使用兩者log4j-core并log4j-api允許使用最新版本的Log4j2,而不會(huì)與框架提供的版本發(fā)生沖突。log4j2.xml在src/main/resources包含以下內(nèi)容的文件夾中設(shè)置配置
log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="com.opensymphony.xwork2" level="debug"/>
        <Logger name="org.apache.struts2" level="debug"/>
        <Root level="warn">
            <AppenderRef ref="STDOUT"/>
        </Root>
    </Loggers>
</Configuration>

注意上面的log4j2配置將控制臺(tái)指定為日志目標(biāo)。

六、 添加Struts 2 Servlet過濾器

web.xml

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

七、配置struts.xml

Struts 2可以使用XML配置文件或注釋(或兩者)來指定URL、Java類和視圖頁(例如index.jsp)之間的關(guān)系。對(duì)于我們基本的Struts 2應(yīng)用程序,我們將使用最小的xml配置。注意文件名是struts.xml,它應(yīng)該在src/main/resources文件夾中(struts.xml 必須在Web應(yīng)用程序的根類路徑上)。

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>

    <constant name="struts.devMode" value="true" />

    <package name="basicstruts2" extends="struts-default">
        <action name="index">
            <result>/index.jsp</result>
        </action>
    </package>

</struts>

這個(gè)最小的Struts 2配置文件告訴框架,如果URL結(jié)束,index.action將瀏覽器重定向到index.jsp。

有關(guān)struts.xml配置文件的更多信息,請(qǐng)參見struts.xml。

八、構(gòu)建并運(yùn)行應(yīng)用程序

運(yùn)行mvn jetty:run使用jetty maven-plugin運(yùn)行Web應(yīng)用程序。

查看控制臺(tái),您應(yīng)該看到許多調(diào)試消息,告訴您Struts 2框架正在包含在basic-struts2 Web應(yīng)用程序中。打開一個(gè)Web瀏覽器,然后轉(zhuǎn)到 http://localhost:8080/basic-struts/index.action (注意index.action不在index.jsp URL的末尾)。您將看到與訪問 http://localhost:8080/basic-struts/index.jsp時(shí)相同的網(wǎng)頁 。

Struts 2日志消息:

2017-07-17 18:12:13,506 WARN Worker-73 org.eclipse.m2e.core.internal.embedder.EclipseLogger - Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
2017-07-17 18:12:13,511 WARN Worker-73 org.eclipse.m2e.core.internal.embedder.EclipseLogger - Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容