歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Struts2 convention-plugin 使用筆記

Struts2 convention-plugin 使用筆記

日期:2017/3/1 9:56:12   编辑:Linux編程

最近又在學習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>

Copyright © Linux教程網 All Rights Reserved