本文讲到的SpringMVC使用的环境为:
1. spring包3.2
2. JDK 1.7
3. tomcat 7
4. eclipse
一、创建工程动态web工程
注意使用的编译JDK为1.7,tomcat运行JDK为1.7
二、导入相关jar包
commons-logging-1.1.3.jar
javax.servlet.jsp.jstl-api-1.2.1.jar
spring-aop-3.2.0.RELEASE.jar
spring-aspects-3.2.0.RELEASE.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-context-support-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
spring-jdbc-3.2.0.RELEASE.jar
spring-orm-3.2.0.RELEASE.jar
spring-test-3.2.0.RELEASE.jar
spring-tx-3.2.0.RELEASE.jar
spring-web-3.2.0.RELEASE.jar
spring-webmvc-3.2.0.RELEASE.jar
三、新建视图
WebRoot目录下新建jsp文件夹
jsp文件夹中新建三个jsp文件:first.jsp、second.jsp、third.jsp
此DEMO的JSP内容任意编写
四、编写Handler
a、在src目录下新建包:com.liwei.ssm.controller
b、新建如下两个Controller类
ItemsController1.java
package com.liwei.ssm.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; /** * 实现controller接口的处理器 * * @author LIWEI * @version 1.0 * @since 2016年5月27日 下午3:12:10 */ public class ItemsController1 implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { // 调用service查询数据库,查询商品列表 ModelAndView modelAndView = new ModelAndView("first"); return modelAndView; } }
ItemsController2.java
package com.liwei.ssm.controller; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.HttpRequestHandler; /** * 实现controller接口的处理器 * * @author LIWEI * @version 1.0 * @since 2016年5月27日 下午3:12:10 */ public class ItemsController2 implements HttpRequestHandler { @Override public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub // 此处完全和servlet的实现方式相同 // 使用此方法,可以设置Response响应的数据格式 PrintWriter out = response.getWriter(); out.print("second"); out.flush(); out.close(); } }
五、配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Demo02_SpringMVC</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- springmvc前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- contextConfigLocation配置springmvc加载的配置文件 --> <!-- 如果没有配置contextConfigLocation,则默认加载/WEB-INF/servlet名称-servlet.xml(此处应是springmvc-servlet.xml) --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 使用*.action或者/,不要配置为/* --> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>
六、配置springmvc.xml
创建source folder,名称为config
在config文件夹中创建springmvc.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" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 配置Handler --> <bean id="ItemsController1" name="/queryItems.action" class="com.liwei.ssm.controller.ItemsController1"></bean> <!-- 配置另一个Handler --> <bean id="ItemsController2" class="com.liwei.ssm.controller.ItemsController2"></bean> <!-- 多个映射器可以并存,前端控制器判断url能让哪些映射器处理,就让正确的映射器处理。 --> <!-- 所有映射器都实现了HandlerMapping接口 --> <!-- 处理器映射器:第一种(两种允许并存) --> <!-- 将bean的name作为url进行查找,需要在配置handler时指定beanname(就是url) --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 处理器映射器:第二种(两种允许并存) --> <!-- 简单URL映射器 --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/queryItems1.action">ItemsController1</prop> <prop key="/queryItems12.action">ItemsController1</prop> <prop key="/queryItems2.action">ItemsController2</prop> </props> </property> </bean> <!-- 处理器适配器 --> <!-- 所有处理器适配器都实现HandlerAdapter接口 --> <!-- 实现Controller接口 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> <!-- 另一个非注解的适配器 --> <!-- 要求实现HttpRequestHandler接口 --> <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean> <!-- 视图解析器 --> <!-- 支持JSP解析,默认使用jstl标签,则classpath下需要有jstl的包 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix" value="/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
七、部署访问
部署在tomcat中,不多说了
访问地址:
http://127.0.0.1:8080/Demo02_SpringMVC/queryItems.action
http://127.0.0.1:8080/Demo02_SpringMVC/queryItems1.action
http://127.0.0.1:8080/Demo02_SpringMVC/queryItems12.action
http://127.0.0.1:8080/Demo02_SpringMVC/queryItems2.action