com.jfinal.template.ext.spring.JFinalView Maven / Gradle / Ivy
The newest version!
/**
* Copyright (c) 2011-2023, James Zhan 詹波 ([email protected]).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jfinal.template.ext.spring;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.view.AbstractTemplateView;
/**
* JFinalView
*
*
* 关键设置:
* 1:setContentType("text/html;charset=UTF-8") 设置 content type 字符集为 UTF-8
*
* 2:setExposeRequestAttributes(true) 设置将 request 中的属性值注入到 model 中去
* 便于在模板中使用 #(value) 访问 request.setAttribute(...) 进去的值
*
* 3: setExposeSessionAttributes(true) 设置将 session 中的属性值注入到 model 中去
* 使用在模板中使用 #(value) 访问 session.setAttribute(...) 进去的值
*
* 注意:JFinalViewResolver.setSessionInView(true) 中的配置与
* JFinalView.setExposeSessionAttributes(true) 可实现
* 相似的功能,区别在于前者访问方式为 #(session.value) 而后者为
* #(value),两种配置只选其一
*
*/
public class JFinalView extends AbstractTemplateView {
@Override
protected void renderMergedTemplateModel(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception {
if (JFinalViewResolver.sessionInView) {
HttpSession hs = request.getSession(JFinalViewResolver.createSession);
if (hs != null) {
model.put("session", new InnerSession(hs));
}
}
try {
OutputStream os = response.getOutputStream();
JFinalViewResolver.engine.getTemplate(getUrl()).render(model, os);
} catch (Exception e) { // 捕获 ByteWriter.close() 抛出的 RuntimeException
Throwable cause = e.getCause();
if (cause instanceof IOException) { // ClientAbortException、EofException 直接或间接继承自 IOException
String name = cause.getClass().getSimpleName();
if ("ClientAbortException".equals(name) || "EofException".equals(name)) {
return ;
}
}
throw e;
}
}
@SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
public static class InnerSession extends HashMap
© 2015 - 2024 Weber Informatics LLC | Privacy Policy