JSP 上未显示操作错误
Action errors are not shown on the JSP
我已经尝试在操作 class 中添加操作错误并将其打印在 JSP 页面上。
当发生异常时,它会进入 catch 块并在控制台中打印 "Error in inserting the Exception, Contact the Admin"。
在 catch 块中,我用 addActionError()
添加了它,并尝试在 jsp 页中打印它...
但 jsp 页面 中未显示消息。
我可能遗漏了什么或做错了什么?
Struts映射:
<action name="dataUpdate" class="foo.bar.myAction" method="updation">
<result name="success" type="redirectAction">
../Aggregator/redirectToDataUpdate
</result>
</action>
动作class:
public String updation() throws JiffieTransactionException{
try {
// do stuff...
} catch (NumberFormatException e) {
addActionError("Error in inserting the Exception, Contact the Admin");
System.out.println("Error in inserting the Exception, Contact the Admin");
e.printStackTrace();
}
return SUCCESS;
}
JSP 打印动作错误代码:
<s:if test="hasActionErrors()">
<br></br>
<div class="errors">
<font color="red">
<s:actionerror/>
</font>
</div>
</s:if>
在 catch 块中添加一个动作消息,例如:
addActionMessage("Error in inserting the Exception, Contact the Admin"); //in catch block
然后在 jsp 上写:
<s:if test="hasActionErrors()">
<br></br>
<div class="errors">
<font color="red">
<s:actionerror/>
</font>
</div>
<s:if test="hasActionMessages()">
<div class="errors">
<font color="red">
<s:actionmessage/>
</font>
</div>
</s:if>
</s:if>
当您执行 redirectAction 时,会创建一个新的 Request,因此所有 actionMessages、actionErrors 以及所有其他参数(未明确声明要在 struts 配置中传递)都会丢失。
然后
- 使用默认 dispatcher result instead of a redirectAction 结果,或
- 使用 MessageStore Interceptor 来保留跨重定向的错误和消息,或者
return 出现错误时类型调度程序的不同结果,例如。 ERROR
:
<action name="dataUpdate" class="foo.bar.myAction" method="updation">
<result name="success" type="redirectAction">....redirectToDataUpdate</result>
<result name="error">previousPage.jsp</result>
</action>
public String updation() {
try {
// do stuff...
return SUCCESS;
} catch (NumberFormatException e) {
addActionError("Errors... ");
e.printStackTrace();
return ERROR;
}
}
我已经尝试在操作 class 中添加操作错误并将其打印在 JSP 页面上。
当发生异常时,它会进入 catch 块并在控制台中打印 "Error in inserting the Exception, Contact the Admin"。
在 catch 块中,我用 addActionError()
添加了它,并尝试在 jsp 页中打印它...
但 jsp 页面 中未显示消息。
我可能遗漏了什么或做错了什么?
Struts映射:
<action name="dataUpdate" class="foo.bar.myAction" method="updation">
<result name="success" type="redirectAction">
../Aggregator/redirectToDataUpdate
</result>
</action>
动作class:
public String updation() throws JiffieTransactionException{
try {
// do stuff...
} catch (NumberFormatException e) {
addActionError("Error in inserting the Exception, Contact the Admin");
System.out.println("Error in inserting the Exception, Contact the Admin");
e.printStackTrace();
}
return SUCCESS;
}
JSP 打印动作错误代码:
<s:if test="hasActionErrors()">
<br></br>
<div class="errors">
<font color="red">
<s:actionerror/>
</font>
</div>
</s:if>
在 catch 块中添加一个动作消息,例如:
addActionMessage("Error in inserting the Exception, Contact the Admin"); //in catch block
然后在 jsp 上写:
<s:if test="hasActionErrors()">
<br></br>
<div class="errors">
<font color="red">
<s:actionerror/>
</font>
</div>
<s:if test="hasActionMessages()">
<div class="errors">
<font color="red">
<s:actionmessage/>
</font>
</div>
</s:if>
</s:if>
当您执行 redirectAction 时,会创建一个新的 Request,因此所有 actionMessages、actionErrors 以及所有其他参数(未明确声明要在 struts 配置中传递)都会丢失。
然后
- 使用默认 dispatcher result instead of a redirectAction 结果,或
- 使用 MessageStore Interceptor 来保留跨重定向的错误和消息,或者
return 出现错误时类型调度程序的不同结果,例如。
ERROR
:<action name="dataUpdate" class="foo.bar.myAction" method="updation"> <result name="success" type="redirectAction">....redirectToDataUpdate</result> <result name="error">previousPage.jsp</result> </action>
public String updation() { try { // do stuff... return SUCCESS; } catch (NumberFormatException e) { addActionError("Errors... "); e.printStackTrace(); return ERROR; } }