在2005年Apache正式推出Struts 1.2后,有许多东西已经被改变。其中很重要的一个改动就是与<html:errors>标记相关的改动,主要就是已经不再推荐使用ActionError类和ActionErrors类,因此,如果要将现有的使用到与<html:errors>标记相关的代码从Struts 1.1下移至Struts 1.2下,需要作以下改动。
1. 将代码中使用到ActionError类的地方换为ActionMessage类。
2. 将除了在ActionForm的validate()方法以外使用到ActionErrors类的地方都替换为ActionMessages。
这样做的主要原因是,ActionError是ActionMessage的子类,而ActionErrors是ActionMessages的子类。开发者认为一个错误消息也是消息的一种,并没有必要专门将其分开存放。只需要使用Globals.MESSAGE_KEY,Globals.ERROR_KEY来进行区分就分。
在struts1.1中的使用
1.html:messages说明:
1.ActionMessages中每一个key对应一个List
2.id属性:遍历ActionMessages时代表一个ActionMessage,可以随意瞎写,这个要与bean:write搭配使用
3.property属性: 指定ActionMessages(ActionErrors)中一个key,这样就可以只显示这个key对应List的了。 如果不指定property属性,那么就是遍历所有的。
eg:Action代码:
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserForm userForm = (UserForm) form;
String username = userForm.getUsername();
String password = userForm.getPassword();
//ActionMessages与ActionMessage是struts1.1中新添加的,并不能像在struts1.2中那样使用。
//在struts1.1中使用ActionErrors没问题。使用ActionMessages有问题
ActionErrors errors = new ActionErrors();
errors.add("error1", new ActionError("login.error.error1"));
errors.add("error2", new ActionMessage("login.error.error2"));
// struts1.1中 不能使用这? 在struts1.2以上高版本中就可以使用
// errors.add("error3", new ActionMessage("数据采集失败...", false));//在页面中取不出来
// this.saveMessages(request, errors);
this.saveErrors(request, errors);
return mapping.findForward("error");
}
}
JSP页面获取信息的方式:
这里如果不使用property属性的话,会将key为error1与error2对应List中的数据全部取出来显示
<html:messages id="error" property="error2">
<bean:write name="error"/>
</html:messages>
2.html:errors说明:
Java代码:
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserForm userForm = (UserForm) form;
String username = userForm.getUsername();
String password = userForm.getPassword();
System.out.println("username :" + username);
System.out.println("username :" + password);
//ActionMessages与ActionMessage是struts1.1中新添加的,并不能像在struts1.2中那样使用。
//在struts1.1中使用ActionErrors没问题。使用ActionMessages有问题。所以干脆使用ActionErrors
ActionErrors errors = new ActionErrors();
errors.add("error1", new ActionError("login.error.error1"));
errors.add("error2", new ActionMessage("login.error.error2"));
// struts1.1中 使用 不正常? 在struts1.2以上高版本中 正常使用
// errors.add("error3", new ActionMessage("数据采集失败...", false));//在页面中取不出来
// this.saveMessages(request, errors);
this.saveErrors(request, errors);
return mapping.findForward("error");
}
}
1. 放入ActionErrors中的ActionMessage对象,不能使用html:errors标签来取,只能使用html:messages标签来取,否则会报错。
eg:需要通过html:message来取
errors.add("error2", new ActionMessage("login.error.error2")); //放入ActionErrors中的是一个ActionMessage对象
JSP代码:
<html:messages id="message" property="error2">
<bean:write name="message"/>
</html:messages>
2.放入ActionErrors中的ActionError对象,既能使用html:errors来获取,也能使用html:messages标签来取。
errors.add("error1", new ActionError("login.error.error1")); //放入ActionErrors中的是一个ActionError对象
JSP代码:
property属性:指定ActionErrors中的一个key,这样就可以只显示这个key对应的List的了
<html:errors property="error1"/>
或者
<html:messages id="message" property="error1">
<bean:write name="message"/>
</html:messages>
2.在struts1.2中的使用
在struts1.2中已经不推荐使用ActionErrors与ActionError了,所以简化了编写代码。
资源文件:login.error.error2=我是徐艳荣
JAVA代码:
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserForm userForm = (UserForm) form;
String username = userForm.getUsername();
String password = userForm.getPassword();
System.out.println("username :" + username);
System.out.println("username :" + password);
ActionErrors errors = new ActionErrors(); // 从struts1.2开始不推荐使用了
ActionMessages messages = new ActionMessages();// 从struts1.2开始,不管错误还是提示信息,推荐统一使用这个
messages.add("error2", new ActionMessage("login.error.error2"));
// struts1.1中使用 不正常,在struts1.2中没问题
messages.add("error3", new ActionMessage("数据采集失败...", false));
this.saveErrors(request, messages);
// this.addErrors(request, messages); //在struts1.2中正常
// this.addMessages(request, messages);//在struts1.2中取不出来
// this.saveMessages(request, messages);//在struts1.2中取不出来
return mapping.findForward("error");
}
}
JSP代码:可以通过如下2种方式获取:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
这种方式打印出来信息间是有空格的
<html:messages id="message">
<bean:write name="message"/>
</html:messages>
这种方式打印出来的信息间是没空格的
<html:errors/>
</body>
</html>
测试结果:我是徐艳蓉 数据采集失败... 我是徐艳蓉数据采集失败...