action 的四种struts 配置方法


配置Action的方法

如何解决action太多的问题??

1.建立多个Action类

  这个方法,Action类多,不简洁,不推荐使用

  

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 
 3 <!DOCTYPE struts PUBLIC
 4         "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
 5         "http://struts.apache.org/dtds/struts-2.5.dtd">
 6 <struts>
 7     
 8     <package name="user" namespace="/" extends="struts-default">
 9         <action name="display" class="action.DisplayAction" method="execute">
10             <result name="success" type="dispatcher">/display.jsp</result>
11         </action>
12         <action name="login" class="action.LoginAction" method="execute">
13         </action>
14         <action name="inspectUserName" class="action.RegisterAction" method="execute">
15         </action>
16         <action name="clickRegister" class="action.ClickRegisterAction" method="execute">
17         </action>
18     </package>
19 </struts>
struts.xml

2.method方式:建立一个Action类,在类中编写不同的方法,在不同的方法中处理不同的请求。
在struts.xml中配置method属性,让不同的action指向同一个Action类的不同方法。

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 
 3 <!DOCTYPE struts PUBLIC
 4         "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
 5         "http://struts.apache.org/dtds/struts-2.5.dtd">
 6 <struts>
 7     
 8     <package name="user" namespace="/" extends="struts-default">
 9         <action name="display" class="action.UserAction" method="display">
10             <result name="success" type="dispatcher">/display.jsp</result>
11         </action>
12         <action name="login" class="action.UserAction" method="login">
13         </action>
14         <action name="inspectUserName" class="action.UserAction" method="register">
15         </action>
16         <action name="clickRegister" class="action.UserAction" method="clickRegister">
17         </action>
18     </package>
19 </struts>
struts.xml

3.感叹号方式:
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
不需要配置method,直接用!方法名的方式来调用。

DynamicMethodInvocation =动态方法调用

这一种action与第二种一样,不过配置文件中代码却少了很多很多

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 
 3 <!DOCTYPE struts PUBLIC
 4         "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
 5         "http://struts.apache.org/dtds/struts-2.5.dtd">
 6 <struts>
 7     <constant name="struts.enable.DynamicMethodInvocation" value="true" />
 8     
 9     <package name="user" namespace="/" extends="struts-default">
10         <action name="user" class="action.UserAction" method="display">
11             <result name="success" type="dispatcher">/display.jsp</result>
12         </action>
13         
14     </package>
15 </struts>
struts.xml

不过在jsp页面写上传的方式要用  user!方法名

 

4.通配符方式(官网推荐) “约定大于配置”

checkLogin
checkExists
add
delete
modify


<action name="*" class="myuser.UserAction" method={1}>

</action>

这一种官方推荐当然是最好用的

action同上不变,直接看struts2.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 
 3 <!DOCTYPE struts PUBLIC
 4         "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
 5         "http://struts.apache.org/dtds/struts-2.5.dtd">
 6 <struts>
 7     <constant name="struts.action.extension" value="action,,"></constant>
 8     <constant name="struts.enable.DynamicMethodInvocation" value="true" />
 9     <package name="user" namespace="/" extends="struts-default">
10         <global-allowed-methods>login,display,clickRegister,inspectUserName</global-allowed-methods>
11         <action name="*" class="action.UserAction" method="{1}">
12             <result name="success" type="dispatcher">/display.jsp</result>
13         </action>
14     </package>
15 </struts>
struts.xml

其中这里面在struts2.3之后需要加上

<global-allowed-methods>login,display,clickRegister,inspectUserName</global-allowed-methods>

如果不加这个是会报

Struts has detected an unhandled exception:

Message:There is no Action mapped for namespace [/] and action name [user_login] associated with context path [/shop].错误的。

详情请看http://blog.csdn.net/gsycwh/article/details/52156642 作者的介绍。这个问题整了我1个多小时。

 

转载于:https://www.cnblogs.com/xiaoxingxingyu/p/8459868.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值