简体   繁体   English

如何根据类错误在jsp中显示消息

[英]How to display messages in jsp according to class errors

here's my Form Class: 这是我的形式课:

public ArrayList<ForbesData> loadData(HttpServletRequest request) throws Exception   {
    ActionErrors errors = new ActionErrors();
     .....
    try {
  ......
 }
catch (NullPointerException e) {
        errors.add("fich", new ActionMessage("error.file.required"));
    }
    catch (IndexOutOfBoundsException e) {
        errors.add("crap", new ActionMessage("error.crapull.required"));
    }
    catch (InvalidFormatException e) {
        errors.add("exc", new ActionMessage("error.excell.required"));
    }

Here's my ApplicationResource.properties: 这是我的ApplicationResource.properties:

error.fich.required= folder doesn't exist!
error.crapull.required=folder isn't crapull!
error.excell.required=folder isn't excel!

here's my struts-config: 这是我的struts-config:

      <struts-config>
<form-beans>
    <form-bean name="UserForm" type="net.cacib.struts.UserForm" /> 
</form-beans>



<action-mappings>
    <action input="/" name="UserForm" path="/userAction" scope="session" validate="true"
        type="net.cacib.struts.UserAction">
        <forward name="success" path="/index.jsp" />
    </action>



    <action path="/Welcome" forward="/welcomeStruts.jsp" />
</action-mappings>

<message-resources parameter="/WEB-INF/ApplicationResource" />

in my jsp: 在我的jsp中:

 <html:errors property="crap" />
 <html:errors property="fich" />
 <html:errors property="exc" />

what I want to do is display error messages to the user each time an exception is thrown. 我想做的是每次引发异常时向用户显示错误消息。

Do you have any idea what I need to change my code to achieve this goal. 您是否知道我需要更改代码以实现此目标。 thank you 谢谢

And what happens now? 现在发生了什么? If it's that you only show one error, it's because you are throwing an exception and have only a single catch block. 如果是,你只显示一个错误,那是因为抛出一个异常,并且只有一个 catch块。

If you want to catch all of them and accumulate errors then you need to put a try/catch around each individual chunk of code that can throw the exceptions. 如果要捕获所有错误并累积错误,则需要对可能引发异常的每个单独的代码块进行尝试/捕获。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM