springMVC
1:简介
1-1:MVC简介
-
一种架构
-
M:(Model )模型,数据和业务逻辑,dao,service,domain
-
V :(View )视图,用户看到的页面。jsp,thymelef,
-
C:(Servelt )控制器,接收用户页面请求,,讲Model数据传递到View,路径跳转。Controller
1-2:springMVC简介
1-2-1:是什么
- 实现MVC架构的轻量级的web框架
1-2-2:优点
- 轻量级,易学,流行,高效
- 与spring兼容性好
- 功能强大,RESTful
1-2-3:运行流程
- Spring的web框架围绕DispatcherServlet(调度Servlet) 设计。
- DispatcherServlet的作用是将请求分发到不同的处理器
1-2-3-1:运行流程图
1-2-3-2:运行流程描述
localhost:8080/hello
服务器域名
hello表示控制器
-
1:用户请求到DispatcherServlet(中央控制器,前端控制器)
-
2:DispatcherServlet收到用户请求后,调用HandlerMapping(处理器映射器),映射器根据url找到Handler(处理器)
-
3:将HandlerExecution代表具体Handler(处理器),其主要作用是根据url查找控制器,如上url被查找控制器为:hello。并将解析后的信息传递给DispatcherServlet
-
4:DispatcherServlet通过HandlerAdapter(处理器适配器)调用Handler(处理器)
-
5:Handler调用指定控制器(Controller,hello)
-
6:执行Controller将将具体的执行信息,如逻辑视图,包装到(ModelAndView)
-
7:将ModelAndView返回给HandlerAdapter
-
8:将HandlerAdapter中的视图逻辑名或模型传返回给DispatcherServlet
-
9:DispatcherServlet调用ViewReslover(视图解析器)来解析HandlerAdapter传递的逻辑视图名。
-
10:DispatcherServlet根据视图解析器解析的视图结果,调用具体的视图(物理视图)。
-
11:最终视图呈现给用户。
2:示例
1:实现接口
1:目录
2:代码
1:web.xml
<?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">
<!--1.注册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>/WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
<!--启动级别-1-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--/ 匹配所有的请求;(不包括.jsp)-->
<!--/* 匹配所有的请求;(包括.jsp)-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2:springmvc-servlet.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: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">
<!--处理映射器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!--处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!--视图解析器:DispatcherServlet给他的ModelAndView-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
<bean id="/hello" class="com.lx.controller.HelloController"/>
</beans>
3:HelloController.java
package com.lx.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView();
mv.addObject("msg", "HelloSpringMVC!");
mv.setViewName("hello"); //: /WEB-INF/jsp/hello.jsp
return mv;
}
}
4:hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>aaa</title>
</head>
<body>
${msg}
</body>
</html>
2:注解
1:目录
2:代码
1:web.xml
<?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">
<!--1.注册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>/WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
<!--启动级别-1-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--/ 匹配所有的请求;(不包括.jsp)-->
<!--/* 匹配所有的请求;(包括.jsp)-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2:springmvc-servlet.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: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">
<!--扫描包,发现注解-->
<context:component-scan base-package="com.lx.controller"/>
<!-- 让Spring MVC不处理静态资源 -->
<mvc:default-servlet-handler/>
<!--映射器和适配器-->
<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>
3:HelloController
package com.lx.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping("/hello")
//url=hello
public ModelAndView handleRequest() throws Exception {
ModelAndView modelAndView = new ModelAndView("hello");
modelAndView.addObject("msg", "aasdsadasd!");
return modelAndView;
}
}
4:hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>aaa</title>
</head>
<body>
${msg}
</body>
</html>
3:项目问题
- 自行体会吧
3:名词
1:@RequestMapping
-
@RequestMapping("/h1")
-
作用在类上,表示父路径
-
作用在方法上,表示映射url到控制器类
2:@Controller
- @Controller注解类型用于声明Spring类的实例是一个控制器
3:@PathVariable
-
让方法参数的值对应绑定到一个URI模板变量上。
-
public String index(@PathVariable int p1, @PathVariable int p2, Model model)
4:地址栏
- 所有的地址栏请求默认都会是 HTTP GET 类型的
5:RestFul
-
一种风格,Restful就是一个资源定位及资源操作的风格
-
资源:互联网所有的事物都可以被抽象为资源
-
使用POST、DELETE、PUT、GET,使用不同方法对资源进行操作
-
@RequestMapping(value = “/hello”,method = {RequestMethod.POST})
4:跳转
- 转发,路径不变,从准发到路径拿到数据回到当前路径
- 充定向,路径变化
1:ModelAndView
@Controller
public class HelloController {
@RequestMapping("/hello")
//url=hello
public ModelAndView handleRequest() throws Exception {
ModelAndView modelAndView = new ModelAndView("jump");//页面
modelAndView.addObject("msg", "ModelAndViewJump!");
return modelAndView;
}
}
2:ServletAPI
- 1、通过HttpServletResponse进行输出
- 2、通过HttpServletResponse实现重定向
- 3、通过HttpServletResponse实现转发
@Controller
public class ResultGo {
@RequestMapping("/s1")
public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
rsp.getWriter().println("Hello,Spring BY servlet API");
}
@RequestMapping("/s2")
public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
rsp.sendRedirect("/index.jsp");
}
@RequestMapping("/s3")
public void test3(HttpServletRequest req, HttpServletResponse rsp) throws Exception {
//转发
req.setAttribute("msg","s3");
req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,rsp);
}
}
3:SpringMVC
1:无需视图解析器
@Controller
public class ResultSpringMVC {
@RequestMapping("/hello1")
public String test1(){
//转发
return "/index.jsp";
}
@RequestMapping("/hello2")
public String test2(){
//转发二
return "forward:/index.jsp";
}
@RequestMapping("/hello3")
public String test3(){
//重定向
return "redirect:/index.jsp";
}
}
2:有视图解析器;
package com.lx.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String test1(){
//return "zxc"; //WEB-INF/JSP/zxc.jsp
//return "redirect:/index.jsp";//index.jsp
return "redirect:/qwer"; //url=qwer
}
@RequestMapping("/qwer")
public ModelAndView handleRequest() throws Exception {
ModelAndView modelAndView = new ModelAndView("jump");//页面
modelAndView.addObject("msg", "ModelAndViewJump!");
return modelAndView;
}
}
5:获取数据
1:前端到后端
1:@RequestParam
2:对象
2:后端到前段
-
ModelAndView
-
ModelMap
-
Model
详情见 https://blog.csdn.net/weixin_41688434/article/details/107316785
6:乱码
在web.xml中添加配置
<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>
details/107316785
6:乱码
在web.xml中添加配置
<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>