gpt4 book ai didi

java - 带有 Freemarker 的 Spring Framework 错误 Controller

转载 作者:行者123 更新时间:2023-11-29 06:15:54 25 4
gpt4 key购买 nike

我有问题。每次发生异常时,我的 spring 错误 Controller 都会绕过我的 sitemash-freemarker 装饰器,只显示错误转储。或者它包含装饰器但不放入用户 session ,因此装饰器中的个性化消失了。

如何使用freemarker在spring中正确集成异常处理?

从 web.xml 中提取:

//standard sitemash decorator and freemarker setup
<error-page>
<error-code>404</error-code>
<location>/pageNotFound.html?code=404</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/servletErrorView.html?code=500</location>
</error-page>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.html</location>
</error-page>

ErrorController.java:

@Controller
public class ErrorController extends AnnotationMethodHandlerAdapter {

private static final Logger log = Logger.getLogger(ErrorController.class);

@Autowired
private ComponentHelper helper;
@Autowired
private MailNotificationService mailNotificationService;

@RequestMapping(value = {"/servletErrorView.html", "/error.html"}, method = RequestMethod.GET)
protected ModelMap showError(HttpServletRequest req) {

String code = null, message = null, type = null, uri = null;
Object codeObj, messageObj, typeObj;
Throwable throwable;

ModelMap mm = helper.getMM();
//todo handle org.springframework.web.bind.MissingServletRequestParameterException

codeObj = req.getAttribute("javax.servlet.error.status_code");
messageObj = req.getAttribute("javax.servlet.error.message");
typeObj = req.getAttribute("javax.servlet.error.exception_type");
throwable = (Throwable) req.getAttribute("javax.servlet.error.exception");
uri = (String) req.getAttribute("javax.servlet.error.request_uri");

// Convert the attributes to string values
if (codeObj != null) code = codeObj.toString();
if (messageObj != null) message = messageObj.toString();
if (typeObj != null) type = typeObj.toString();

// The error reason is either the status code or exception type
String reason = (code != null ? code : type);
if (uri == null) {
uri = req.getRequestURI();
}
log.error("ErrorController\n reason:"+reason+"\n message:"+message+"\n uri:"+uri+"\n ",throwable);

mm.addAttribute("message", "<H4>" + reason + "</H4>" +
"<H4>" + message + "</H4>" +
"<P>" + ((throwable != null) ? getStackTrace(throwable) : "") + "</P>" +
"<I>Error accessing " + uri + "</I>");

String subject = "Error - "+reason;
String freemarkerTemplet = "/WEB-INF/freemarker/errorMail.ftl";

mailNotificationService.sendEmail(subject, "email@domain.com", mm, freemarkerTemplet);

return mm;
}

public static String getStackTrace(Throwable aThrowable) {
//add the class name and any message passed to constructor
final StringBuilder result = new StringBuilder("Trace: ");
result.append(aThrowable.toString());
final String NEW_LINE = "<br>";
result.append(NEW_LINE);

//add each element of the stack trace
for (StackTraceElement element : aThrowable.getStackTrace()) {
result.append(element);
result.append(NEW_LINE);
}
return result.toString();
}
}

错误.ftl

<html>
<#import "/spring.ftl" as spring/>
<#import "common.ftl" as common/>
<head>

<title>ITeezy: Error page</title>
<meta name="description" content="Error"/>
</head>

<body id="error">

<div id="main">
<h1>Error</h1>
<p><a href="<@spring.url "/index.html"/>"> <- Go back to Homepage</a></p><br>
<div id="logo"> <a href="<@spring.url "/index.html"/>"> <img src="/images/mainlogo.png" alt="[Logo]" width="260" height="140"/> </a> </div>

<#if message?exists>
<div class="message">Error: ${message}</div>
</#if>
<br>
We're sorry you received an error. Please do us a favor and let us know!
Email: <img src="/images/support-email.png" width="118" height="13"/>
with the error message and a description of what you were doing. Thanks!

<#if exception?exists>
${exception}
<#list exception.stackTrace as st>
${st}
</#list>
<#else>
<#if javax?exists && javax.servlet?exists && javax.servlet.error?exists && javax.servlet.error.exception?exists>
Servlet Exception:<p>
${javax.servlet.error.exception} <br>
${javax.servlet.error.exception.message?default("")} <br>
<#list javax.servlet.error.exception.stackTrace as st>
${st}<br>
</#list>
</#if>
</#if>

</div>
</body>
</html>

最佳答案

像这样:

<bean id="errorMapping" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="error/default"/>
<property name="defaultStatusCode" value="500"/>
<property name="exceptionMappings">
<props>
<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error/uploadsize</prop>
</props>
</property>
</bean>

...您将在模型中获得名为exception 的对象。不需要 web.xml 配置。您可以为特定异常类指定自定义 View 或回退到默认 View 。您还可以覆盖标准解析器之一,以使用其他数据填充模型,例如,将堆栈跟踪转储为字符串并将其显示在剧透 block 下的异常页面上。

关于java - 带有 Freemarker 的 Spring Framework 错误 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5112597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com