您的位置:首页 > 编程语言 > Java开发

如何创建一个Struts 2 Web应用

2018-01-13 21:56 357 查看
   翻译自Struts官方文档,原文地址:点击打开链接

   本教程将一步一步的教你如何使用Struts2 框架创建一个简单的Web应用。

   虽然Struts 2框架使用很简单,但是想要创建一个复杂的Web应用,你最好有J2EE基础知识和技术,包括:

Java
Filters, JSP, and Tag Libraries
JavaBeans
HTML and HTTP
Web Containers (such as Tomcat)
XML
                    想要学习更多基础技术,请看关键技术入门

Java相关环境要求

       Servlet API 2.4+, JSP 2.0+ , Java 7+.


我们的第一个Struts Web应用

       接下来,我们将使用Struts 2创建一个Web应用,使用Maven作为依赖管理工具。你可以去Struts 2 GitHub仓库struts-examples 查看或下载所有示例程序。


创建一个Struts 2 Web应用,使用Maven管理依赖和构建这个项目。

这个教程假设你已经知道如何创建一个Java web应用,并且会使用Maven管理项目依赖和构建Web应用。这儿有一篇关于如何使用Eclipse创建一个Maven工程的文章


步骤 1 - 创建一个Java Web项目

     
根据Maven项目标准文件夹结构,在你的Java IDE中创建一个Java Web项目(Maven项目),并命名为basic_struts。在你项目的pom.xml中将包含下面的内容:

pom.xml

<build>
<finalName>basic-struts</finalName>
</build>


     添加Jetty maven插件到pom.xml.

      pom.xml jetty 插件

<build>
...
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.7.v20170914</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 命令来运行你的项目。


步骤2 - 添加 index.jsp

       我们下一步将在项目中添加一个简单的index.jsp文件.在src/mian/webapp下创建一个index.jsp文件,并以Basic Struts 2 Application作为标题,在页面中添加一个h1标签,内容为Welcome to Struts 2!。

     index.jsp

<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
<meta charset="UTF-8">
<title>Basic Struts 2 Application - Welcome</title>
</head>
<body>
<h1>Welcome To Struts 2!</h1>
</body>
</html>


       使用mvn jetty:run 来运行这个项目。

       在你的浏览器中输入 http://localhost:8080/basic-struts/index.jsp.你将看到下面的页面:




步骤 3 - 添加 Struts 2 Jar 包 到 Class Path

      现在我们已经能够运行这个Java Web项目了,接下来添加Struts 2框架所需的Jar包到项目Class Path中。我们只需在pom.xml中添加一下依赖:

      pom.xml 依赖

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts2.version}</version>
</dependency>


   当然你需要使用当前Struts 2版本去替换掉 
${struts2.version}
  (或者在pom中 定义一个 
properties
).
Maven将自动下载 
struts2-core
jar包 和其依赖包 (传递性依赖).

    从Struts2 2.2.3版本开始,不再需要单独引入javassist依赖。


步骤 4 - 添加 日志

     要想 查看Struts2项目运行的流程,需要添加log4j2日志。在你项目的src/main/resources下创建一个
log4j2.xml
 文件,按照下面的内容去配置log4j2:

    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配置指定控制台作为日志的输出目标。

   你需要添加log4j2依赖到pom.xml文件中:

pom.xml log4j 依赖

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>


     可以使用log4j-core 和 log4j-api的最新版本,这并不会和Struts 2框架提供的log4依赖冲突。

      通常,我们会使用Maven bom在dependencyManagement标签中包含Struts
和 log4j2依赖,像下面那样。这样你就可以在每个使用的依赖模块配置中省略version 这一行的配置,
并且 
struts2-*
 和 
log4j-*
 所有的模块都能够使用一致的版本。
struts2-bom
 从2.3.20版本开始可以使用.

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>

<struts2.version>2.5.14.1</struts2.version>
<log4j2.version>2.10.0</log4j2.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-bom</artifactId>
<version>${struts2.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-bom</artifactId>
<version>${log4j2.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!-- Struts 2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
</dependency>

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



步骤 5 - 添加 Struts 2 Servlet 过滤器

       启用Struts 2 框架需要在web.xml中配置一个Servlet过滤器。下面展示如何在web.xml中配置Struts
2核心过滤器。web.xml文件在src/main/webapp/WEB-INF下:

web.xml Servlet Filter

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Basic Struts2</display-name>
<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>

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

</web-app>


     获取更多关于Struts 2部署描述符的信息,请看Core Developers Guide / web.xml page. 注意,url-pattern节点值为 
/*
 这意味着Struts
2过滤器将拦截这个应用所有的请求。


步骤 6 - 创建 struts.xml

      Struts 2 不仅可以使用XML 配置还可以使用注解配置(或者两者混合使用)来表示URL、Java类和视图页面(例如index.jsp)之间的关系。对于基础Struts2 的应用,我们将采用最小化配置。 注意:这个文件的文件名是 
struts.xml
 并且它应该放在 
src/main/resources
 文件夹下(
struts.xml
 文件必须放在web应用的根class
path下,也就是WEB-INF/classes路径下。默认的Maven在打包部署的时候会将src/main/resources下的资源文件拷贝到WEB-INF/classes下).

     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
.


步骤 7 - 构建并运行应用

       使用jetty Maven插件(打开终端,进入到项目目录下)运行
mvn jetty:run
 命令,去运行这个web应用。

       在控制台将会看到很多的调试信息,意思是告诉你Struts 2框架已经被包含在basic-struts2 web应用中了。

       打开浏览器输入 http://localhost:8080/basic-struts/index.action (注意:URL结尾是
index.action
 不是 
index.jsp
).
你将看到和访问 http://localhost:8080/basic-struts/index.jsp.一样的页面。 查看控制台输出,你应该可以看到关于
index.action
 and 
index.jsp
的消息。

Struts 2 日志信息

...
2017-04-17 11:16:01,084 DEBUG [qtp1723848804-22] xwork2.DefaultActionProxy (DefaultActionProxy.java:89) - Creating an DefaultActionProxy for namespace [/] and action name [index]
...
2017-04-17 11:16:01,172 DEBUG [qtp1723848804-22] result.ServletDispatcherResult (ServletDispatcherResult.java:131) - Forwarding to location: /index.jsp
...



获取帮助

如果你在使用教程的示例程序时遇到问题,你可以去Struts 2 用户问题讨论区 找答案,如果在这没有找到你需要的答案,你可以在这里发布你的问题。
上一篇Struts 2快速入门or下一篇
Struts 2 Hello World
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: