码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2框架之配置文件struts.mxl理解

时间:2014-06-05 02:16:32      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:struts2   struts.xml   server   源代码   

摘要: struts.xml是struts2开源框架的核心配置文件,其中包含一些全局的属性,用户请求和响应Action之间的对应关系,以及配置Action中可能用到的参数,以及处理结果的返回页面。还包括各种拦截器的配置等。本文用源代码的角度,来理解struts.xml文件的运行机理,及牵涉的各种文件开发项目前灵活运用struts源代码各种文件,省去将所有的参数及key值拼写出错。

配置struts2项目的基本要求:

下载struts源代码:(本文用的代码为:struts-2.3.15.1)

A、先找到struts2-blank.war(文件路径\struts-2.3.15.1\apps\struts2-blank.war),解压缩:

之后将路径(\struts2-blank\WEB-INF\lib)中的jar包添加到工程中,即可。

B、再找到struts.xml文件,路径为(\struts2-blank\WEB-INF\src\java\struts.xml),找到struts.xml文件,将这个struts.xml文件拷贝到自己创建的Web工程中的src目录下,即可。

 

本文主要理解struts.xml文件,我们先打开该文件,如下所示:

bubuko.com,布布扣

如上图所示,这是struts.xml配置文件的基本参数格式,我们现在来解析一些这个配置文件各个参数的作用及用法。

1、struts PUBLIC,主要是struts开源框架的一些规范。这个Key值,可以用来配置struts.xml文件的XML的提示信息,请参看《Struts2框架中书写XML配置文件时能添加提示技巧(方案二)

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD StrutsConfiguration 2.3//EN"

    "http://struts.apache.org/dtds/struts-2.3.dtd">

2、Struts.xml配置文件中的全局常量配置。如下面对action请求动作的配置。

<!-- 配置 Struts 可以受理的请求的扩展名 -->

    <constant name="struts.action.extension" value="action,do,">

</constant>

参看如下默认参数(default.properties),路径为:

/project/Web AppLibraries/struts2-core-2.3.15.3.jar/org.apache.struts2/default.properties,如下图所示,要修改默认变量,可以进行如上参数配置

bubuko.com,布布扣

这样就可以接受以.do和.action及什么也不加的请求操作了,例如:

http://localhost:8080/struts2-3/TestAware.do

http://localhost:8080/struts2-3/TestAware.action

http://localhost:8080/struts2-3/TestAware

 

 

3、struts2配置文件中package,这个部分主要负责各种URL路径的转换(转发、重定向等),是视图层与逻辑层的枢纽(控制转发层)。

    <package name="helloWorld" extends="struts-default">

    <action name="product-input"

         class="com.opensymphony.xwork2.ActionSupport"

         method="execute">

         <result name="success" type="dispatcher">/WEB-INF/pages/input.jsp</result>

    </action>       

    </package>

 

struts.xml先继承struts-default.xml文件,我们先看看这个文件,这个文件的路径为:/project/Web App Libraries/struts2-core-2.3.15.3.jar/struts-default.xml

bubuko.com,布布扣

这个文件中默认了很多的拦截器,默认的拦截器栈及默认的action类(com.opensymphony.xwork2.ActionSupport),源代码查看这个类:

bubuko.com,布布扣

有了这个默认类的出现,我们可以不写action的配置代码,只要将struts.xml配置文件进行配置,就能实现类的转发或者重定向,struts.xml配置文件的信息如下图所示:

<action name="product-input"

         class="com.opensymphony.xwork2.ActionSupport"

         method="execute">

         <result name="success" type="dispatcher">/WEB-INF/pages/input.jsp</result>

    </action>

 

如果不用上面com.opensymphony.xwork2.ActionSupport类做为默认类,哪的自己要在后台再写一个Action类,例如相应的struts.xml的配置文件如下所示:

<action name="product-save"class="com.guanqing.struts2.helloworld.Product"

         method="save">

         <result name="details">/WEB-INF/pages/details.jsp</result> 

</action>

其他的比较网上普遍有的注意点,这里就不说啦,请参考如下文档:

http://blog.csdn.net/springsen/article/details/7294433

http://blog.csdn.net/zhongwen7710/article/details/26630673

Struts2框架之配置文件struts.mxl理解,布布扣,bubuko.com

Struts2框架之配置文件struts.mxl理解

标签:struts2   struts.xml   server   源代码   

原文地址:http://blog.csdn.net/zhongwen7710/article/details/27119277

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!