Actionerror和Actionmessage标签的用法

Actionerror和Actionmessage标签的用法:

这两个标签的使用,在网上挺多答案的可是都不是我想要知道的那种答案。这一次我通过自己努力的尝试暂且知道了这两个标签的的用途拿出来和大家分享下。

这两个标签的作用是一样的都是我们在前台页面传送信息到后台时在前台提示消息他们的唯一不同点就是他们Action实例的用不同方法

的返回值;

Actionerror使用的是getActionErrors()这个方法的返回值

Actionmessage使用的是getActionmessage()这个方法的返回值

那么这两个标签到底怎么使用用在什么地方我在这里给大家做个例子吧!这样才能更好的说明问题。

我使用的环境是eclipse+tomcat的开发环境

这是我课程例子,就用这个吧!

struts.xml文件配置

[html]  view plain  copy
  1. <action class="action.ActionErrorTag" name="3-14,15actionerror">  
  2.             <result name="success">/3-14,15actionerror.jsp</result>  
  3.         </action>  

测试Action类:

[java]  view plain  copy
  1. package action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class ActionErrorTag extends ActionSupport {  
  6.       
  7.     //定义两个参数接收前台提交数据  
  8.     private int operand1;  
  9.     private int operand2;  
  10.     public int getOperand1() {  
  11.         return operand1;  
  12.     }  
  13.   
  14.     public void setOperand1(int operand1) {  
  15.         this.operand1 = operand1;  
  16.     }  
  17.   
  18.     public int getOperand2() {  
  19.         return operand2;  
  20.     }  
  21.   
  22.     public void setOperand2(int operand2) {  
  23.         this.operand2 = operand2;  
  24.     }  
  25.       
  26.     public String execute() {  
  27.         if(getOperand1()==123456){  
  28.             addActionMessage("账号验证成功");  
  29.             if(getOperand2()==123456){  
  30.                 addActionMessage("密码验证成功");  
  31.             }else{  
  32.                 addActionError("密码验证失败!");  
  33.             }  
  34.         }else{  
  35.             addActionError("账号验证失败!");  
  36.         }  
  37.         return SUCCESS;  
  38.     }  
  39. }  

前台页面实例代码:

[html]  view plain  copy
  1. <%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK" %>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5.   <head>  
  6.     <meta http-equiv="Content-Type" content="text/html; charset=GBK">  
  7.     <title>  
  8.       struts2 actionerror标签示例  
  9.     </title>  
  10.   </head>  
  11.   <body>  
  12.     <h3>  
  13.     <!-- JSP页面中使用<s:actionerrror/>和<s:actionmessage/>>来输出ActionError和ActionMessage信息。下面是该JSP页面中使用这两个标签的示例代码:-->  
  14.     <!-- 输出getActionError()方法返回值 -->  
  15.     <s:actionerror/>  
  16.     <!-- 输出getActionMessage()方法返回值 -->  
  17.     <s:actionmessage />  
  18.     <<s:form action="/3-14,15actionerror.action">  
  19.   
  20.          <s:textfield name="operand1" label="操作数1"/>  
  21.         <s:textfield name="operand2" label="操作数2"/>  
  22.         <s:submit value="代数和"/>  
  23.     
  24.     </s:form>  
  25.     <br>  
  26.     <hr size="3" color="blue">  
  27.   
  28.   </body>  
  29. </html>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值