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>