添加freemarker的shiro标签
程序员文章站
2022-04-29 21:46:36
...
上次写的集成shiro的标签是直接通过maven导入的jar包,但是发现引入了很多无用的jar。现在直接通过代码导入shiro的标签。shiro标签可以在http://download.csdn.net/detail/nightliar/9838637处下载。
1.首先新建一个BaseFreemarkView.java类,继承freemarker的视图类FreeMarkerView。
package com.business.util;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.servlet.view.freemarker.FreeMarkerView;
import com.business.shiro.freemarker.ShiroTags;
import com.common.web.util.RequestUtils;
public class BaseFreemarkView extends FreeMarkerView {
@Override
protected void exposeHelpers(Map<String, Object> model,HttpServletRequest request) throws Exception {
model.put("base", RequestUtils.getDomain(request));
model.put(Constant.IMAGE_DOMAIN, Configurer.getProperty(Constant.IMAGE_DOMAIN));
model.put(Constant.FRONT_DOMAIN, Configurer.getProperty(Constant.FRONT_DOMAIN));
model.put(Constant.DOWNLOAD_DOMAIN, Configurer.getProperty(Constant.DOWNLOAD_DOMAIN));
model.put("base", RequestUtils.getDomain(request));
model.put("shiro", new ShiroTags());//shiro标签
super.exposeHelpers(model, request);
}
}
2.在spring-mvc-servlet.xml中修改freemarker的配置,将BaseFreemarkView配置进去。
<!-- 配置SpringMVC的视图解析器 -->
<!-- FreeMarker的配置 -->
<bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/template/" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">0</prop>
<prop key="defaultEncoding">UTF-8</prop>
<prop key="url_escaping_charset">UTF-8</prop>
<prop key="locale">zh_CN</prop>
<prop key="boolean_format">true,false</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="number_format">#.##</prop>
<prop key="whitespace_stripping">true</prop>
<prop key="classic_compatible">true</prop>
<prop key="template_exception_handler">ignore</prop>
</props>
</property>
</bean>
<!-- 配置 FreeMarker视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass" value="com.business.util.BaseFreemarkView" />
<property name="cache" value="true" />
<property name="order" value="0" />
<property name="prefix" value="" />
<property name="suffix" value=".html" />
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="requestContextAttribute" value="request" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
</bean>
这个和以前配置的不一样,这里只是在视图里面配置,所以只会再视图里显示,在shiro的那篇文章中是在全局配置的。这里还在BaseFreemarkView里加入了一些通用的全局变量。