【问题标题】:Break line in message thrown by custom Exception in JavaJava中自定义异常抛出的消息中的断行
【发布时间】:2023-03-05 06:01:01
【问题描述】:

我需要在自定义Exception 抛出的消息之间换行。

我正在使用 Struts 2 addActionError(),错误消息将显示在 JSP 上。

这是我的 Java 代码:

if (result_Of_Scan.equals("ERROR")) {
    throw new AdserverException("We are getting problem with connecting our " 
                    + "Antivirus Server! Please try  again after some time");
}

所以我需要在浏览器的第一行显示"We are getting problem with connecting our Antivirus Server",在下一行显示"Please try again after some time"

我尝试了\n<br>&ltbr&gt,但它们不起作用。

【问题讨论】:

  • 异常消息不应包含任何专用于显示的格式信息。我什至建议不要将异常消息用作 GUI 消息。让你的 GUI 处理异常(也许让它读取exception.getMessage())让它显示正确的失败消息。解析异常消息和组装 GUI 消息只是 GUI 的职责。
  • 你在哪里使用addAction?这个jsp怎么显示?
  • 以上代码在 Util 类中,我从 Action 类调用并在 jsp 中以下列方式使用它

标签: java jsp exception error-handling struts2


【解决方案1】:

要在 JSP 中的新行上显示消息,您应该使用 <br>。当您尝试它时,它没有显示在新行上,因为 <s:actionerror> 默认转义 html。您应该使用escape="false" 属性来更改输出以允许呈现html。

<s:if test="hasActionErrors()"> <div class="alert_msg alert"> <s:actionerror escape="false"/> </div> </s:if>

【讨论】:

  • 但是这样我必须为每个包含文件上传选项的jsp文件更改hasActionError。
猜你喜欢
  • 1970-01-01
  • 2017-02-28
  • 1970-01-01
  • 2015-04-25
  • 1970-01-01
  • 2019-12-06
  • 1970-01-01
  • 1970-01-01
  • 2011-09-18
相关资源
最近更新 更多