1. 搭建和测试SpringMVC的开发环境
- 在web.xml中配置DispatcherServlet前端控制器(要先更新xml版本)
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> </web-app>
<!--在web.xml中配置Spring提供的过滤器类 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--不拦截所有是html的页面请求--> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <!--配置前端控制器,对浏览器发送的请求进行统一处理--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--加载springmvc.xml配置文件的位置和名称,配置的是Spring配置--> <init-param> <!--contextConfigLocation:上下文配置路径,固定值--> <param-name>contextConfigLocation</param-name> <!--classpath:类路径,值得是Java和resources文件夹--> <!--springmvc.xml:指的是配置文件的名称:需要配置springmvc.xml,在下面--> <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> <!--开启项目时打开的页面--> <welcome-file-list> <welcome-file>/index.html</welcome-file> </welcome-file-list>
- 创建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"> <!--配置spring创建容器时要扫描的包--> <context:component-scan base-package="com.qcby"/> <!--处理映射器--> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!--处理器适配器--> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!--配置视图解析器--> <bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver"> <property name="order" value="1"/> <property name="characterEncoding" value="UTF-8"/> <property name="templateEngine" ref="templateEngine"/> </bean> <!-- templateEngine --> <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine"> <property name="templateResolver" ref="templateResolver"/> </bean> <bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver"> <property name="prefix" value="/html/" /> <property name="suffix" value=".html" /> <property name="templateMode" value="HTML5"/> <property name="characterEncoding" value="UTF-8"/> </bean> <!--处理静态资源的映射问题--> <mvc:default-servlet-handler/> <!-- 配置spring开启注解mvc的支持 默认就是开启的 ,要想让其他组件(不包含映射器、适配器、处理器)生效就必须需要配置了--> <mvc:annotation-driven/> </beans>
3.测试SpringMVC的框架搭建是否成功
- 编写index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>主页</title> </head> <body> <html> <head> <meta charset="utf-8"> <title>入门程序</title> </head> <body> <h3>入门</h3><a href="/ssm/hello" >入门程序</a> </body> </html> </body> </html>
编写suc.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>成功</title> </head> <body> <h1>Hello <b th:text="${msg}"></b></h1> </body> <script> </script> </html>
- 创建AccountController类,编写方法,进行测试
package com.qcby.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class AccountController { /** * 处理超链接发送出来的请求 * @param model * @return */ @RequestMapping(path = "/hello") public String sayHello(Model model){ System.out.println("入门方法执行了2..."); //报错暂时先注掉 //List<Account> accounts = accountService.findAll(); // 向模型中添加属性msg与值,可以在html页面中取出并渲染 model.addAttribute("msg","hello,SpringMVC"); // 配置了视图解析器后,写法 return "suc"; } }
启动Tomcat进行测试
2. Spring整合SpringMVC的框架
-
目的:在controller中能成功的调用service对象中的方法。
-
在项目启动的时候,就去加载Spring.xml的配置文件,在web.xml中配置ContextLoaderListener监听器(该监听器默认加载WEB-INF目录下的applicationContext.xml的配置文
件,所以要配置全局的变量加载类路径下的配置文件)。
<!--配置Spring的监听器--> <display-name>Archetype Created Web Application</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--配置加载类路径的配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:Spring.xml</param-value> </context-param>
- 在controller中注入service对象,调用service对象的方法进行测试
package com.qcby.controller; import com.qcby.entity.Account; import com.qcby.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import java.util.List; @Controller public class AccountController { //依赖注入 @Autowired private AccountService accountService; /** * 处理超链接发送出来的请求 * @param model * @return */ @RequestMapping(path = "/hello") public String sayHello(Model model){ System.out.println("入门方法执行了2..."); List<Account> accounts = accountService.findAll(); // 向模型中添加属性msg与值,可以在html页面中取出并渲染 model.addAttribute("msg","hello,SpringMVC"); // 配置了视图解析器后,写法 return "suc"; } }
启动程序,控制台输出"业务层查询所有",Controller层能够调用到service层了
Spring和SpringMVC联系到了一起