到这里基本都没什么说的了,因为mvc知识在整合的时候基本没有变动。
第一步:先到web下的web.xml去配置我们的前置控制器DispatcherServlet—> 配置乱码过滤filter —>配置session的相关配置如session-timeout
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
第二步:编写spring-mvc.xml文件—>注解驱动—>静态资源过滤
—>扫描包: controlLer—>配置视图解析器
记得将spring-mvc.xml用import导入我们的applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--1.注解驱动 注意这个驱动给的配置得是mvc的-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--2.静态资源过滤-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--3.扫描包: controlLer-->
<context:component-scan base-package="com.zsp.controller"></context:component-scan>
<!--4.视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
第三步:去我们配置视图解析器对应的文件下创建文件—>然后去control对应的文件夹里编写文件跳转到视图解析器对应的文件下就可以了
package com.zsp.controller;
import com.zsp.pojo.Books;
import com.zsp.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class BookController {
@Autowired
@Qualifier("bookServiceImpl")
BookService bookService;
@RequestMapping("/queryBooks")
public String queryBooks(Model model){
List<Books> books = bookService.queryBooks();
model.addAttribute("list",books);
return "a";
}
@RequestMapping("/t1")
public String queryBooks1(){
return "a";
}
}
a.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/9/14
Time: 16:55
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${list}
</body>
</html>