ActionError 未显示

2022-01-16 00:00:00 jquery javascript java jsp struts2

如果 Action 类返回错误,我想在 ErrorDiv 中加载我的 Error.jsp.我正在做一个 AJAX 调用.

I want to load my Error.jsp in my ErrorDiv if Action class returns error. I am doing an AJAX call.

JS:

success: function(result){    
    if(result === 'success')
        alert('Database Updated Successfully!');
    else{
         $('#ErrorDiv').load('/gma/pages/Error.jsp');
    }
}

                

Error.jsp:

<body>

<%
    request.setAttribute("decorator", "none");
    response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
    response.setHeader("Pragma","no-cache"); //HTTP 1.0
    response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
%>
<s:if test="hasActionErrors()">
       <s:actionerror />
</s:if>

</body>

但是,不会显示操作错误.在我检查的萤火虫中,对 GET Error.jsp 的响应是 <body></body> 部分为空.

However, action errors are not getting displayed. In firebug I checked, response to GET Error.jsp in that the <body> </body> part comes empty.

为什么 actionError 不显示?

动作类:

try{
slsConfigureTspThresholdRemote.setThresholdParameters(circleId, tspId, thresholdTypeFlag, thresholdParametersList);

}
catch (Exception e){    
    addActionError(e.getMessage());
    e.printStackTrace();
    
    result = "error";
    return ERROR;
}

struts.xml:

<action name="updateThresholdParameters"
class="cdot.oss.cmsat.gma.struts.ConfigureTspThresholdAction" method="updateThresholdParameters">

<result name="success" type="json">
    <param name="root">result</param>
</result> 

<result name="error">pages/Error.jsp</result>

目前,我正在执行 $('#ErrorDiv').html(result); 以便将我的 JSP 加载到 div 而不是

Presently, I am doing $('#ErrorDiv').html(result); so that my JSP get loaded in div instead of

$('#ErrorDiv').load('/gma/pages/Error.jsp');!

推荐答案

验证错误只对同一个请求有效.此外,对 JSP 的直接访问可能由 Struts 标记不起作用的 Web 容器处理.

The validation errors are available only to the same request. Also direct access to the JSPs might be handled by the web container where the Struts tags will not work.

您应该使用一个动作来呈现一个 JSP,并且该动作应该运行一个 store 拦截器,如果你想保留前一个请求的验证错误.

You should use an action to render a JSP and the action should run a store interceptor if you want to keep the validation errors from the previous request.

如果您想使用相同的响应来返回不同的结果,您可以为每个结果设置不同的状态代码.

If you want to use the same response to return a different result you can set the different status code with each result.

在客户端你可以查看Ajax响应返回的状态码并做相应的事情.

On the client you could check the status code returned by the Ajax response and do corresponding things.

success: function(data, textStatus, jqXHR){
    if(jqXHR.status == 200) {                      
      alert('Database Updated Successfully!'); 
    }
}
error: function(data, textStatus, jqXHR){
    if(jqXHR.status == 400) {
      $('#ErrorDiv').html(data);        
    }     
}

要在 JSP 中显示错误,请将以下内容添加到 scriptlet 代码中

To show errors in the JSP add the following to the scriptlet code

response.setStatus(400);

相关文章