-
搭建和测试 SpringMVC 的开发环境:
- web.xml 元素顺序:
- 在 web.xml 中配置 DisPatcherServlet 前端控制器:
<!-- 配置前端控制器 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 加载 springmvc.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>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
- 在 web.xml 中配置 DispatcherServlet 过滤器解决中文乱码:
<!-- 解决 post 请求中文乱码的过滤器 --> <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>
- 创建 springmvc.xml 配置文件,编写配置文件:
<?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"> <!-- 扫描 controller 的注解 --> <context:component-scan base-package="com.qcby.controller"/> <!-- 配置视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- JSP 文件所在的目录 --> <property name="prefix" value="/WEB-INF/pages/"/> <!-- 设置文件的后缀名 .jsp --> <property name="suffix" value=".jsp"/> </bean> <!-- 设置静态资源不过滤 --> <!-- <mvc:resources mapping="/css/**" location="/css/"/>--> <!-- <mvc:resources mapping="/js/**" location="/js/"/>--> <!-- <mvc:resources mapping="/images/**" location="/images/"/>--> <!-- 开启对 SpringMVC 注解的支持 --> <mvc:annotation-driven/> </beans>
- 测试 SpringMVC 的框架搭建是否成功:
- 编写 index.jsp 和 suc.jsp 页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <body> <h2>Hello World!</h2> <a href="/account/findAll">查询所有</a> </body> </html> //----------- suc 页面 ------------ <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>跳转成功</h1> </body> </html>
- 创建 AccountController 类,编写方法进行测试:
@Controller @RequestMapping("/account") public class AccountController { /** * 查询所有方法 */ @RequestMapping("/findAll") public ModelAndView findAll(){ System.out.println("表现层:查询所有账户"); ModelAndView mv = new ModelAndView(); mv.setViewName("suc"); return mv; } }
- 编写 index.jsp 和 suc.jsp 页面:
- web.xml 元素顺序:
-
Spring 整合 SpringMVC 框架:
- 目的:在 controller 中能成功地调用 service 对象中的方法
- 在项目启动的时候,就去加载 applicationContext.xml 的配置文件,在 web.xml 中配置 ContextLoaderListener 监听器(监听器只能加载 WEB-INF 目录下的 applicationContext.xml 的配置文件)
<!-- 配置 Spring 的监听器 --> <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>
- 在 controller 中注入 service 对象,调用 service 对象的方法进行测试:
@Controller @RequestMapping("/account") public class AccountController { @Autowired private AccountService accountService; /** * 查询所有方法 */ @RequestMapping("/findAll") public ModelAndView findAll(){ System.out.println("表现层:查询所有账户"); List<Account> list = accountService.findAll(); ModelAndView mv = new ModelAndView(); mv.setViewName("suc"); return mv; } }