SpringMVC1
2021-02-19 00:19
                         标签:NPU   无效   查询条件   原理图   命名空间   ack   传参   row   inf    1.SpringMVC 中重要组件   3.Spring 容器和 SpringMVC 容器的关系     2)Spring 容器和 SpringMVC 容器是父子容器.   1. 导入 jar 2. 在 web.xml 中配置前端控制器 DispatcherServlet 3. 在 src 下新建 springmvc.xml 4. 编写控制器类 1.在 web.xml 中配置 Filter 1. 把内容写到方法(HandlerMethod)参数中,SpringMVC 只要有这个内容,注入内容.   2)如果请求参数名和方法参数名不对应使用@RequestParam()赋值   3)如果方法参数是基本数据类型(不是封装类)可以通过@RequestParam 设置默认值.   4)如果强制要求必须有某个参数使用required 3. HandlerMethod 中参数是对象类型 4. 请求参数中包含多个同名参数的获取方式 5. 请求参数中对象.属性格式   2)新建一个类   3)控制器   6. 在请求参数中传递集合对象类型参数   2)新建类   3)控制器 7. restful 传值方式.   3)在控制器中   1. 默认跳转方式请求转发. 1. SpringMVC 会提供默认视图解析器. 3. 如果希望不执行自定义视图解析器,在方法返回值前面添加forward:或 redirect: 1. 在方法上只有@RequestMapping 时,无论方法返回值是什么都认为需要跳转。 3. 底层使用Jackson进行json转换,在项目中一定要导入jackson的jar   SpringMVC1 标签:NPU   无效   查询条件   原理图   命名空间   ack   传参   row   inf    原文地址:https://www.cnblogs.com/sunny-sml/p/12687212.html一.SpringMVC 简介
  1)DispatcherServlet: 前端控制器,接收所有请求(如果配置/不包含 jsp)
  2)HandlerMapping: 解析请求格式的.判断希望要执行哪个具体的方法.
  3)HandlerAdapter: 负责调用具体的方法.
  4)ViewResovler:视图解析器.解析结果,准备跳转到具体的物理视图
2.SpringMVC 运行原理图
  1)源码
    SpringMVC 容器中能够调用 Spring 容器的所有内容.
    图示:
二.SpringMVC 环境搭建

  如果不配置 servlet> 
    servlet-name>jqkservlet-name>
    servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> 
    init-param> 
        param-name>contextConfigLocationparam-name> 
        param-value>classpath:springmvc.xmlparam-value> 
    init-param> 
    load-on-startup>1load-on-startup> 
servlet> 
servlet-mapping> 
    servlet-name>jqkservlet-name> 
    url-pattern>/url-pattern> 
servlet-mapping>
  引入 xmlns:mvc 命名空间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/context 
        http://www.springframework.org/schema/context/spring-context.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
         
        context:component-scan base-package="com.bjsxt.controller">context:component-scan> 
         
        -org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandler Mapping --> 
        -org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerA dapter --> 
        mvc:annotation-driven>mvc:annotation-driven> 
         
        mvc:resources location="/js/" mapping="/js/**">mvc:resources> 
        mvc:resources location="/css/" mapping="/css/**">mvc:resources> 
        mvc:resources location="/images/" mapping="/images/**">mvc:resources> 
beans>
@Controller 
public class DemoController {
    @RequestMapping("demo") 
    public String demo(){ 
        System.out.println("执行 demo"); 
        return "main.jsp"; 
    } 
    @RequestMapping("demo2") 
    public String demo2(){ 
        System.out.println("demo2"); 
        return "main1.jsp"; 
    } 
}
三.字符编码过滤器
 
filter>     
    filter-name>encodingfilter-name>
    filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class> 
    init-param> 
        param-name>encodingparam-name> 
        param-value>utf-8param-value> 
    init-param> 
