SSM框架---SpringMVC
2021-04-03 21:36:22 25 举报
AI智能生成
请大家不要直接克隆,着手梳理一遍才会变成自己的知识
作者其他创作
大纲/内容
回顾servlet
1、创建父项目
2、导入maven依赖
3、创建子模块
4、添加框架支持
5、子模块导入maven依赖
6、web.xml配置servlet
7、编写servlet测试类
1、继承HttpServlet类
2、重写doGet和doPost方法
1、获取前端参数
String method = req.getParameter("method");
2、调用业务层
3、视图转发
req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,resp);
8、在web.xml中注册servlet
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.learning.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
9、配置tomcat
配置一个SpringMVC
基础版
1、创建子模块,添加web支持
2、导入springmvc依赖
3、配置web.xml,注册DispatcherServlet
4、编写springmvc-servlet.xml
1、添加 处理器映射器
BeanNameUrlHandlerMapping
2、添加 处理器适配器
SimpleControllerHandlerAdapter
3、添加 视图解析器
InternalResourceViewResolver
5、编写业务controller
6、把类交给SpringIOC容器,注册bean
7、编写需要跳转的页面
8、如果报错404,需要建立lib目录,添加jar包
9、如果报错500,去把lib中的老版本的jsp,servletjar包删除
注解版
1、创建子模块、添加web支持
2、在web.xml中注册springmvc
<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:springmvc-servlet.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>
3、新建springmvc-servlet.xml文件
如果文件出错,打开setting、editor、file type、text、找到不对劲的名字,删除
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--spring自动扫描-->
<context:component-scan base-package="com.learning.controller"/>
<!--让springmvc不处理静态资源-->
<mvc:default-servlet-handler/>
<!--springmvc注解驱动
实现这个后不需要再手动注册handlerMapping和handlerAdapter
annotation-driven会帮我们自动注入-->
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
4、创建controller
/**
* @author 22944
*/
@Controller
@RequestMapping("/hello")
public class HelloAnnotationController {
@RequestMapping("/h1")
public String h1(Model model){
model.addAttribute("msg","你好啊");
return "hello";
}
}
一个请求的执行过程
1、前端发送请求
2、servlet通过路径映射进入springmvc
<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:springmvc-servlet.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>
3、springmvc找到handleMapping
4、handleMapping找到对应的control返回给前端控制器,前端控制器再通过处理器适配器找到对应的controller
5、处理业务逻辑
6、返回一个ModelView给前端控制器
7、前端控制器转发给视图解析器,添加前缀后缀,找到对应的jsp页面,返回给前端控制器
8、前端控制器把返回的jsp返回给用户
SpringMVC的乱码过滤器
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
json交互
1、新建子模块、添加web支持
2、maven导入jackson-databind依赖
3、注册DispatcherServlet
<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:springmvc-servlet.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>
4、编写springmvc-servlet
<!-- 自动扫描指定的包,下面所有注解类交给IOC容器管理 -->
<context:component-scan base-package="com.learning.controller"/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
5、注册过滤器
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
6、编写Controller
7、乱码统一解决
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
SpringMVC拦截器
拦截器是aop思想的体现
1、编写web.xml
1、注册DispatcherServlet
<!--配置DispatcherServlet-->
<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>
2、配置过滤器
<!--配置过滤器-->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2、编写applicationContext.xml
1、添加注解支持
<!--注解驱动-->
<mvc:annotation-driven/>
2、自动扫描controller包
<!--配置自动扫描controller-->
<context:component-scan base-package="com.learning.controller"/>
3、配置静态资源过滤
<!--配置静态资源过滤-->
<mvc:default-servlet-handler/>
4、配置视图解析器
<!--视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
5、配置拦截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.learning.config.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
3、编写controller类
1、新建config包
2、编写拦截器类
实现HandlerInterceptor接口,实现方法
文件示例
package com.learning.config;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 小也
* @create 2021/4/2 22:11
*/
public class MyInterceptor implements HandlerInterceptor {
/**
* return true 放行,执行下一个拦截器
* return false 拦截,不执行下一个拦截器
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("=========方法执行前=========");
return false;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("=========方法执行后=========");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("=========清理=========");
}
}
0 条评论
下一页