Struts2 convention-plugin 使用笔记


最近又在学习struts2 ,之前虽然使用过,但是也只是使用,struts2 的很多功能根本就没去了解过,现在决定学习一下,暂不要求有多深入。所以如果您需要的是一个高级教程,那么请转车。。。此次列车不去那里。

系统环境如下

  • eclipse 3.6
  • tomcat 6
  • JDK 6
  • Ubuntu 12.04 LTS

struts 版本为2.3.4.1,在 struts2的官方文档中讲到

The Convention Plugin is bundled with Struts since 2.1 and replaces the Codebehind Plugin and Zero Config plugins.

在struts2.1 之后,已经使用Convention Plugin替换了CodeBehind,如果您使用的版本与我使用的版本不一致的话,请注意这里的区别以及由此引起的配置差异。

需要使用Jar包如下(在struts2的lib包下都有,粘贴复制就OK了)

  • asm-3.3.jar
  • asm-commons-3.3.jar
  • commons-fileupload-1.2.2.jar
  • commons-io-2.0.1.jar
  • commons-lang3-3.1.jar
  • freemarker-2.3.19.jar
  • javassist-3.11.0.GA.jar
  • ognl-3.0.5.jar
  • struts2-convention-plugin-2.3.4.1.jar
  • struts2-core-2.3.4.1.jar
  • xwork-core-2.3.4.1.jar

这些是我发现的最少包配置,至少我在删除这些包的时候,总会出现各种各样的错误的。也许你长的比较帅气(性感) struts会卖你面子不报错的 大笑

这里要注意javassist-3.11.0.GA.jar 这个包,如果删除这个包程序会报错无法启动,因为ongl这个包依赖它,而其他的某一个包依赖ongl,所以这两个包是不能删掉的。

首先用eclipse 新建一个 动态web项目 这一步不需要多说,相信大家都会的,不会的话那么、这个、那个,你去百度一下吧.新建完成后,将以上各包复制进WEB-INF 下面的lib包下,build path。

在src目录下新建一个xml,我这里名字叫做struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.convention.classes.reload" value="true" /> 
 <constant name="struts.devMode" value="true" />
<!-- Convention Plugin  扫描注解时候的跟路径,根据你的项目包结构做出改变 -->
&nbsp;<constant name="struts.convention.package.locators.basePackage" value="struts"/> 
<!--Convention Plugin  扫描注解的最基础包  我这里actions包存放的是项目的action
Convention Plugin默认会去扫描所以继承了ActionSupport 和名字以Action结尾的文件并将其作为一个Action;这两个配置非必须,我发现我就算将其注释掉后程序依然可以运行。
&nbsp;-->
 <constant name="struts.convention.package.locators" value="actions"/> 
</struts>

如果需要可以新建一个struts.properties文件,根据文档来讲,和struts.xml 是差不多的,

 All properties can also be set using Constant Configuration in an XML configuration file.

这个文件提供以K-V模式配置,

在WEB-INF 下的web.xm文件我的配置如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <display-name>Struts</display-name>
 <filter>
              <!-- 注意这个filterclass  ,-->
                <filter-name>action</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
                <!-- 如果你的struts版本<2.1.3  那么请使用 这个filter<span class="code-tag">                  <filter-class></span>org.apache.struts2.dispatcher.FilterDispatcher<span class="code-tag"></filter-class></span>
 -->

 <!-- 这里也是定义action所在包,我将其注释掉程序可以运行,不过如果你的包结构比较复杂,还是建议将这个配置上 <init-param><param-name>actionPackage</param-name><param-value>struts.action</param-value></init-param> --></filter><filter-mapping><filter-name>action</filter-name><url-pattern>/*</url-pattern></filter-mapping><jsp-config><taglib><taglib-uri>/s</taglib-uri><taglib-location>/WEB-INF/struts-tags.tld</taglib-location></taglib></jsp-config><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list></web-app>

  • 1
  • 2
  • 下一页

相关内容