Actionerror和Actionmessage标签的用法:
这两个标签的使用,在网上挺多答案的可是都不是我想要知道的那种答案。这一次我通过自己努力的尝试暂且知道了这两个标签的的用途拿出来和大家分享下。
这两个标签的作用是一样的都是我们在前台页面传送信息到后台时在前台提示消息他们的唯一不同点就是他们Action实例的用不同方法
的返回值;
Actionerror使用的是getActionErrors()这个方法的返回值
Actionmessage使用的是getActionmessage()这个方法的返回值
那么这两个标签到底怎么使用用在什么地方我在这里给大家做个例子吧!这样才能更好的说明问题。
我使用的环境是eclipse+tomcat的开发环境
这是我课程例子,就用这个吧!
struts.xml文件配置
- <action class="action.ActionErrorTag" name="3-14,15actionerror">
- <result name="success">/3-14,15actionerror.jsp</result>
- </action>
测试Action类:
- package 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>
-
-
- <s:actionerror/>
-
- <s:actionmessage />
- <<s:form action="/3-14,15actionerror.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>