- 浏览: 616460 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (228)
- io (15)
- cluster (16)
- linux (7)
- js (23)
- bizarrerie (46)
- groovy (1)
- thread (1)
- jsp (8)
- static (4)
- cache (3)
- protocol (2)
- ruby (11)
- hibernate (6)
- svn (1)
- python (8)
- spring (19)
- gma (1)
- architecture (4)
- search (15)
- db (3)
- ibatis (1)
- html5 (1)
- iptables (1)
- server (5)
- nginx (4)
- scala (1)
- DNS (1)
- jPlayer (1)
- Subversion 版本控制 (1)
- velocity (1)
- html (1)
- ppt poi (1)
- java (1)
- bizarrerie spring security (1)
最新评论
-
koreajapan03:
楼主啊,好人啊,帮我解决了问题,谢谢
自定义过滤器时,不能再使用<sec:authorize url="">问题 -
snailprince:
请问有同一页面,多个上传实例的例子吗
webuploader用java实现上传 -
wutao8818:
姚小呵 写道如何接收server返回的参数呢?例如你返回的是“ ...
webuploader用java实现上传 -
姚小呵:
如何接收server返回的参数呢?例如你返回的是“1”,上传的 ...
webuploader用java实现上传 -
zycjf2009:
你好,我想用jplayer做一个简单的播放器,但是因为对js不 ...
jplayer 实战
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="336" height="280" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"> <param name="src" value="http://img.alimama.cn/bm/wrappler_swf/2009-11-26/2042702_0af5658d088e285c4ac474cd20865090_336x280.swf?v=1259240549"> <embed type="application/x-shockwave-flash" width="336" height="280" src="http://img.alimama.cn/bm/wrappler_swf/2009-11-26/2042702_0af5658d088e285c4ac474cd20865090_336x280.swf?v=1259240549"></embed></object>
http://forum.springframework.org/showthread.php?t=53476
I hope this thread will help others who are trying to combine these
technologies. After much pain, I finally got it working exactly the way
I want.
My goal was to build a website using Freemarker for templating,
SiteMesh for layouts, and Spring MVC. Normally you would just put the
Freemarker files in WEB-INF/freemarker.
However, this site is one of many we build that re-use the same Content
Management Admin screens - so we wanted the Freemarker templates for
those forms in the classpath. But we sometimes need to override those
admin screens with customizations in a specific project. To enable
customizations, we want to be able to first try to load the Freemarker
template from the WAR file in /modules/
, and if it's not found there, load it from the classpath package modules
.
Additionally, each website will have its site-specific HTML content written in Freemarker - these files be stored in /WEB-INF/freemarker
.
Summary:
* The content of a site-specific web page is written in Freemarker and stored in /WEB-INF/freemarker
.
* Site-specific SiteMesh decorators are written in Freemarker and stored in the website's /decorators/
directory. (This could probably be in /WEB-INF/freemarker/decorators)
* Re-usable, default admin screens are written in Freemarker and stored in classpath:modules
.
* If needed, a developer can override an admin screen for the site by copying the freemarker file from classpath:modules
into the site's /modules.
folder. Then they can make any customizations needed.
We also use Freemarker in the service tier to generate emails, so we can't just follow the Reference Guide instructions for using Freemarker in the web tier
. Instead, we have to use Spring's FreeMarkerConfigurationFactoryBean
class to create a custom Freemarker Configuration
bean.
applicationContext-freemarker.xml
<bean id="freemarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean"> <description>Using the Config directly so we can use it outside the web tier</description> <property name="templateLoaderPaths"> <list> <value>/WEB-INF/freemarker/</value> <value>/modules/</value> <value>/</value> <value>classpath:modules</value> <value>classpath:org/springframework/web/servlet/view/freemarker</value> </list> </property> <property name="freemarkerSettings"> <props> <prop key="datetime_format">MM/dd/yyyy</prop> <prop key="number_format">#</prop> <prop key="whitespace_stripping">true</prop> </props> </property> <property name="freemarkerVariables"> <map> <entry key="xml_escape" value-ref="fmXmlEscape" /> <entry key="html_escape" value-ref="fmHtmlEscape" /> </map> </property> </bean> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <description>Required for Freemarker to work in web tier</description> <property name="configuration" ref="freemarkerConfiguration" /> </bean> <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" /> <bean id="fmHtmlEscape" class="freemarker.template.utility.HtmlEscape" />
In the freemarkerConfiguration
bean above, the important setting is the templateLoaderPaths
. When Freemarker tries to find a template file, it will look for the template in the following order:
* /WEB-INF/freemarker
* /modules/
* / (the site's root - important for decorators)
* The modules package in the classpath - this could be a JAR file in WEB-INF/lib or in WEB-INF/classes
* The org/springframework/web/servlet/view/freemarker package on the
classpath - this is so we can reuse Spring's Freemarker Macros
By Default, The Freemarker Servlet creates a new Configuration object, which will only
load templates from the Servlet Context, a physical directory, OR a
classpath location (in other words, it does not use
freemarker.cache.MultiTemplateLoader). SiteMesh extends the
FreemarkerServlet to do its decorating. Since we need to re-use the
Configuration created in applicationContext-freemarker.xml
(which loads templates from multiple locations), I needed to extend com.opensymphony.module.sitemesh.freemarker.Freema rkerDecoratorServlet
. I wrote the following Subclass:
package com.kazaam.sitemesh.servlet; import java.io.IOException; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.opensymphony.module.sitemesh.freemarker.FreemarkerDecoratorServlet; import freemarker.cache.TemplateLoader; import freemarker.template.Configuration; /** * @author mstralka * */ public class KzSpringFreemarkerDecoratorServlet extends FreemarkerDecoratorServlet { @Override public void init() throws ServletException { super.init(); WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); Configuration springConfiguration = (Configuration) ctx.getBean("freemarkerConfiguration"); TemplateLoader templateLoader = springConfiguration.getTemplateLoader(); getConfiguration().setTemplateLoader(templateLoader); } }
Normally, The SiteMesh FreemarkerDecoratorServlet is configured
in web.xml. Since we want to re-use our Spring Interceptors, we are
going to use Spring's ServletWrappingController to configure the
servlet as a Spring bean instead.
In spring-servlet.xml
(or whatever you call your Spring
MVC XML file), add the following bean (which wraps the Servlet in a
controller so we can re-use our Spring MVC interceptors for Hibernate,
etc):
<bean id="freemarkerWrapperServletController" class="org.springframework.web.servlet.mvc.ServletWrappingController"> <property name="servletClass" value="com.kazaam.sitemesh.servlet.KzSpringFreemarkerDecoratorServlet" /> <property name="servletName" value="sitemesh-freemarker" /> <property name="initParameters"> <props> <prop key="TemplatePath">/</prop><!--this is ignored by our custom implementation but keep it here--> <prop key="default_encoding">ISO-8859-1</prop> </props> </property> </bean>
Add the following to your UrlMapping Bean's mappings property:
<prop key="/**/*.ftl">freemarkerWrapperServletController</prop> <prop key="/**/*.ftd">freemarkerWrapperServletController</prop>
In web.xml, make sure *.ftl and *.ftd are handled by your Spring dispatcher servlet (my dispatcher servlet is named spring ):
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
In addition to whatever other URL patterns you let Spring handle, add the following so the Freemarker templates are handled by Spring too:
<servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.ftd</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.ftl</url-pattern> </servlet-mapping>
To complete the SiteMesh configuration, here is my sitemesh.xml file in the /WEB-INF/ directory (Note that I name my decorators.xml file sitemesh-decorators.xml file instead because I like them to appear next to each other alphabetically in Eclipse)
<sitemesh> <property name="decorators-file" value="/WEB-INF/sitemesh-decorators.xml" /> <excludes file="/WEB-INF/sitemesh-decorators.xml" /> <page-parsers> <parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" /> <parser content-type="text/html;charset=ISO-8859-1" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" /> </page-parsers> <decorator-mappers> <mapper class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper"> <param name="property.1" value="meta.decorator" /> <param name="property.2" value="decorator" /> </mapper> <mapper class="com.opensymphony.module.sitemesh.mapper.FrameSetDecoratorMapper"/> <mapper class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper"> <param name="decorator" value="printable" /> <param name="parameter.name" value="printable" /> <param name="parameter.value" value="true" /> </mapper> <mapper class="com.opensymphony.module.sitemesh.mapper.FileDecoratorMapper"/> <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper"> <param name="config" value="/WEB-INF/sitemesh-decorators.xml" /> </mapper> </decorator-mappers> </sitemesh>
And here is my sitemesh-decorators.xml file (yours will be different):
<decorators defaultdir="/decorators"> <!-- SiteMesh Decorator Mappings: See http://opensymphony.com/sitemesh/dm.html --> <!-- No decorator --> <excludes> <pattern>/misc/*</pattern> <pattern>/dwr/*</pattern> <pattern>/articles/rss.*</pattern> <pattern>/dwr/index.html</pattern> </excludes> <decorator name="main" page="main.ftd"> <url-pattern>*</url-pattern> </decorator> <decorator name="authoring" page="authoring.ftd"> <url-pattern>/page/create*</url-pattern> <url-pattern>/page/edit*</url-pattern> <url-pattern>/article/create*</url-pattern> <url-pattern>/article/edit*</url-pattern> </decorator> <decorator name="popup" page="popup.ftd"> <url-pattern>/page/wikihelp*</url-pattern> <url-pattern>/page/help*</url-pattern> </decorator> </decorators>
发表评论
-
webuploader用java实现上传
2014-10-17 23:55 40774<div id="uploader&q ... -
springmvc,Servlet3异步,websocket
2014-10-17 22:58 1445spring4.0以后加入了对websocket技术的支持 ... -
SpringMVC中的文件上传【转发】
2014-09-19 21:54 1045http://blog.csdn.net/jadyer/art ... -
{转}spring security 3 自定义认证授权示例
2011-11-10 16:46 2319转:http://www.4ucode.com/Study/T ... -
用spring resource 接口获取 web应用的ROOT目录
2010-12-09 19:44 3167Resource r = new ClassPathResou ... -
springmvc 表单 @RequestMapping 上传文件
2010-06-04 18:08 6542@RequestMapping(method = Req ... -
ControllerClassNameHandlerMapping @Controller
2009-06-27 01:25 4446这里也提到这个问题 http://forum.springs ... -
spring MessageSource
2009-06-12 13:06 1306http://www.blogjava.net/xzclog/ ... -
PagedListHolder
2009-03-26 23:32 1693org.springframework.beans.suppo ... -
HOWTO: Use Freemarker, SiteMesh, and Spring MVC(2)
2009-03-19 23:52 2035Here's an example of the Freema ... -
spring mvc & freemarker 版ROR
2008-06-19 01:27 3571实现COC原则无配置自动映射 引用http://localho ... -
spring mvc 也能实现 ror 类似的URL路由
2008-06-18 18:54 3583看过ror的朋友一定知道它所体现的一个核心思想就是惯例优先原则 ... -
重头开始学 Spring JPetStore 5
2008-06-18 18:25 2047重头开始学 Spring JPetStore 4 里面的Con ... -
重头开始学 Spring JPetStore 4
2008-06-18 15:42 2469跟踪一个 .do 请求吧 就它了。 Enter the S ... -
重头开始学 Spring JPetStore 3
2008-06-18 14:21 2315按 重头开始学 Spring JPetStore 2 中的提示 ... -
重头开始学 Spring JPetStore 2
2008-06-17 11:50 17742. BUILD AND DEPLOYMENT 编译和发布 ... -
重头开始学 Spring JPetStore 1
2008-06-17 11:35 1658先看看它的readme.txt 引用@author Juer ... -
一个关于Spring的好消息及Spring Batch概述
2008-05-26 00:45 56883年前,即将大学毕业,遇到了spring,还以SSH框架为题做 ...
相关推荐
Spring MVC 是一个强大...在Spring MVC中集成FreeMarker,首先需要在项目的配置文件(如`servlet-context.xml`)中配置FreeMarker视图解析器。这通常包括指定模板目录、编码格式和是否缓存模板等设置。例如: ```xml ...
1. **配置POM.xml**:在项目的`pom.xml`文件中添加Spring MVC、Tiles和Freemarker的依赖。确保版本兼容,通常最新的稳定版本是首选。 2. **配置Spring MVC**:在Spring的配置文件(如`app-servlet.xml`)中,配置...
Spring MVC 是一个基于Java的轻量级Web应用框架,它是Spring框架的重要组成部分,主要用于构建Web应用程序的后端控制器。这个教程“Spring MVC - A Tutorial”旨在帮助开发者深入理解和掌握Spring MVC的核心概念和...
Spring MVC 和 Freemarker 是两种广泛应用于Java Web开发的技术。Spring MVC 是一个基于Spring框架的Model-View-Controller架构,用于构建高效、灵活的Web应用程序。而Freemarker 是一款强大的模板引擎,它与后端...
4. **View**:视图负责渲染模型数据,Spring MVC支持多种视图技术,如JSP、FreeMarker、Thymeleaf等。 5. **HandlerMapping**:负责将请求映射到特定的控制器方法。 6. **HandlerAdapter**:适配器模式,使得Spring ...
本项目结合了Freemarker、Spring Security、Spring MVC和Spring Data JPA,旨在实现前端JTable的简单CRUD(创建、读取、更新、删除)功能。以下是这些技术的详细介绍及其在项目中的应用。 **Freemarker** 是一个...
Spring MVC 是一款强大的Java Web开发框架,用于构建高效、可维护和模块化的Web应用程序。它作为Spring框架的一部分,提供了一种优雅的方式来处理HTTP请求和响应,使得开发者可以专注于业务逻辑而不是底层实现。在这...
Spring MVC支持多种视图技术,如JSP、Thymeleaf、FreeMarker等。 5. **View**: 视图负责渲染模型中的数据并呈现给用户。它通常与某种模板语言结合,如JSP中的EL(Expression Language)和JSTL(JavaServer Pages ...
1. **配置Spring MVC**:首先,我们需要在Spring的配置文件中定义DispatcherServlet,设置视图解析器为FreeMarkerViewResolver,并配置FreeMarker的配置路径。 2. **配置MyBatis**:接着,配置MyBatis的...
- 视图(View):负责渲染模型数据,Spring MVC支持多种视图技术如JSP、FreeMarker、Thymeleaf等。 5. **数据绑定与验证**: - 数据绑定:Spring MVC可以自动将请求参数绑定到Controller方法的参数上。 - 数据...
标题中的"开发Spring MVC应用程序补充—程序源码下载.rar_spring_spring mvc_spring mvc 源码_sp"表明这是一个关于Spring MVC框架的开发教程,其中包含了源代码供学习者参考。Spring MVC是Spring框架的一个核心组件...
1. **依赖注入**:Spring MVC 4.0继续支持Spring框架的核心功能,依赖注入(DI),允许开发者通过配置来管理对象及其依赖关系,降低了代码耦合度,提高了可测试性。 2. **ModelAndView对象**:在处理完请求后,控制...
7. **多视图解析器**:Spring MVC支持多种视图解析器,如JSP、FreeMarker、Thymeleaf等,可以根据项目需求灵活选择。 8. **模板引擎集成**:例如,与Thymeleaf的集成使得开发者能编写声明式逻辑的模板,提高了视...
**Spring MVC3 集成 FreeMarker 概述** Spring MVC 是一个强大的MVC框架,用于构建基于Java的Web应用程序。它提供了模型、视图和控制器的分离,使得开发过程更加模块化,易于维护。而FreeMarker则是一个模板引擎,...
**Spring MVC与FreeMarker整合应用详解** Spring MVC是Spring框架的一部分,它是一个强大的Web应用程序开发模型,用于构建灵活、可维护的Java Web应用。而FreeMarker则是一个模板引擎,允许开发者将逻辑代码与呈现...
1. **Spring MVC基本概念**:首先,了解Spring MVC的基本架构,包括DispatcherServlet、Controller、Model、View和ViewResolver等组件的角色和交互方式。 2. **配置Spring MVC**:学习如何通过XML或Java配置来设置...
Freemarker和Sitemesh是两个在Web开发中常用的开源技术。Freemarker是一个模板引擎,主要用于生成动态HTML或其他文本格式的输出,而Sitemesh则是一个页面布局和装饰框架,用于统一网站的外观和感觉。这两者的整合...
- **View**: 负责呈现视图,Spring MVC 支持多种视图技术,如 JSP、FreeMarker、Thymeleaf 等。 - **HandlerMapping**: 将请求映射到处理器,Spring 2.5 中主要通过 XML 配置来实现。 - **HandlerAdapter**: 适配...
Spring MVC 和 FreeMarker 是两个在Java Web开发中广泛使用的框架,它们共同构建了一个高效、灵活的Web应用程序。Spring MVC是Spring框架的一部分,主要用于处理HTTP请求和响应,而FreeMarker则是一个模板引擎,用于...