程序员JSPMaven

学习笔记 Eclipse Struts2 Examples-创建

2017-07-17  本文已影响0人  Android那些事儿

文章摘要
1、如何创建一个Struts 2 Web应用程序
2、配置struts 2 jar
3、添加Struts 2 Servlet过滤器
4、配置struts.xml,配置URL以及java类等的关系
5、构建并运行


英文文献请点击此处~

一、前提

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

二、创建第一个Maven程序

要开始使用Struts 2,我们将使用Maven创建一个Web应用程序来管理工件依赖关系。
您可以通过struts-examples从Struts 2 GitHub仓库中检出所有示例应用程序。

<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>

以上插件将使您能够运行应用程序 mvn jetty:run
至此,web工程创建完毕。

三、配置index.jsp

我们的下一步是index.jsp,为此Web应用程序添加一个简单的内容。创建index.jsp下src/main/webapp 用的标题基本的Struts 2的应用-欢迎,并在body的H1标题添加欢迎的Struts 2!
此处我们配置为HelloWorld。

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

运行mvn jetty:run运行应用程序。
在浏览器浏览:http://localhost:8080/basic-struts/index.jsp,可以看到结果。

四、将Struts 2 Jar文件添加到类路径

现在我们知道我们有一个工作的Java Web应用程序,可以将最少的必需Struts 2框架Jar文件添加到Web应用程序的类路径中。在pom.xml添加下面的依赖节点:

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

当然,用当前的Struts 2版本替换XXXX。Maven将获取struts2-corejar和其他jar文件struts2-core所需要的(传递依赖项)。

五、添加日志记录

<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,而不会与框架提供的版本发生冲突。log4j2.xml在src/main/resources包含以下内容的文件夹中设置配置
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配置将控制台指定为日志目标。

六、 添加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)之间的关系。对于我们基本的Struts 2应用程序,我们将使用最小的xml配置。注意文件名是struts.xml,它应该在src/main/resources文件夹中(struts.xml 必须在Web应用程序的根类路径上)。

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>

这个最小的Struts 2配置文件告诉框架,如果URL结束,index.action将浏览器重定向到index.jsp。

有关struts.xml配置文件的更多信息,请参见struts.xml。

八、构建并运行应用程序

运行mvn jetty:run使用jetty maven-plugin运行Web应用程序。

查看控制台,您应该看到许多调试消息,告诉您Struts 2框架正在包含在basic-struts2 Web应用程序中。打开一个Web浏览器,然后转到 http://localhost:8080/basic-struts/index.action (注意index.action不在index.jsp URL的末尾)。您将看到与访问 http://localhost:8080/basic-struts/index.jsp时相同的网页 。

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!
上一篇下一篇

猜你喜欢

热点阅读