from: http://blog.csdn.net/yakson/article/details/9813531
前言
1.为什么要使用spring MVC呢?
2.为什么要使用Freemarker呢?
3.为什么不使用Struts2呢?
此示例出现的原因就是发现了struts2的性能太差,所以学习Spring MVC。又由于前一个项目使用的是Struts2+Freemarker写的,所以要替换Struts2就需要让Spring MVC也要支持Freemarker。
项目准备
1.Spring 包
spring2.5.6.jar
spring-aop.jar
spring-beans.jar
spring-context.jar
spring-context-support.jar
spring-core.jar
spring-jdbc.jar
spring-jms.jar
spring-orm.jar
spring-test.jar
spring-tx.jar
spring-web.jar
spring-webmvc.jar
spring-webmvc-portlet.jar
spring-webmvc-struts.jar
2.Freemarker包
freemarker-2.3.19.jar
加粗的部分为项目需要引入的包。
开始我们的整合之旅吧!
开发工具:NetBeans IDE 7.3.1
一、新建web项目springMVCAndFreemarker
二、将jar包引入项目
spring.jar
spring-webmvc.jar
freemarker.jar
依赖包
commons-logging.jar
三、配置web.xml
WEB-INF/web.xml
-
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </context-param>
-
-
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <servlet>
- <servlet-name>springmvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>springmvc</servlet-name>
- <url-pattern>/*</url-pattern>
- </servlet-mapping>
四、添加applicationContext.xml文件
src/applicationContext.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
-
- </beans>
接着在内容中添加Freemarker的支持配置
-
- <bean id="freemarkerConfig"
- class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
- <property name="templateLoaderPath" value="/WEB-INF/view/" />
- <property name="freemarkerSettings">
- <props>
- <prop key="template_update_delay">0</prop>
- <prop key="default_encoding">UTF-8</prop>
- <prop key="number_format">0.##########</prop>
- <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
- <prop key="classic_compatible">true</prop>
- <prop key="template_exception_handler">ignore</prop>
- </props>
- </property>
- </bean>
五、添加spring MVC的servlet配置文件,命名规则(servlet-name+servlet.xml)
WEB-INF/springmvc-servlet.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
-
- </beans>
接着在内容中添加Spring MVC的配置
-
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
- <property name="suffix">
- <value>.ftl</value>
- </property>
- <property name="contentType" value="text/html;charset=UTF-8"></property>
- </bean>
六、创建控制器Controller类
com.qisentech.springmvc.action.HelloWordController.Java
-
-
-
-
-
- package com.qisentech.springmvc.action;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.web.servlet.ModelAndView;
- import org.springframework.web.servlet.mvc.Controller;
-
-
-
-
-
-
-
-
-
- public class HelloWordController implements Controller {
-
- @Override
- public ModelAndView handleRequest(HttpServletRequest hsr
- , HttpServletResponse hsr1) throws Exception {
- ModelAndView mv = new ModelAndView("hello");
- mv.addObject("title", "Spring MVC And Freemarker");
- mv.addObject("content", " Hello world , test my first spring mvc ! ");
- return mv;
- }
- }
代码中new ModelAndView("hello")中的hello就是模板文件的名称
由于我们的Freemarker的模板文件是配置在WEB-INF/view下,参考applicationContext.xml配置
<property name="templateLoaderPath" value="/WEB-INF/view/" />
所以我们需要在WEB-INF/view下新建模板文件,名称(hello.ftl)
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
- "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>${title}</title>
- </head>
- <body>
- ${content}
- </body>
- </html>
七、将控制器交给Spring MVC进行拦截
添加以下内容到springmvc-servlet.xml
-
- <bean name="HelloAction" class="com.qisentech.springmvc.action.HelloWordController" />
- <bean id="urlMapping"
- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property name="mappings">
- <props>
-
- <prop key="/hello">HelloAction</prop>
- </props>
- </property>
- </bean>
以上步骤处理好,我们就可以运行了
访问地址:http://localhost:8088/springMVCAndFreemarker/hello
页面输出:
Hello world , test my first spring mvc !
就此,我们的第一个Spring MVC + Freemarker的示例结束了!
需要源码的,可以留言,留下邮箱,我看到了给你发过去!
附上项目结构图: