使用Maven搭建Struts2框架的开发环境

1、建立基于Maven的Web项目

  

  

  

  

  我使用的是MyEclipse8.5的版本,建立好的Web项目以下所示:java

  

  咱们知道,一个标准的Maven项目是必须包括【src/main/java】,【src/main/resources】,【src/test/java】,【src/test/resources】这四个Source Folder的,而建立好的项目当中只有一个(不懂为啥MyEclipse8.5没有帮我生成【src/main/java】),因此咱们还须要手动建立剩下的【src/main/java】,【src/test/java】,【src/test/resources】这三个Source Folder,以建立【src/main/java】为例,具体步骤以下:web

  

  

  点击【Finish】按钮完成建立,以下图所示:apache

  

  【src/test/java】,【src/test/resources】也是采用相同的方式进行建立,这里用一张动态的gif动画演示一下建立过程,以下图所示:浏览器

  

  最终效果以下:缓存

  

  这样咱们的【Struts2AnnotationMavenProject】项目才是一个标准的Maven项目,咱们可使用Maven来构建一下【Struts2AnnotationMavenProject】项目,看看可否正常构建成功,以下图所示:app

  

   从运行结果显示,项目能够正常构建成功。框架

2、搭建Struts2的开发环境

2.一、添加Struts2框架的核心jar包

  因为咱们是使用Maven管理项目中的jar包的,因此咱们须要在pom.xml文件中添加Struts2框架的核心jar包的描述。jsp

  编辑pom.xml文件,添加添加Struts2框架的核心jar包的描述,以下:maven

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 3   <modelVersion>4.0.0</modelVersion>
 4   <groupId>me.gacl</groupId>
 5   <artifactId>Struts2AnnotationMavenProject</artifactId>
 6   <packaging>war</packaging>
 7   <version>0.0.1-SNAPSHOT</version>
 8   <name>Struts2AnnotationMavenProject Maven Webapp</name>
 9   <url>http://maven.apache.org</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      <!-- Struts2的核心包 -->
18     <dependency>
19         <groupId>org.apache.struts</groupId>
20         <artifactId>struts2-core</artifactId>
21         <version>2.3.16</version>
22     </dependency>
23   </dependencies>
24   <build>
25     <finalName>Struts2AnnotationMavenProject</finalName>
26   </build>
27 </project>

  pom.xml文件中标红的部分就是咱们添加的Struts2框架的核心jar包的描述,保存pom.xml文件,此时Maven就会自动帮咱们把struts2-core这个jar包依赖的其余相关jar包导入到咱们的Web项目当中,以下图所示:学习

  

2.二、添加Struts2框架的配置文件struts.xml

  Maven约定,web项目开发中的使用到的配置文件都要放到【src/main/resources】这个Source Folder,以下图所示:

  

  编辑struts.xml文件,添加经常使用的配置项,配置信息以下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
 3 <struts>
 4     <!-- 全部匹配*.action的请求都由struts2处理 -->
 5     <constant name="struts.action.extension" value="action" />
 6     <!-- 是否启用开发模式 -->
 7     <constant name="struts.devMode" value="true" />
 8     <!-- struts配置文件改动后,是否从新加载 -->
 9     <constant name="struts.configuration.xml.reload" value="true" />
10     <!-- 设置浏览器是否缓存静态内容 -->
11     <constant name="struts.serve.static.browserCache" value="false" />
12     <!-- 请求参数的编码方式 -->
13     <constant name="struts.i18n.encoding" value="utf-8" />
14     <!-- 每次HTTP请求系统都从新加载资源文件,有助于开发 -->
15     <constant name="struts.i18n.reload" value="true" />
16     <!-- 文件上传最大值 -->
17     <constant name="struts.multipart.maxSize" value="104857600" />
18     <!-- 让struts2支持动态方法调用 -->
19     <constant name="struts.enable.DynamicMethodInvocation" value="true" />
20     <!-- Action名称中是否仍是用斜线 -->
21     <constant name="struts.enable.SlashesInActionNames" value="false" />
22     <!-- 容许标签中使用表达式语法 -->
23     <constant name="struts.tag.altSyntax" value="true" />
24     <!-- 对于WebLogic,Orion,OC4J此属性应该设置成true -->
25     <constant name="struts.dispatcher.parametersWorkaround" value="false" />
26 
27     <package name="basePackage" extends="struts-default">
28 
29 
30     </package>
31 
32 </struts>

