Spring配置文件如下
- context:include-filter 要一起使用,表示:只扫描指定注解的类。
- context:exclude-filter直接使用,表示:不扫描指定注解的类,其他注解类全部扫描。
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启Spring容器的注解扫描-->
<context:component-scan base-package="com.xxx.demo">
<!--Spring只扫描service和dao,controller交给SpringMVC扫描 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/company?serverTimezone=UTC&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--注入JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
<!--配置一个ModelAndView,单例即可-->
<bean id="modelAndView" class="org.springframework.web.servlet.ModelAndView"/>
</beans>
SpringMVC配置文件如下
- 开启注解扫描
- 配置视图解析器
- 配置不拦截静态资源
- 开启SpringMVC注解支持
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--1.开启注解扫描-->
<context:component-scan base-package="com.xxx.demo">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--2.配置视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--3.配置不拦截静态资源-->
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/images/**" location="/images/"/>
<mvc:resources mapping="/js/" location="/js/**"/>
<!--4.开启SpringMVC注解支持-->
<mvc:annotation-driven/>
</beans>
web.xml 配置文件如下
有几个注意事项
- 要加载Spring的配置文件
- 要加载SpringMVC的配置文件
- 要配置前端控制器
- 要配置中文乱码过滤器(选择性配置)
<?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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<!--配置Spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--中文乱码过滤器-->
<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>
<!--配置前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMvc.xml</param-value>
</init-param>
<!--服务器启动加载该servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
SpringMVC大概过程图解
我的理解:
- SpringMVC的运行核心是DisPatcherServlet
前端控制器,用户发起请求到DisPatcherServlet,由它调用各个组件配合工作的成,DisPatcherServlet的存在大大降低了组件之间的耦合性 - HandlerMapping 处理器映射器: 记录url与处理器的映射,方式有注解、XML配置
- HandLer 后端控制器: 处理 Controller层所写的业务代码,也就是接收请求并处理
- HandlerAdapter 处理器适配器: 执行处理器
- ViewResolver 视图解析器:负责解析view视图,同时填充数据到网页中,展示给用户
SpringMVC常用注解
- @Controller : 表示该类是一个 Controller 类,负责接收用户的请求,并处理
- @RequestMapping 表示请求映射,在类上加表示一级目录,在方法上加表示第二级目录,一般的 action 中的 url 填的是 一级目录+/+二级目录
Controller类中的常用方法接收参数,携带参数转发到view
- 可用ModelView对象进行转发, 先将参数保存到 ModelView中addObject("",""),再设置ModelView.setViewName(“url”)中的url为需要转发的路径.
- 可接收Model参数,利用 model.setAttribute(),类似request.setAttribute设置,直接return “url”
,url为需要转发的路径
package com.xxx.demo.controller;
import com.xxx.demo.service.IProductService;
import com.xxx.demo.vo.Product;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
@Controller
@RequestMapping(path = "/productAdminController")
public class ProductAdminController {
@Resource(name = "productService")
private IProductService service;
@Resource(name="modelAndView")
private ModelAndView view;
@RequestMapping(path = "/addProduct")
public String addProduct(){
return "admin/addProduct";
}
@RequestMapping(path = "/addDo")
public ModelAndView addDo(Product p){
view.addObject("flag",service.addProduct(p));
view.setViewName("admin/add_do");
return view;
}
@RequestMapping(path = "/delete_do")
public ModelAndView delete_do(String productID){
view.addObject("flag",service.deleteProductById(Integer.parseInt(productID)));
view.setViewName("admin/delete_do");
return view;
}
@RequestMapping(path = "/updateProduct")
public ModelAndView updateProduct(String productID){
view.addObject("p_1",service.findProductByID(Integer.parseInt(productID)));
view.setViewName("admin/updateProduct");
return view;
}
@RequestMapping(path = "/updateDo")
public ModelAndView updateDo(Product p){
view.addObject("flag",service.updateProduct(p));
view.setViewName("admin/update_do");
return view;
}
@RequestMapping(path = "/detailProduct")
public String detailProduct(Model model){
//Model底层将存入 request域中
model.addAttribute("list",service.getAllProducts());
return "admin/detailProduct";
}
}
总结
先搭建好Spring,SpringMVC的环境,在Controller类中注入从Spring容器中获取到的service或者是其他bean, 接收前台请求,获取请求中的参数,调用service,配合其他bean 完成业务处理和请求转发