概念:
通俗的讲,SpringMVC就相当于WEB 中的servletg功能,它就是一个框架主要实现逻辑控制,进行实现页面跳转之类的,但其更灵活,它是通过一套MVC注解让POJO成为处理请求的控制器,无需实现任何接口,同时,SpringMVC还支持 REST风格的URL请求。此外,SpringMVC在数据绑定、视图解析、本地化处理及静态资源处理上都有许多不俗的 表现。 它在框架设计、扩展性、灵活性等方面全面超越了Struts、WebWork等MVC框架,从原来的追赶者一跃成为MVC 的领跑者。 SpringMVC框架围绕DispatcherServlet(前端控制器)这个核心展开,DispatcherServlet是SpringMVC框架的总 导演、总策划,它负责截获请求并将其分派给相应的处理器处理。废话不多说,见下图:
由上图可以很清楚的看出SpringMVC主要实现Control层。因此它是数据模型与图形化的枢纽。既然是枢纽,如此重,那继续向下看,一张图让你搞清SpringMVC内部框架。
由上图可以看到SpringMVC中主要由处理器、处理器适配器、处理器映射器、前端控制器、以及视图解析器构成,缺一不可。 其中每个处理器的作用见上图标记,其中上图已将所有关系以及作用表达的明明白白。
以下进入代码演示阶段:
说明:由于此处先不连接数据库,因此数据库需要建立模拟数据库,采用模拟数据,若有兴趣可关注见下次博客(SpringMVC and JDBC结合使用)以及(SpringMVC and Mybaties结合使用),这两篇博客均会完美的结合数据库与SpringMVC。本博客提供三种方式,但第一和第二种方式过于复杂,适合了解,实际中常用第三种方法:
方法一:
1.首先写一类提供Setter and Getter方法作为模型层
package com.ping.VO;
public class Items {
private int ItemsId;
private String ItemsName;
private int ItemsPrice;
public Items(){
}
public int getItemsId() {
return ItemsId;
}
public void setItemsId(int itemsId) {
ItemsId = itemsId;
}
public String getItemsName() {
return ItemsName;
}
public void setItemsName(String itemsName) {
ItemsName = itemsName;
}
public int getItemsPrice() {
return ItemsPrice;
}
public void setItemsPrice(int itemsPrice) {
ItemsPrice = itemsPrice;
}
}
2.对处理器、处理器适配器、处理器映射器、以及视图解析器的配置(springmvc-servlet.xml)
<?xml version="1.0" encoding="ISO-8859-1"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
<!--方法一-->
<!--处理器映射器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!--处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!--处理器-->
<bean id="handler1" name="/handler1.action" class="com.ping.controller.Hander1"/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsps/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
3.(control)的配置,由于此处举例,就把模拟数据库放入应该类中;
package com.ping.controller;
import com.ping.VO.Items;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
public class Hander1 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
/**模拟数据库
*
*/
List<Items> itemsList=new ArrayList <> ( );
Items items1 = new Items();
items1.setItemsId(1);
items1.setItemsName("arvin");
items1.setItemsPrice(200);
Items items2 = new Items();
items2.setItemsId(2);
items2.setItemsName("arvin2");
items2.setItemsPrice(200);
Items items3 = new Items();
items3.setItemsId(3);
items3.setItemsName("arvin3");
items3.setItemsPrice(200);
itemsList.add(items1);
itemsList.add(items2);
itemsList.add(items3);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemsListKey",itemsList);
modelAndView.setViewName("showItemsList");
return modelAndView;
}
}
4.总控制器的配置,也就是web.xml文件的配置;
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
5.jsp图形化页面的书写
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table border="1" align="center">
<tr>
<th>ItemsId</th>
<th>ItemsName</th>
<th>ItemsPrice</th>
</tr>
<c:forEach items="${itemsListKey}" var="items">
<tr>
<td>${items.itemsId}</td>
<td>${items.itemsName}</td>
<td>${items.itemsPrice}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
注意:以上程序基于maven与Tomcat配置环境下运行
方法二:
除过处理器的配置方式不同及control不同,其它方法参照方法一:
.对处理器、处理器适配器、处理器映射器、以及视图解析器的配置(springmvc-servlet.xml)
<?xml version="1.0" encoding="ISO-8859-1"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!--???:?????-->
<!--??????-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/abc.action">handler2</prop>
</props>
</property>
</bean>
<!--??????-->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
<!--???-->
<bean id="handler2" class="com.ping.collter.Handler2"/>
<!--?????-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsps/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--??????????,???,???-->
<context:component-scan base-package="com.ping.collter"/>
<!-- Mapping + Adapter-->
<mvc:annotation-driven/>
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
-->
</beans>
control配置
package com.ping.collter;
import com.ping.VO.Items;
import org.springframework.web.HttpRequestHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Handler2 implements HttpRequestHandler {
@Override
public void handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
List<Items> itemsList = new ArrayList<Items>();
Items items1 = new Items();
items1.setItemsId(1);
items1.setItemsName("arvin");
items1.setItemsPrice(200);
Items items2 = new Items();
items2.setItemsId(2);
items2.setItemsName("arvin2");
items2.setItemsPrice(200);
Items items3 = new Items();
items3.setItemsId(3);
items3.setItemsName("arvin3");
items3.setItemsPrice(200);
itemsList.add(items1);
itemsList.add(items2);
itemsList.add(items3);
//ModelAndView
request.setAttribute("itemsListKey",itemsList);
request.getRequestDispatcher("/jsps/showItemsList.jsp").forward(request,response);
}
}
方法三:
由于方法一的集中处理器过于复杂,在开发中严重影响开发效率,然后提供以下方法,
除过处理器的配置方式不同,其它方法参照方法一:
.对处理器、处理器适配器、处理器映射器、以及视图解析器的配置(springmvc-servlet.xml)
<?xml version="1.0" encoding="ISO-8859-1"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<context:component-scan base-package="com.ping.controll"/>
<!--除视图解析器以外的其它几个处理器-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsps/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>