Struts框架的基础配置

常用的常量配置

 1)i18n.ecoding 设置编码 解决了post请求乱码的问题

在src目录下的 struts.xml 中的标签中修改


<struts>

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

</struts>

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

 

<!--extension  使用设置action访问的扩展名

    action,,

可以改成do  为什么要改成do  因为struts1用的是do

如果改成do 那么访问路径中的action中必须加上do 否则会有404

-->

<!-- 修改访问后缀的扩展名 及访问路径的地址结尾  这里设置的是
     以   .do   或 空  即 不加后缀也可以访问
-->
修改前 可通过在后缀加上  .action 或者不加后缀访问
修改后 可通过在后缀加上     .do 或者不加后缀  访问
<constant name="struts.action.extension" value="do,,"></constant>

也可以

<constant name="struts.action.extension" value="do,"></constant>
这样的话   就只有在访问地址后缀加上   .do  才可以访问  其他都不行

 3)struts.devMode  设置开发者模式 默认关闭

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

 

 

3. 高级配置

   为什么要使用动态方法

   ...提高开发效率,避免代码的重复(冗余代码)

(1) 推荐使用

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

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

  struts2 可以采用通配符的方式默认去读取method{1}会自动将路径中userAction... 配置得到method中

 

(2) 开启动态方法
首先要开启动态方法的常量

 <action name="userAction" class="cn.hd.dynamic.UserAction">
        <result name="success">/hello.html</result>
    </action>
<!--动态方法调用常量 默认是关闭的-->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

   action中的method 删除掉

   完成了动态方法的开启

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


   注意:

第一种方法在struts2的高版本中会无效

3)第三种

首先你也要开启动态方法,然后增加allowed-method 属性

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

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

 

  

4. 默认配置 (了解)

<!--
   method 属性可以不写 默认方法是  execute
   result 标签中的name  属性 可以不写 success
      type  可以不写 默认是 dispatcher
      class  可以不写  默认的类是 ActionSupport  打开这个类文件
      它里面有 execute 方法  该方法返回一个success字符串
-->

 

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

-->

<struts>
<package name="default" namespace="/default" extends="struts-default">

    <default-action-ref name="defaultAction"></default-action-ref>

    <action name="defaultAction" class="cn.hd.defaultDemo.DefaultAction" method="execute">

        <result>/hello.html</result>

    </action>

</package>
</struts>


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值