天天看點

Freemarker SpringMVC 前台無法使用session;request等對象問題

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;

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;

}

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);

}

}

protected void initApplicationContext() throws BeansException {

super.initApplicationContext();

if (getConfiguration() == null) {

FreeMarkerConfig config = autodetectConfiguration();

setConfiguration(config.getConfiguration());

}

checkTemplate();

}

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());

}

}