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

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

2016-05-05 15:04 483 查看

一、创建基于Maven的Web项目

  

  

  

  

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

  

  我们知道,一个标准的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】为例,具体步骤如下:

  

  

  点击【Finish】按钮完成创建,如下图所示:

  

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

  


  最终效果如下:

  

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

  


   从运行结果显示,项目可以正常构建成功。

二、搭建Struts2的开发环境

2.1、添加Struts2框架的核心jar包

  由于我们是使用Maven管理项目中的jar包的,所以我们需要在pom.xml文件中添加Struts2框架的核心jar包的描述。

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



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.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.3、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.1、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.4、在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.5、测试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进行构建的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: