我有这个逻辑代码(将错误放到响应中):
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex) {
boolean maxSizeEx = true;
if (ex instanceof MaxUploadSizeExceededException) {
logger.debug("tried to upload a file that exceeded capacity", ex);
} else {
logger.warn("caught unexpected exception", ex);
maxSizeEx = false;
}
final boolean fMaxSizeEx = maxSizeEx;
View view = new View() {
public void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, String> data = new HashMap<String, String>();
if (fMaxSizeEx) {
data.put("error", "Limit size exceeded");
} else {
data.put("error", "internal error");
}
JsonApiController.mapToJsonAndOutput(response.getOutputStream(), data);
}
public String getContentType() {
return "application/json";
}
};
return new ModelAndView(view);
}
我希望在那之后,在视图中输出错误信息(我的jsf页面)。
我怎样才能在我的jsf页面中这样做?
这是mapToJsonAndOutput方法的代码:
protected static void mapToJsonAndOutput(OutputStream out, Map<String, String> data) throws IOException {
if (data == null || data.isEmpty()) {
logger.warn("You should not call mapToJsonAndOutput if there is no data");
return;
}
JsonFactory f = new JsonFactory();
JsonGenerator g = f.createJsonGenerator(out, JsonEncoding.UTF8);
g.writeStartObject();
for (Entry<String, String> e : data.entrySet()) {
g.writeStringField(e.getKey(), e.getValue());
}
g.writeEndObject();
g.flush();
DevisPdfResourceController.close(out);
}
感谢您的帮助。
编辑:
if (fMaxSizeEx) {
data.put("error", "Limit size exceeded");
} else {
data.put("error", "internal error");
}
JsonApiController.mapToJsonAndOutput(response.getOutputStream(), data)
代码data.put(&#34;错误&#34;,&#34;超出限制大小&#34;)似乎无效,因为我想在我的回复中添加错误消息。
有什么建议吗?
答案 0 :(得分:0)
我不知道这是否是您案例中最简单的方法,但是有一个名为<h:messages>
的JSF标记可以显示4个严重级别的FacesContext消息。
请参阅文档:http://www.jsftoolbox.com/documentation/help/12-TagReference/html/h_messages.html
以下是一个简单的例子:
查看(.jsf):
<h:messages />
Controller(.java):
FacesMessage message = new FacesMessage(FacesMessage.YOUR_SEVERITY, "your description", "your message");
FacesContext.getCurrentInstance().addMessage(null, message);