struts2 -- 一个action类实现多个方法

SSH之action中的多个方法的调用方法

public String logon(){  
    return "success"; 
}  
  
public String register(){  
    return"success";
}  

有三种方式可以将页面提交和action的方法对应。

     (一)动态方法调用,配置文件不变,一个action对应配置文件中一个action标签,表单提交的action不直接等于某个actionname,而是以actionname!action的方法名来提交。

1.配置文件

<action name="user" class="com.tgb.struts2.action.LogonAction" >  
    <result name="success">/success.jsp</result>  
    <result name="error">/error.jsp</result>  
</action> 

2. 表单提交

登录:<form id="form" action="user!logon" method="post">            
注册:<form id="form" action="user!register" method="post">  

   (二)  修改配置文件将同一个action中的每个方法都用一个action标签映射

1.配置文件

<action name="logon" class="com.tgb.struts2.action.LogonAction" >  
    <result name="success">/success.jsp</result>  
    <result name="error">/error.jsp</result>  
</action>  
<action name="register" class="com.tgb.struts2.action.LogonAction" >  
    <result name="success">/success.jsp</result>  
    <result name="error">/error.jsp</result>  
</action>  

 2. 表单提交

登录:<form id="form" action="logon" method="post">               
注册:<form id="form" action="register" method="post">  

    (三)  使用通配符映射方式

    1. 配置文件

<action name="*User_*" class="com.tgb.struts2.action.LogonAction" method="{1}" >            
<result name="success">/{2}.jsp</result>  
<result name="error">/error.jsp</result>  
</action> 

method={1}表示对应使用的方法为第一个*的名字,{2}.jsp表示跳转到名字为第二个*的名字的地址

2. 表单提交

<form id="form" action="logonUser_index" method="post">
<!-- 执行action中的logon方法,执行成功后返回index首页面 -->  
<form id="form" action="registerUser_logon" method="post">
<!--执行action中的register方法,执行成功后返回logon登录页面  --> 

总结: 这三种方式中,使用!会暴露所使用的框架,第二种方式会使配置文件变得复杂冗余,第三种方式中和了前两种方式的缺点,建议使用通配符映射方式

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
192.168.85.1 - - [26/Jun/2022:06:07:07 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 24 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 24 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 24 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 200 12925 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 200 12925 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 200 12925 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 200 14 192.168.85.1 - - [26/Jun/2022:06:08:06 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 15 192.168.85.1 - - [26/Jun/2022:06:08:16 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 1227 192.168.85.1 - - [26/Jun/2022:06:10:15 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 79 192.168.85.1 - - [26/Jun/2022:06:13:25 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 404 752 192.168.85.1 - - [26/Jun/2022:06:16:42 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 35 192.168.85.1 - - [26/Jun/2022:06:16:57 -0400] "GET //struts2-showcase/hhh.jsp HTTP/1.1" 403 642 192.168.85.1 - - [26/Jun/2022:06:18:55 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 35 192.168.85.1 - - [26/Jun/2022:06:19:02 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 35 192.168.85.1 - - [26/Jun/2022:06:19:09 -0400] "GET //struts2-showcase/hhh1.jsp HTTP/1.1" 403 642 192.168.85.1 - - [26/Jun/2022:06:19:34 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 400 192.168.85.1 - - [26/Jun/2022:06:20:37 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 5 192.168.85.1 - - [26/Jun/2022:06:20:42 -0400] "GET //struts2-showcase/hhh1.jsp HTTP/1.1" 403 642 192.168.85.1 - - [26/Jun/2022:06:20:46 -0400] "GET //struts2-showcase/hhh.jsp HTTP/1.1" 403 642 192.168.85.1 - - [26/Jun/2022:06:20:51 -0400] "GET /struts2-showcase/hhh.jsp HTTP/1.1" 403 642
最新发布
07-12

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值