springMvc是什么
springmvc是表现层的框架,是一个spring的表现层组件。是整个spring框架的一部分,可是也能够不使用springmvc。跟struts2框架功能相似。其中的mvc指的是,表现层的Model、View、controller。前端
springMvc能够作什么
springmvc能够接收用户的数据以及将数据显示给用户。java
springMvc的工做原理
springMvc的入门程序
下面,咱们一块儿来建立一个基于springmvc框架的小demo。web
第一步:建立一个dynamic web project 项目,取名为springmvc。spring
第二步:将咱们的程序用到的jar包导入webContent目录下的WEB-INF目录下的lib文件夹中,具体以下图所示:浏览器
第三步:建立springmvc.xml文件spring-mvc
这是springmvc的核心配置文件,能够在该文件中配置一些springmvc的设置,如开启注解、注解驱动、试图解析器等。若是没有配置该文件,框架默认会去找/WEB-INF/springMvc0917-servlet.xml文件,若是依然找不到,那么就会抛出异常。mvc
该文件代码以下:app
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 开启注解扫描, 这样@Controller, @Service等注解才会生效 --> <context:component-scan base-package="cn.itcast.controller"></context:component-scan> </beans>
第四步:配置前端控制器,在web.xml中添加DispatcherServlet的配置。将springmvc框架经过web.xml文件,配置到咱们的系统中。而且将springMvc.xml中的配置注入到咱们的框架中去。框架
web.xml中的代码以下:

1 <!-- 前端控制器 --> 2 <servlet> 3 <servlet-name>springmvc</servlet-name> 4 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 5 <init-param> 6 <param-name>contextConfigLocation</param-name> 7 <param-value>classpath:springmvc.xml</param-value> 8 </init-param> 9 </servlet> 10 <servlet-mapping> 11 <servlet-name>springmvc</servlet-name> 12 <!-- 13 /* 在springMvc中不可使用, 由于/* 全部的路径都会进入到DispatcherServlet中可是不放行 14 / 全部的url路径都会进入DispatcherServlet, 放行.jsp的文件, 其余文件都不放行包括.js.css.png.jpg等 15 *.action url路径只有以.action结尾的才能够进入DispatcherServlet, 并放行 16 --> 17 <url-pattern>*.action</url-pattern> 18 </servlet-mapping>
第五步:建立ItemsController
ItemController是一个普通的java类,不须要实现任何接口,只须要在类上添加@Controller注解便可。@RequestMapping注解指定请求的url,其中“.action”能够加也能够不加。在itmesController中@RequestMapping("/list")指的是,咱们在访问ModelAndView这个方法的时候,在url路径上须要写的地址是:主机名:端口号名/工程名称/list.action。modelAndView.addObject("itemList", itemList);则是将一个itemList对象存入request域中,存的名称则是前面第一参数中的itemList。modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");设置的是返回到哪一个页面。

1 package cn.itcast.controller; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.servlet.ModelAndView; 9 10 import cn.itcast.pojo.Items; 11 12 @Controller 13 public class ItemsController { 14 15 //表示url到请求方法的映射, 其实就是经过浏览器中输入一个url, 这个url能够对应访问到一个controller中的方法 16 @RequestMapping("/list") 17 public ModelAndView list() throws Exception{ 18 List<Items> itemList = new ArrayList<>(); 19 20 //商品列表 21 Items items_1 = new Items(); 22 items_1.setName("联想笔记本_3"); 23 items_1.setPrice(6000f); 24 items_1.setDetail("ThinkPad T430 联想笔记本电脑!"); 25 26 Items items_2 = new Items(); 27 items_2.setName("苹果手机"); 28 items_2.setPrice(5000f); 29 items_2.setDetail("iphone6苹果手机!"); 30 31 itemList.add(items_1); 32 itemList.add(items_2); 33 34 //模型和视图 35 //模型中能够放入返回给页面的数据, 36 //视图中放了页面的路径 37 ModelAndView modelAndView = new ModelAndView(); 38 //将返回给页面的数据放入其中 39 modelAndView.addObject("itemList", itemList); 40 //指定页面的逻辑路径 41 //逻辑路径: 就是页面地址的字符串 42 //物理路径: 物理路径就是页面的对象 43 modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp"); 44 return modelAndView; 45 } 46 }