`

三、改变struts.xml默认路径后web.xml如何配置

阅读更多

在struts2框架中struts.xml是应当放到src的根目录,程序编译后会将struts.xml放到WEB-INF/classes目录下。

Struts2在web.xml中的一般配置如下:

<filter>
        <filter-name>struts2</filter-name>
        <filter-class>
             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>        
</filter>
<filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>

根据配置就会先后加载struts-default.xml、struts-plugin.xml、然后自动加载classes目录下的struts.xml 。

但是有时为了协作开发或方便管理,有时可能需要把struts.xml放到其他位置,如下图所示:

这个时候web.xml文件该如何配置呢?

<filter>
        <filter-name>struts2</filter-name>
        <filter-class>
              org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
        <init-param>
        	<param-name>config</param-name>
        	<param-value>
                    struts-default.xml,config/struts2/struts.xml
                </param-value>
        </init-param>        
</filter>
<filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>

我们可以通过上述web.xml的配置找到struts.xml文件的位置,但在这里有人可能会疑问了,那为什么要struts-default.xml也需要指定呢?因为我们改变了struts.xml文件的位置后,如果不手动指定,就不会自动加载。就会出现下面的异常:

 


 

所以需要特别注意struts2默认加载的文件,如struts-default.xml,struts-plugin.xml都需要在

<param-value/>中重新指定一下。

 

温馨提示:在上述的程序中,大家可能注意我重新指定了struts-default.xml而没有写struts-plugin.xml,那是因为我这个项目没有用到插件,引用的是项目的基本jar,基本Jar中是没有struts-plugin.xml配置文件的。这个配置文件是在插件jar包中。如图:

 


 任意解压缩一个Jar包即可发现:

 


所以我们需要清楚虽然配置文件的加载顺序是
struts-default.xml、struts-plugin.xml、struts.xml,但是如果没有插件的应用那么就是struts-default.xml、struts.xml的顺序。这就是上述显性指定时没有指定struts-plugin.xml的原因。

 

当然如果不想在web.xml文件中指定struts-default.xml,struts-plugin.xml的话,也可在struts.xml文件中通过include将原有配置文件包含进来。

<struts>	
    <include file="struts-default.xml" ></include>	
    <package name="example" namespace="/" extends="struts-default">
        <action name="login" class="com.yanln.test.action.LoginAction">
            <result name="success">success.jsp</result>           
        </action>
    </package>
</struts>

 

注意:每个人的编程习惯是不一样的,有的人可能把配置文件习惯如下方式放置


 那么web.xml文件可以这样配置:

 

<filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
        <init-param>
        	<param-name>config</param-name>
        	<param-value>
                     struts-default.xml,../config/struts2/struts.xml
                </param-value>        	
        </init-param>        
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

 主要原因是struts2默认会到WEB-INF/classes目录下去找配置文件。

 

 

 
 
 

  • 大小: 66.6 KB
  • 大小: 102.4 KB
  • 大小: 65.3 KB
  • 大小: 80.8 KB
  • 大小: 47.2 KB
  • 大小: 19.8 KB
分享到:
评论

相关推荐

    struts2的rest风格插件实例

    通过 struts.xml、struts.properties 或 web.xml 改变 struts.convention.default.parent.package 常量的值,让支持 REST 风格的 Action 所在的包默认继承 rest-default,而不是继承默认的 convention-default 父包...

    Struts2属性文件详解

    struts.configuration.xml.reload 该属性设置当struts.xml文件改变后,系统是否自动重新加载该文件.该属性的默认值是false. struts.velocity.configfile 该属性指定Velocity框架所需的velocity.properties文件的...

    struts2零配置个人整理文档

    默认所有的结果页面都存储在WEB-INF/content下,你可以通过设置struts.convention.result.path这个属性的值来改变到其他路径。如: Xml代码 &lt;constant name="struts.convention.result.path" value="/WEB-INF/page" ...

    Struts2\constant应用

    &lt;constant name="struts.configuration.xml.reload" value="true" /&gt; &lt;!-- 开发模式下使用,这样可以打印出更详细的错误信息 --&gt; &lt;constant name="struts.devMode" value="true" /&gt; &lt;!-- 默认的视图主题 --&gt; ...

    java考试系统

    (C) FilterDispatcher类中的init()方法中将会读取类路径下默认的配置文件struts.xml完成初始化操作 (D) struts2读取到struts.xml的内容后,会将内容封装进Action对象中并存放在内存。 ~ 3.在Struts中提供了多种返回...

    Myeclipse开发struts+hibernate+spring新手入门--环境配置---项目开发示例

    Struts config path:默认,一般不需要改变。 Struts specification:选择Struts版本,在此项目中,选择Struts 1.2版本。 Base Package for new classes:设置为空。 取消Install Struts TLDs选项。 其他默认: ...

    SpringStrutsHibernate的配置

    改变web.xml的默认文件名:(一般默认引入的文件名,我们不改) http://blog.csdn.net/qq413041153/article/details/7922961 web.xml文件配置: <servlet> ①HelloWorld</servlet-name> ②...

    struts2注解详细说明

    需要在你项目的struts.xml中添加如下配置    name="struts.convention.action.suffix" value="Controller"/&gt;   name="struts.convention.action.mapAllMatches" value="true"/&gt;   name="struts.convention....

    java命名规范 开发规范

    b. 配置struts-moduleName.xml; 直接跳转示例; 调用Service示例; c. 开发对应的{EntityName}Action类; 该类必须继承com.hikvision.swdf.xx.BaseAction&lt;{EntityName}&gt;,该Action类有一个关键属性entity,即泛型...

    Roller4.0的另外一套cas简单解决方案

    首先修改security.xml. 修改authenticationProcessingFilterEntryPoint节点为下面: &lt;bean id="authenticationProcessingFilterEntryPoint" class="org.acegisecurity.ui.webapp....

    ssh(structs,spring,hibernate)框架中的上传下载

    WEB-INF下的applicationContext.xml为Spring的配置文件,struts-config.xml为Struts的配置文件,file-upload.jsp为文件上传页面,file-list.jsp为文件列表页面。  本文后面的章节将从数据持久层->业务层->Web层的...

    webx3框架指南PDF教程附学习Demo

    1.2.1. 三个大层次 ..................................................................................... 6 1.2.2. 剪裁和定制Webx .........................................................................

    Spring中文帮助文档

    2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 ...

    Spring API

    2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 ...

    spring chm文档

    2.4.1. 在XML里更为简单的声明性事务配置 2.4.2. JPA 2.4.3. 异步的JMS 2.4.4. JDBC 2.5. Web层 2.5.1. Spring MVC的表单标签库 2.5.2. Spring MVC合理的默认值 2.5.3. Portlet 框架 2.6. 其他特性 2.6.1. ...

    Struts2 in action中文版

    第1章 Struts 2:现代Web框架 2 1.1 Web应用程序:快速学习 2 1.1.1 构建Web应用程序 2 1.1.2 基础技术简介 3 1.1.3 深入研究 6 1.2 Web应用程序框架 7 1.2.1 什么是框架 7 1.2.2 为什么使用框架 8 1.3 Struts 2框架...

    jsoup jar包

    通过文件decorators.xml可以看到,我配置了只对可以匹配上/user*的url请求进行装饰,因为存在url改写,所以对于被装饰的文件原路径无关,装饰是针对url的.比如我访问url为:...

    公文传输系统velocity struts spring hibernate(lib太大只发截图自己去下)

    Tomcat服务器默认安装后使用8080端口,打开浏览器输入以下URL地址访问公文传输系统。 http://localhost:8080/GWCS/index.jsp 注意:如果改变Tomcat服务器端口,访问地址也要相应变更。 管理员用户名: admin 管理员...

    BOS 技术整理

    web.xml 配置 spring 监听器 src/main/resource 下 建立 applicationContext.xml rc/main/resource 提供 config.properties 外部属性文件、log4j.properties 日志配置文件 配置文件如下: ...

    Faceye 基础版概框

    (注:默认部署后既解包,如没有解包,请首先解包) 4.4.数据库连接配置 打开:x:/tomcat/faceye/WebRoot/Web-INF/classes/com/faceye/core/config/ApplicationContext-resource.xml 修改数据库连接参数(主要为数据库...

Global site tag (gtag.js) - Google Analytics