filter> 
filter-mapping> 
    filter-name>encodingfilter-name> 
    url-pattern>/*url-pattern> 
filter-mapping>
四.传参
2. 基本数据类型参数
  1)默认保证参数名称和请求中传递的参数名相同@Controller 
public class DemoController { 
    @RequestMapping("demo") 
    public String demo(String name,int age){ 
        System.out.println("执行 demo"+" "+name+" "+age);
        return "main.jsp";
    }
}
@RequestMapping("demo") 
public String demo(@RequestParam(value="name1") String name,@RequestParam(value="age1")int age){ 
        System.out.println("执行 demo"+" "+name+" "+age); 
        return "main.jsp"; 
}
    防止没有参数时报错500@RequestMapping("page") 
public String page(@RequestParam(defaultValue="2") int pageSize,@RequestParam(defaultValue="1") int pageNumber){ 
    System.out.println(pageSize+" "+pageNumber); 
    return "main.jsp"; 
}
@RequestMapping("demo2") 
public String demo2(@RequestParam(required=true) String name){ 
    System.out.println("name 是 SQL 的查询条件,必须要传 递 name 参数"+name); 
    return "main.jsp"; 
}
  1)请求参数名和对象中属性名对应(get/set 方法)@RequestMapping("demo4") 
public String demo4(People peo){ 
    return "main.jsp"; 
}
  复选框传递的参数是多个同名参数(例如多选框的参数)@RequestMapping("demo5") 
public String demo5(String name,int age,@RequestParam("hover") List
  1)jsp 中代码input type="text" name="peo.name"/> 
input type="text" name="peo.age"/>
    对象名和参数中.前面的名称对应public class Demo {
    private People peo;
@RequestMapping("demo6") 
public String demo6(Demo demo){ 
    System.out.println(demo); 
    return "main.jsp"; 
}
  1)jsp 中格式input type="text" name="peo[0].name"/> 
input type="text" name="peo[0].age"/> 
input type="text" name="peo[1].name"/> 
input type="text" name="peo[1].age"/>
public class Demo {
    private List
@RequestMapping("demo6") 
public String demo6(Demo demo){ 
    System.out.println(demo); 
    return "main.jsp"; 
}
  1)简化 jsp 中参数编写格式
  2)在 jsp 中设定特定的格式
a href="demo8?id1=123&name=abc">跳转a>
a href="demo8/123/abc">跳转a>
    在@RequestMapping 中一定要和请求格式对应
    {名称} 中名称自定义名称
    @PathVariable 获取@RequestMapping 中内容,默认按照方法参数名称去寻找.@RequestMapping("demo8/{id1}/{name}") 
public String demo8(@PathVariable String name,@PathVariable("id1") int age){
    System.out.println(name +" "+age); 
    return "/main.jsp"; 
}
四.跳转方式
2. 设置返回值字符串内容
  1)添加 redirect:资源路径  重定向
  2)添加 forward:资源路径  或省略 forward: 转发@RequestMapping("demo9")
public String demo9(){
    return "forward:demo10";
}
五.视图解析器
2. 程序员自定义视图解析器bean id="viewResolver" class="org.springframework.web.servlet.view.InternalR esourceViewResolver"> 
    property name="prefix" value="/">property> 
    property name="suffix" value=".jsp">property> 
bean>
@RequestMapping("demo10")
public String demo10(){
    return "forward:demo11";
}
@RequestMapping("demo11")
public String demo11(){
    return "main";
}
六.@ResponseBody
2. 在方法上添加@ResponseBody(恒不跳转)
  1)如果返回值满足 key-value 形式(对象或 map)
    把响应头设置为 application/json;charset=utf-8
    把转换后的内容输出流的形式响应给客户端.
  2)如果返回值不满足 key-value,例如返回值为 String
    把相应头设置为 text/html
    把方法返回值以流的形式直接输出.
    如果返回值包含中文,出现中文乱码
      produces 表示响应头中 Content-Type 取值.@RequestMapping(value="demo12",produces="text/html; charset=utf-8")
@ResponseBody 
public String demo12() throws IOException{ 
    People p = new People(); 
    p.setAge(12);
    p.setName("张三"); 
    return "中文";
}
  spring4.1.6 对 jackson 不支持较高版本,jackson2.7 无效.
上一篇:插入排序