Actionerror标签和Actionmessage标签的用法

Actionerror和Actionmessage标签的用法:

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

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

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

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


测试Action类:

import com.opensymphony.xwork2.ActionSupport;

public class ActionErrorTag extends ActionSupport {

//定义两个参数接收前台提交数据
private int operand1;
private int operand2;
public int getOperand1() {
return operand1;
}
public void setOperand1(int operand1) {
this.operand1 = operand1;
}
public int getOperand2() {
return operand2;
}
public void setOperand2(int operand2) {
this.operand2 = operand2;
}
public String execute() {
if(getOperand1()==123456){
addActionMessage("账号验证成功");
if(getOperand2()==123456){
addActionMessage("密码验证成功");
}else{
addActionError("密码验证失败!");
}
}else{
addActionError("账号验证失败!");
}
return SUCCESS;
}

}


前台页面实例代码
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">
    <title>
      struts2 actionerror标签示例
    </title>
  </head>
  <body>
    <h3>
    <!-- JSP页面中使用<s:actionerrror/>和<s:actionmessage/>>来输出ActionError和ActionMessage信息。-->
    <!-- 输出getActionError()方法返回值 -->
    <s:actionerror/>
    <!-- 输出getActionMessage()方法返回值 -->
    <s:actionmessage />
<<s:form action="actionerror.action">
  <s:textfield name="operand1" label="操作数1"/>
  <s:textfield name="operand2" label="操作数2"/>
  <s:submit value="代数和"/>
</s:form>
    <br>
    <hr size="3" color="blue">
  </body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值