先搭建普通mvc工程,测试成功后,集成freemarker并将注入方式改为注解。
第一步:创建web工程,取名smvc-test
第二步:引入jar包,spring-framework-3.1.1.RELEASE-with-docs.zip中dist下的所有jar,引入freemarker-2.3.15.jar(freemarker的依赖包),commons.logging-1.1.1.jar,jstl-1.2.jar。
第三步:修改web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-*.xml
</param-value>
</context-param>
-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 中英文过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
第四步:创建名为dispatch-servlet.xml文件(XXX-servlet.xml,XXX可自定义,但是名称要与web.xml中配置的一致)。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<!-- HandlerMapping -->
<bean
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!-- HandlerAdapter -->
<bean
class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<!-- ViewResolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
第六步:创建Controller类
package cn.smvc.web.controller;
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 HelloWorldController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
ModelAndView mv = new ModelAndView();
//添加模型数据 可以是任意的POJO对象
mv.addObject("message", "Hello World!");
//设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面
mv.setViewName("hello");
return mv;
}
}
第七步:创建jsp文件
<html>
<head>
<title>Hello World</title>
</head>
<body>
${message}
</body>
</html>
第八步:在spring配置文件中添加控制器类的配置文件
<bean name="/hello"
class="cn.smvc.web.controller.HelloWorldController" />
将工程发布测试,访问http://localhost:8080/smvc-test/hello,成功返回则表示springmvc工程初步搭建成功。
第九步:集成freemarker并改注解注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
<span style="white-space:pre"> </span>xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<span style="white-space:pre"> </span>xmlns:context="http://www.springframework.org/schema/context"
<span style="white-space:pre"> </span>xmlns:mvc="http://www.springframework.org/schema/mvc"
<span style="white-space:pre"> </span>xsi:schemaLocation="
<span style="white-space:pre"> </span>http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
<span style="white-space:pre"> </span>http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
<span style="white-space:pre"> </span>http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<span style="white-space:pre"> </span><context:component-scan base-package="cn"/>
<mvc:annotation-driven/>
<span style="white-space:pre"> </span><!-- HandlerMapping -->
<span style="white-space:pre"> </span><bean
<span style="white-space:pre"> </span>class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<span style="white-space:pre"> </span><!-- HandlerAdapter -->
<span style="white-space:pre"> </span><bean
<span style="white-space:pre"> </span>class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<span style="white-space:pre"> </span><bean id="freeMarkerViewResolver"
<span style="white-space:pre"> </span>class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<span style="white-space:pre"> </span><property name="cache" value="true" />
<span style="white-space:pre"> </span><property name="prefix" value="" />
<span style="white-space:pre"> </span><property name="suffix" value=".ftl" />
<span style="white-space:pre"> </span><property name="order" value="1" />
<span style="white-space:pre"> </span><property name="contentType">
<span style="white-space:pre"> </span><value>text/html;charset=utf-8</value>
<span style="white-space:pre"> </span></property>
<span style="white-space:pre"> </span></bean>
<span style="white-space:pre"> </span><bean id="freemarkerConfig"
<span style="white-space:pre"> </span>class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<span style="white-space:pre"> </span><property name="templateLoaderPath" value="/WEB-INF/view/" />
<span style="white-space:pre"> </span><property name="freemarkerSettings">
<span style="white-space:pre"> </span><props>
<span style="white-space:pre"> </span><prop key="defaultEncoding">utf-8</prop>
<span style="white-space:pre"> </span></props>
<span style="white-space:pre"> </span></property>
<span style="white-space:pre"> </span></bean>
</beans>
第十步:创建
Controller类
<pre name="code" class="java">import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.util.UrlPathHelper;
@Controller
@RequestMapping(value = "/syk")
public class ExampleController {
@RequestMapping(value = "/html", method = RequestMethod.GET)
public String hello(HttpServletRequest request, HttpServletResponse response, Object handler, Model m) {
m.addAttribute("user", "CyrusLeo");
String requestUri = new UrlPathHelper().getLookupPathForRequest(request);
m.addAttribute("appUrl", requestUri);
m.addAttribute("appName", "Test");
m.addAttribute("appHtml",requestUri);
return "demo/helloworld";
}
}
第十一步:创建 视图页面
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Welcome ${user}!</h1>
<a
href="${appUrl}">${appName}</a>
</body>
</html>
将工程发布,访问http://localhost:8080/smvc-test/syk/html 正确显示则成功
ps:最后发布时需能在日志中看到如下信息才能表示注释功能配置成功
信息: Mapped "{[/syk/html],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}"
自定义访问路径
http://localhost:8080/smvc/syk/html
附上工程目录层次