在form 中的Validate()方法中return了ActionErrors,如何在jsp页面中获取这个异常,不使用 <html:errors>等标签,就用代码获取错误信息。希大虾们赐教。
public ActionErrors validate(ActionMapping mapping, 
HttpServletRequest request) { String accountNumber=getAccountNumber(); 
String password=getPassword(); ActionErrors errors=new ActionErrors(); 
if(accountNumber.equals("")){ errors.add("ActionErrors.GLOBAL_MESSAGE",new ActionMessage("accountNumber.none")); 
return errors; 

if(password.equals("")){ 
errors.add("ActionErrors.GLOBAL_MESSAGE",new ActionMessage("password.none")); 
return errors; } 
return null; 

我知道可以用request把错误传到前台,请问怎么从ActionErrors把错误信息取出来?

解决方案 »

  1.   

    不知道你为什么不不用errors标签
    去看一下ErrorsTag的源代码,然后看一下doStartTag方法中的代码,摘出来,贴在你的jsp上
    路径是org.apache.struts.taglib.html.ErrorsTag
      

  2.   

    <%
        if (request.getAttribute("struts.valueStack") != null) {
            OgnlValueStack stack = (OgnlValueStack) request
                    .getAttribute("struts.valueStack");
            Map map = (HashMap) stack.findValue("errors");
            if(map.size()!=0)
            {
                System.out.print(((ArrayList) map.get("ssn")).get(0));
            }
        }
    %>---------------------------------------------ognl----------------------------------------
    %{#request["struts.valueStack"] neq null?errors.ssn[0]:'11111'}
    额 不过这个好像是struts2的
      

  3.   

    用资源配置文件.在Jsp页面用<html:error/>标记下.struts-config.xml中配置...
      

  4.   

    回楼上几位,不用标签的原因是 forward过去的jsp页面是一个xml页面 类似于这样的:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%
    response.setContentType("text/xml");
    String niname=(String)request.getAttribute("niname");
    String result=(String)request.getAttribute("result");
    out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    out.print("<loginResult>");
    out.print("<result>"+result+"</result>");
    out.print("<niname>"+niname+"</niname>");
    out.print("</loginResult>");
    %>
      

  5.   

    错误信息一般是保存在request范围:
    你在页面中这样取试试:
    假设:ActionErrors.GLOBAL_MESSAGE="xxx";request.getAttribute("xxx");
      

  6.   

    ActionErrors errors = new ActionErrors();request.setAttribute(Globals.ERROR_KEY ,errors);或者session.setAttribute(Globals.ERROR_KEY ,errors);获取:
    ActionErrors errors = new ActionErrors();errors = (ActionErrors)request.getAttribute(Globals.ERROR_KEY);或者errors = (ActionErrors)session.getAttribute(Globals.ERROR_KEY);
      

  7.   

    既然你不用struts的标签那么就可以向10楼说的这样!
      

  8.   

    问题解决了 用的一楼的方法代码如下:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ page import="org.apache.struts.action.ActionMessage"%>
    <%@ page import="org.apache.struts.action.ActionMessages"%>
    <%@ page import="org.apache.struts.taglib.TagUtils"%>
    <%@ page import="org.apache.struts.util.MessageResources"%>
    <%@ page import="java.util.Iterator"%>
    <%@ page import="java.util.Locale"%>
    <%@ page import="org.apache.struts.Globals"%>
    <%
    String error = "";
    ActionMessages errors = (ActionMessages) request.getAttribute("ae"); String property = "ActionErrors.GLOBAL_MESSAGE";
    String bundle = null;
    String locale = Globals.LOCALE_KEY; StringBuffer results = new StringBuffer();
    boolean headerDone = false;
    String message = null;
    Iterator reports = (property == null) ? errors.get() : errors
    .get(property); while (reports.hasNext()) { org.apache.struts.action.ActionMessage report = (org.apache.struts.action.ActionMessage) reports
    .next();
    message = TagUtils.getInstance().message(pageContext, bundle,
    locale, report.getKey(), report.getValues());
    error = results.append(message).toString();
    } response.setContentType("text/xml"); out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    out.print("<loginResult>");
    out.print("<result>" + error + "</result>");
    out.print("<niname>" + "no result" + "</niname>");
    out.print("</loginResult>");
    %>