Freemarker SpringMVC 前台无法使用session;request等对象问题
2015-12-17 15:53
591 查看
SpringMVC和Freemarker整合时
我通过 继承org.springframework.web.servlet.view.AbstractTemplateView类实现FreeMarker的视图展现;
发现此视图展现为一个轻量级FreeemarkerView;由于它不支持request、session、application等对象;
另外此类为一个抽象类;需要写一个实现方法;在实现方法中重写renderMergedTemplateModel方式;
将session等对象注册到modelmap中;即可以在界面中使用了;
例:
package com.lmd.zjt.core;
import java.io.IOException;
import java.util.Calendar;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContextException;
import org.springframework.web.servlet.view.AbstractTemplateView;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;
import freemarker.core.ParseException;
import freemarker.template.Configuration;
/**
* 轻量级的FreeemarkerView
*
* 不支持jsp标签、不支持request、session、application等对象,可用于前台模板页面。
*/
public class SimpleFreeMarkerView extends AbstractTemplateView {
/**
* 部署路径调用名称
*/
public static final String CONTEXT_PATH = "base";
/**
* 系统当前时间
*/
public static final String SYS_DATE = "sysTime";
private Configuration configuration;
public void setConfiguration(Configuration configuration) {
this.configuration = configuration;
}
protected Configuration getConfiguration() {
return this.configuration;
}
/**
* 自动检测FreeMarkerConfig
*
* @return
* @throws BeansException
*/
protected FreeMarkerConfig autodetectConfiguration() throws BeansException {
try {
return (FreeMarkerConfig) BeanFactoryUtils
.beanOfTypeIncludingAncestors(getApplicationContext(),
FreeMarkerConfig.class, true, false);
} catch (NoSuchBeanDefinitionException ex) {
throw new ApplicationContextException(
"Must define a single FreeMarkerConfig bean in this web application context "
+ "(may be inherited): FreeMarkerConfigurer is the usual implementation. "
+ "This bean may be given any name.", ex);
}
}
/**
* Invoked on startup. Looks for a single FreeMarkerConfig bean to find the
* relevant Configuration for this factory.
* <p>
* Checks that the template for the default Locale can be found: FreeMarker
* will check non-Locale-specific templates if a locale-specific one is not
* found.
*
* @see freemarker.cache.TemplateCache#getTemplate
*/
protected void initApplicationContext() throws BeansException {
super.initApplicationContext();
if (getConfiguration() == null) {
FreeMarkerConfig config = autodetectConfiguration();
setConfiguration(config.getConfiguration());
}
checkTemplate();
}
/**
* Check that the FreeMarker template used for this view exists and is
* valid.
* <p>
* Can be overridden to customize the behavior, for example in case of
* multiple templates to be rendered into a single view.
*
* @throws ApplicationContextException
* if the template cannot be found or is invalid
*/
protected void checkTemplate() throws ApplicationContextException {
try {
// Check that we can get the template, even if we might subsequently
// get it again.
logger.debug(getUrl());
logger.debug(this.getAttributesMap());
logger.debug(this.getStaticAttributes());
getConfiguration().getTemplate(getUrl());
} catch (ParseException ex) {
throw new ApplicationContextException(
"Failed to parse FreeMarker template for URL [" + getUrl()
+ "]", ex);
} catch (IOException ex) {
throw new ApplicationContextException(
"Could not load FreeMarker template for URL [" + getUrl()
+ "]", ex);
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void renderMergedTemplateModel(Map model,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
model.put(CONTEXT_PATH, request.getContextPath());
model.put(SYS_DATE, Calendar.getInstance().getTime());
model.put("session", request.getSession());
model.put("request", request);
getConfiguration().getTemplate(getUrl()).process(model,
response.getWriter());
}
}
我通过 继承org.springframework.web.servlet.view.AbstractTemplateView类实现FreeMarker的视图展现;
发现此视图展现为一个轻量级FreeemarkerView;由于它不支持request、session、application等对象;
另外此类为一个抽象类;需要写一个实现方法;在实现方法中重写renderMergedTemplateModel方式;
将session等对象注册到modelmap中;即可以在界面中使用了;
例:
package com.lmd.zjt.core;
import java.io.IOException;
import java.util.Calendar;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContextException;
import org.springframework.web.servlet.view.AbstractTemplateView;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;
import freemarker.core.ParseException;
import freemarker.template.Configuration;
/**
* 轻量级的FreeemarkerView
*
* 不支持jsp标签、不支持request、session、application等对象,可用于前台模板页面。
*/
public class SimpleFreeMarkerView extends AbstractTemplateView {
/**
* 部署路径调用名称
*/
public static final String CONTEXT_PATH = "base";
/**
* 系统当前时间
*/
public static final String SYS_DATE = "sysTime";
private Configuration configuration;
public void setConfiguration(Configuration configuration) {
this.configuration = configuration;
}
protected Configuration getConfiguration() {
return this.configuration;
}
/**
* 自动检测FreeMarkerConfig
*
* @return
* @throws BeansException
*/
protected FreeMarkerConfig autodetectConfiguration() throws BeansException {
try {
return (FreeMarkerConfig) BeanFactoryUtils
.beanOfTypeIncludingAncestors(getApplicationContext(),
FreeMarkerConfig.class, true, false);
} catch (NoSuchBeanDefinitionException ex) {
throw new ApplicationContextException(
"Must define a single FreeMarkerConfig bean in this web application context "
+ "(may be inherited): FreeMarkerConfigurer is the usual implementation. "
+ "This bean may be given any name.", ex);
}
}
/**
* Invoked on startup. Looks for a single FreeMarkerConfig bean to find the
* relevant Configuration for this factory.
* <p>
* Checks that the template for the default Locale can be found: FreeMarker
* will check non-Locale-specific templates if a locale-specific one is not
* found.
*
* @see freemarker.cache.TemplateCache#getTemplate
*/
protected void initApplicationContext() throws BeansException {
super.initApplicationContext();
if (getConfiguration() == null) {
FreeMarkerConfig config = autodetectConfiguration();
setConfiguration(config.getConfiguration());
}
checkTemplate();
}
/**
* Check that the FreeMarker template used for this view exists and is
* valid.
* <p>
* Can be overridden to customize the behavior, for example in case of
* multiple templates to be rendered into a single view.
*
* @throws ApplicationContextException
* if the template cannot be found or is invalid
*/
protected void checkTemplate() throws ApplicationContextException {
try {
// Check that we can get the template, even if we might subsequently
// get it again.
logger.debug(getUrl());
logger.debug(this.getAttributesMap());
logger.debug(this.getStaticAttributes());
getConfiguration().getTemplate(getUrl());
} catch (ParseException ex) {
throw new ApplicationContextException(
"Failed to parse FreeMarker template for URL [" + getUrl()
+ "]", ex);
} catch (IOException ex) {
throw new ApplicationContextException(
"Could not load FreeMarker template for URL [" + getUrl()
+ "]", ex);
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void renderMergedTemplateModel(Map model,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
model.put(CONTEXT_PATH, request.getContextPath());
model.put(SYS_DATE, Calendar.getInstance().getTime());
model.put("session", request.getSession());
model.put("request", request);
getConfiguration().getTemplate(getUrl()).process(model,
response.getWriter());
}
}
相关文章推荐
- 【转】Java 中字符串的格式化
- Java学习----对象与对象之间的关系
- java equals 与 hashCode
- 使用Struts2上传时,一定要设置 文件上传大小限制。
- 使用Struts2上传时,一定要设置 文件上传大小限制。
- JVM垃圾回收描述
- springmvc 使用filter过滤的时候放置出现循环条
- java.lang.OutOfMemoryError: unable to create new native thread
- Gradle Plugin Samples 之 Gradle Eclipse Compatible (三)
- JAVA -- 使用BASE64Decoder
- java Web项目Service层通用接口和entityVo对象与entity对象转化问题的解决方案
- Java学习----创建对象的数组
- 关于Java 中 static{}此写法说明
- JAVA IO 复习
- Java多线程总结(5)— 原子性、可见性、有序性和并发库的原子性操作
- elipse导入项目中文乱码解决
- 在eclipse中统计代码的行数
- Java基础知识精华部分(个人总结)
- java客户端发送消息到服务端
- Caused by: java.lang.ClassNotFoundException:XXXXX in loader dalvik.system.PathClassLoader[/data/