晋江和莆田:struts2.0配置文件、常量配置详解

来源:百度文库 编辑:中财网 时间:2024/04/29 23:09:20

struts2.0配置文件、常量配置详解

通常struts2加载struts2常量的顺序如下:

1.struts-default.xml:该文件保存在struts2-core-2.0.6.jar文件中。
2.struts-plugin.xml:该文件保存在struts2-Xxx-2.0.6.jar等Struts2插件JAR文件中。
3.struts.xml:该文件是Web应用默认的Struts2配置文件。
4.struts.properties:该文件是Web应用默认的Struts2配置文件。
5.web.xml:该文件是Web应用的配置文件。

如果在多个文件中配置了同一个Struts2常量,则后一个文件中的配置的常量值会覆盖前面文件中配置的常量值。
在不同文件中配置常量的方式是不一样的,但不管哪个文件中,配置Struts2常量都要指定两个属性:常量name和常量value。

推荐在struts.xml文件中配置Struts2常量。

此处只加载了前三个配置文件,这是在常量struts.configuration.files中配置的。该属性指定Struts2框架默认加载的配置文件,如果需要指定默认加载多个配置文件,则多个配置文件的文件名之间以英文逗号(,)隔开。该属性的默认值为struts-default.xml,struts-plugin.xml,struts.xml,这就是上图中加载的三个配置文件。

Struts2常量的具体用法实例

Xml代码

  1. version="1.0"encoding="UTF-8"?>
  2. "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
  3. "http://struts.apache.org/dtds/struts-2.0.dtd">
  4.  
  5. name="struts.i18n.encoding"  value="UTF-8"/>
  6.  
  7. name="struts.serve.static.browserCache"  value="false"/>
  8.  
  9. name="struts.configuration.xml.reload"    value="true"/>
  10.  
  11. name="struts.devMode"  value="true"/>
  12.  
  13. name="struts.ui.theme"  value="simple"/>
  14.  
  15. name="struts.objectFactory"  value="spring"/>
  16.  
  17. name="struts.locale"  value="zh_CN"/>
  18. name="struts.i18n.encoding"  value="GBK"/>
  19.  
  20. name="struts.objectFactory" value="spring">
  21.  
  22. name="struts.objectFactory.spring.useClassCache"/>
  23.  
  24. name="struts.objectTypeDeterminer"value="tiger"/>
  25.  
  26. name="struts.multipart.parser" value="cos"/>
  27. name="struts.multipart.parser" value="pell"/>
  28. name="struts.multipart.parser" value="jakarta"/>
  29.  
  30. name="struts.multipart.saveDir" value="/tmpuploadfiles"/>
  31.  
  32. name="struts.multipart.maxSize" value="2097152"/>
  33.  
  34. name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.DefaultActionMapper"/>
  35.  
  36. name="struts.action.extension" value="do"/>
  37.  
  38. name="struts.serve.static.browserCache "value="true"/>
  39.  
  40. name="struts.enable.DynamicMethodInvocation" value="true"/>
  41.  
  42. name="struts.enable.SlashesInActionNames"  value="true"/>
  43.  
  44. name="struts.tag.altSyntax" value="true"/>
  45.  
  46. name="struts.configuration.xml.reload" value="true"/>
  47.  
  48. name="struts.devMode" value="true"/>
  49.  
  50. name="struts.i18n.reload" value="false"/>
  51.  
  52. name="struts.ui.theme" value="xhtml"/>
  53.  
  54. name="struts.ui.templateDir" value="template"/>
  55.  
  56. name="struts.ui.templateSuffix" value="ftl"/>
  57.  
  58. name="struts.velocity.configfile" value="velocity.properties"/>
  59.  
  60. name="struts.velocity.contexts" value="...."/>
  61.  
  62. name="struts.velocity.toolboxlocation" value="...."/>
  63.  
  64. name="struts.url.http.port" value="80"/>
  65.  
  66. name="struts.url.https.port" value="443"/>
  67.  
  68. name="struts.url.includeParams" value="get"/>
  69.  
  70. name="struts.custom.i18n.resources" value="application"/>
  71.  
  72. name="struts.freemarker.manager.classname" value="org.apache.struts2.views.freemarker.FreemarkerManager"/>
  73.  
  74. name="struts.freemarker.templatesCache"value="false"/>
  75.  
  76. name="struts.freemarker.wrapper.altMap" value="true"/>
  77.  
  78. name="struts.xslt.nocache"value="false"/>
  79. name="struts.configuration.files" value="struts-default.xml,struts-plugin.xml,struts.xml"/>
  80.  
  81. name="struts.mapper.alwaysSelectFullNamespace" value="false"/>

 

=======================================================

1、在struts2中,表单中提交的action,为什么一定要用xxx.action的形式呢?原因是在default.properties这个资源文件(只读)中是这么定义的。可以去修改它里面的配置。(方法是:在src目录下创建struts.properties文件,然后修改成自己需要的形式即可。),另外,为什么struts2的配置文件名一定要是struts.xml呢?其实这些定义都是在default.properties文件(只读)中配置好的。当然可以找到它,进行修改。(方法是:在src目录下创建struts.properties文件,然后修改成自己需要的形式即可。原因是)

2、如何防止表单中提交的中文,取出后显示是乱码:解决方法:

只需要在struts.xml文件的标签间加入一句代码即可

当然,也可以在struts.properties或web.xml中进行转码的配置。但是优先级的顺序是:web.xml>struts.properties>struts.xml

struts.properties(位于src目录下)中配置转码的格式:

struts.i18n.encoding=GBK

web.xml中配置转码的代码是:在中增加如下代码:

struts.i18n.encoding

GBK

3、要测试一个action运行的时间(会在控制台输出执行时间)。只需要struts.xml文件中的标签中加入如一代码(拦截器,具体说明与使用需要查看struts-default.xml文件),并在需要测试的action标签内部加上对它的引用即可。

    timer”class=”com.opensymphony.xwork2.interceptor.TimerInterceptor”>

params”class=”om.opensymphony.xwork2.interceptor.ParametersIncerceptor”>//为获取表单的参数需要加入这一句

 

下面是对上面代码的引用:

timer”/>

params”/>//引用一下获取表单参数

也就是说:如果加上了timer这个属性,就得加上params这个参数来获取表单中的参数。有点类似构造方法的原理

4、struts.xml文件中标签中的method属性,来指定该action调用method属性的值的那个方法。方法可以定义在xxxAction.java文件中。如:struts.xml中配置如下

/resultmodify.jsp

   

    //表示modify这个方法存在于TestMethod.java这个类中。只要浏览器一请求modify.action,就会自动转到相应的类中并调用相应的mofidy()。当然,标签中的name属性名是可以随意取的。只是在请求时名称要与它一致即可。               

   

        /resultquery.jsp

   

//与上面的功能类似

5、在action标签中用通配符映射来匹配:

* 表示0-N个字符,不包括/   常用

** 表示0-N个字符,包括/

\ 表示转义符

比如:将上面两个action标签合并为一个action标签。如下:

/{1}UserSucc.jsp

   

它可以表示任何对User进行操作的acion。都由TestMethod这个类来处理。并且method属性的值,是随着访问的变化而进行变化调用类中相应的方法。跳转到的页面也一样的规律。这种方式,要求取的方法名要有规律。

 

没有任何通配符的action匹配优先级最高。有通配符时,一律按顺序匹配。