springMVC入门程序
一、环境搭建
二、配置前端控制器
在web.xml中配置前端控制器,前端控制器的本质是一个Servlet所以其配置方式,和Servlet一样
<!-- springmvc前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>
这个的话,那处理器映射器、处理器适配器、视图解析器还都没有呢,还是不能工作。
<!-- springmvc前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- contextConfigLocation:配置springmvc要加载的组件(处理器映射器、处理器适配器、视图解析器。。。) contextConfigLocation:默认加载路径:/WEB-INF/servlet-name-servlet.xml(springmvc-servlet.xml) --> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- *.action:访问以。action结尾的由DispatcherServlet解析 /:所有访问地址都由DispatcherServlet解析,对于静态文件的解析需要配置不让DispatcherServlet解析,测试方式可以实现RESTful风格url --> <url-pattern>*.action</url-pattern> </servlet-mapping>
三、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: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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> </beans>
四、配置处理器映射器
①、有哪些处理器映射器
内容如下:
# Default implementation classes for DispatcherServlet's strategy interfaces.
# Used as fallback when no matching beans are found in the DispatcherServlet context.
# Not meant to be customized by application developers.
org.springframework.web.servlet.LocaleResolver=org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver
org.springframework.web.servlet.ThemeResolver=org.springframework.web.servlet.theme.FixedThemeResolver
//处理器映射器
org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,\
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
//处理器适配器
org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
org.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver,\
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,\
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
org.springframework.web.servlet.RequestToViewNameTranslator=org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator
org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver
org.springframework.web.servlet.FlashMapManager=org.springframework.web.servlet.support.SessionFlashMapManager
②、配置处理器映射器,根据url获取Handler
<!-- 处理器映射器 将bean的name作为url精选查找 如下: <bean name="/xxx.action" class="xxxxx.xxxController"/> --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
五、配置处理器适配器
<!-- 处理器适配器 执行实现Controller的Handler --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
六、编写Handler,并交给springmvc管理
//需要实现 controller接口,才能由org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter适配器执行 public class ItemsController implements Controller { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { List<Items> itemsList=new ArrayList<Items>(); Items items_1 = new Items(); items_1.setName("笔记本"); items_1.setPrice(6000f); items_1.setDetail("笔记本电脑!"); Items items_2 = new Items(); items_2.setName("手机"); items_2.setPrice(5000f); items_2.setDetail("手机!"); itemsList.add(items_1); itemsList.add(items_2); //返回ModelAndView ModelAndView modelAndView = new ModelAndView(); //相当 于request的setAttribut,在jsp页面中通过itemsList取数据 modelAndView.addObject("itemsList", itemsList); //指定视图 modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp"); return modelAndView; } }
将编写Handler在spring容器加载,name为路径方式字符串
<bean name="/queryItem.action" class="com.xxx.springmvc.controller.ItemsController"/>
七、编写视图
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <!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> </head> <body> <form action="${pageContext.request.contextPath }/queryItem.action" method="post"> 查询条件: <table width="100%" border=1> <tr> <td><input type="submit" value="查询"/></td> </tr> </table> 商品列表: <table width="100%" border=1> <tr> <td>商品名称</td> <td>商品价格</td> <td>生产日期</td> <td>商品描述</td> <td>操作</td> </tr> <c:forEach items="${itemsList }" var="item"> <tr> <td>${item.name }</td> <td>${item.price }</td> <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td> <td>${item.detail }</td> <td><a href="${pageContext.request.contextPath }/item/editItem.action?id=${item.id}">修改</a></td> </tr> </c:forEach> </table> </form> </body> </html>
八、配置视图解析器
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
九、发布访问:http://localhost:8080/02springmvc/queryItem.action
简单的示例就完成了。
总结:
1、springmvc是基于servlet的,所以需要配置和servlet一致,配置前端控制器
2、处理器映射器根据url对应的bean name 查到对应的Handler
3、处理器适配器执行所有实现Controller的Handler
3、Handler返回模型数据(数据和视图名)
4、通过视图解析器根据视图名获取视图对象,后面操作就不赘述了