2.三、convention-plugin和config-browser-plugin插件介绍

  之前用struts2框架开发项目时,每次编写好一个Action,就须要在struts.xml文件中配置Action,而convention-plugin这个插件的出现出现后,就再也不须要在struts.xml文件中配置Action了,convention-plugin提供了一种很是方便的注解方式来配置Action类。

  convention-plugin采用"约定大于配置”的思想,只要咱们遵照约定,彻底能够少写配置甚至不写配置;config-browser-plugin插件则用于方便的浏览项目中的全部action及其与 jsp view的映射。这二个插件结合起来学习,能很方便的搞定struts2中各类复杂的action-view映射需求,因此如今使用Struts2框架开发Web应用时,通常都会配合这两个插件一块儿使用。

2.3.一、convention-plugin和config-browser-plugin插件使用

  为了方便使用Struts2框架,咱们最好在项目中配合convention-plugin、 config-browser-plugin这两个插件一块儿使用,在maven项目的pom.xml中加上这两个插件的jar包描述,以下:

 1 <!-- convention-plugin插件,使用了这个插件以后,就能够采用注解的方式配置Action -->
 2     <dependency>
 3         <groupId>org.apache.struts</groupId>
 4         <artifactId>struts2-convention-plugin</artifactId>
 5         <version>2.3.20</version>
 6     </dependency>
 7     <!--config-browser-plugin插件,使用了这个插件以后,就能够很方便的浏览项目中的全部action及其与 jsp view的映射-->
 8     <dependency>
 9         <groupId>org.apache.struts</groupId>
10         <artifactId>struts2-config-browser-plugin</artifactId>
11         <version>2.3.20</version>
12     </dependency>

  

2.四、在web.xml文件中配置Struts2的核心过滤器

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7 
 8     <display-name>Archetype Created Web Application</display-name>
 9 
10     <!-- add struts2 configiguration -->
11     <filter>
12         <description>配置struts2的核心过滤器</description>
13         <filter-name>struts2</filter-name>
14         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
15     </filter>
16 
17     <filter-mapping>
18         <filter-name>struts2</filter-name>
19         <url-pattern>*.action</url-pattern>
20     </filter-mapping>
21     <!-- end add struts2 configuration-->
22 </web-app>

2.五、测试Struts2框架是否搭建成功

  编写一个TestAction类,用于测试

 1 package me.gacl.action;
 2 
 3 import org.apache.struts2.convention.annotation.Action;
 4 import org.apache.struts2.convention.annotation.Namespace;
5 @ParentPackage("basePackage") 6 //使用convention插件提供的@Action注解将一个普通Java类标识为能够处理用户请求的Action类 7 @Action 8 //使用使用convention插件提供的@Namespace注解指明这个Action类的命名空间 9 @Namespace("/") 10 public class TestAction { 11 /** 12 * test方法的访问方式:http://localhost:8080/Struts2AnnotationMavenProject/test!test 13 * MethodName: test 14 * Description: 15 * @author xudp 16 */ 17 public void test(){ 18 System.out.println("调用了TestAction里面的test方法"); 19 } 20 }

  在将类使用@Action注解标识时发现@Action注解必须使用JDK1.6才行,因此我修改了JDK的版本,改为使用JDK1.6的,以下图所示:

  

  因为使用了convention-plugin插件的提供的注解方式映射Action的访问路径,因此咱们再也不须要像之前那样在Struts.xml文件中配置Action的访问路径了,测试结果以下:

  

  能够看到,咱们的TestAction里面的test方法已经成功调用了,这说明咱们的Struts2框架的开发环境搭建成功。而且咱们也感觉到了使用convention-plugin插件开发基于注解的Struts2程序是很是方便和快捷的。

  咱们的项目中还使用到了config-browser-plugin插件,下面咱们来感觉一下这个config-browser-plugin插件带来的便利之处

  输入访问URL:http://localhost:8080/项目名/config-browser/index.action来访问config-browser-plugin插件提供的视图页面,例如:http://localhost:8080/Struts2AnnotationMavenProject/config-browser/index.action,这样就能够进入config-browser-plugin插件提供的视图界面,以下图所示:

  

  以上就是在MyEclipse中使用Maven搭建Struts2框架的开发环境的相关介绍,使用了Maven以后,大大提升了框架开发环境的搭建速度,起码咱们再也不须要关心Struts2框架的开发环境须要哪些jar包,咱们只须要引入Struts2的核心jar包struts2-core,而后Maven就会自动帮咱们把struts2-core这个jar包的相关依赖jar所有加入到项目中,将web应用中的jar包所有交给Maven进行管理是一种很是好的作法,如今主流的项目都是采用Maven进行构建的。

相关文章
相关标签/搜索