struts2配置文件详解 常量配置

struts2配置文件详解

1.struts.xml

这两个地址要一致:


2..includesrc struts.xml可以去读其他位置的xml文件 要求 新创建的xml必须有约束

 <include file="cn\hd\dynamic\struts01.xml"></include>

          常量配置:

默认的常量配置:


如何修改这些常量:

①在struts.xml中去修改

扫描二维码关注公众号,回复: 1941151 查看本文章

<!--il8n 国际化 相当于我们在servlet中设置的编码
        解决了post请求的中文乱码问题-->
<constant name="struts.i18n.encoding" value="utf-8"></constant>

②在src目录下创建struts.peroperties文件 直接在该文件中去修改常量即可


③在web.xml文件中去修改,添加一个context-param。在param-name  中书写常量名 param-value  中书写常量值


  三个地方同时修改那个生效

web.xml先生效>struts.properties第二个>struts.xml最后一个

推荐使用第一个;

常用的常量:

 ①il8n.ecoding 设置编码 解决了post请求的乱码问题

<constant name="struts.i18n.encoding" value="utf-8"></constant>


②扩展名

struts.action.extension 设置action的访问扩展名

<!--extension 使用设置action访问的扩展名
   action
     可以改为do 为什么要改成go 以为struts1用的是do

如果改为do那么在访问路径中的action中要加.do 否则404-->

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

③struts.devMode 设置开发中模式 默认关闭

<!--developerMode 开发者模式
     1.热部署 你修改配置文件后,等一段时间会自动加载
     2.提高错误信息的提示(友好的错误提示)-->
<constant name="struts.devMode" value="true"></constant>

高级配置:

动态方法:

如何配置动态方法

1)推荐使用

<action name="userAction_*" class="cn.hd.dynamic.UserAction" method="{1}">
    <result name="success">/hello.html</result>
</action>

Action配置中的name属性决定了浏览器的访问地址

Struts2可以采用通配符的方式默认去读取method{1}会自动将路径中

userAction。。。配置到mehtod中

②开启动态方法

(1)

首先开启动态方法的常量

<!--动态方法的调用的常量 默认是关闭的-->

<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>


(2)

action中的method属性删除掉完成动态方法的开启

(3)

测试时候 在浏览器的路径下 输入action 的name值+!+方法名(Action中的方法)

 注意:第一种方法 struts2的高版本中会无效

   首先你要开启动态方法,然后添加allowed-methods属性

<action name="userAction_*" class="cn.hd.dynamic.UserAction" method="{1}">
    <result name="success">/hello.html</result>
    <allowed-methods>update,add,delete,set</allowed-methods>
</action>

默认配置:

 <!--method 属性刻意不写 默认方法
       默认方法execute
        result标签中的name属性可以不写success

       type 可以不写 默认dispatcher
       class 可以不写 默认的是ActionSupport 打开这个类文件 它里面有execute方法 该方法
       返回一个success字符串-->
    <action name="defaulfAction">
        <result>/hello.html</result>

    </action>
</package>

 

<!--默认的action地址
  如果请求地址不存在,那么会默认访问default-action-ref
  里面的action-->
<default-action-ref name="defaulfAction"></default-action-ref>

        

        下篇看源码

猜你喜欢

转载自blog.csdn.net/qq_41961660/article/details/80819941
今日推荐