zoukankan      html  css  js  c++  java
  • SpringMVC基础-01

    1、流程
      1)、导包
    commons-logging-1.1.3.jar
    spring-aop-4.0.0.RELEASE.jar
    spring-beans-4.0.0.RELEASE.jar
    spring-context-4.0.0.RELEASE.jar
    spring-core-4.0.0.RELEASE.jar
    spring-expression-4.0.0.RELEASE.jar
    spring-web-4.0.0.RELEASE.jar
    spring-webmvc-4.0.0.RELEASE.jar
      2)、写配置;
           1)web.xml可能要写什么
                配置springmvc的前端控制器,指定springmvc配置文件位置
         web.xml:
     1 <!DOCTYPE web-app PUBLIC
     2         "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     3         "http://java.sun.com/dtd/web-app_2_3.dtd" >
     4 
     5 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
     6          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     7          id="WebApp_ID" version="3.0">
     8     <display-name>2.SpringMVC_helloworld</display-name>
     9     <welcome-file-list>
    10         <welcome-file>index.jsp</welcome-file>
    11     </welcome-file-list>
    12 
    13     <!-- SpringMVC思想是有一个前端控制器能拦截所有请求,并智能派发;
    14         这个前端控制器是一个servlet;应该在web.xml中配置这个servlet来拦截所有请求
    15      -->
    16 
    17     <!-- The front controller of this Spring Web application,
    18     responsible for handling all application requests -->
    19     <servlet>
    20         <servlet-name>springDispatcherServlet</servlet-name>
    21         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    22 
    23         <init-param>
    24             <!-- contextConfigLocation:指定SpringMVC配置文件位置 -->
    25             <param-name>contextConfigLocation</param-name>
    26             <param-value>classpath:springmvc.xml</param-value>
    27         </init-param>
    28         <!-- servlet启动加载,servlet原本是第一次访问创建对象;
    29         load-on-startup:服务器启动的时候创建对象;值越小优先级越高,越先创建对象;
    30          -->
    31         <load-on-startup>1</load-on-startup>
    32     </servlet>
    33 
    34     <!-- Map all requests to the DispatcherServlet for handling -->
    35     <!--  *.do   *.action  *.haha -->
    36     <!--
    37         /:拦截所有请求,不拦截jsp页面,*.jsp请求
    38         /*:拦截所有请求,拦截jsp页面,*.jsp请求
    39 
    40 
    41         处理*.jsp是tomcat做的事;所有项目的小web.xml都是继承于大web.xml
    42         DefaultServlet是Tomcat中处理静态资源的?
    43             除过jsp,和servlet外剩下的都是静态资源;
    44             index.html:静态资源,tomcat就会在服务器下找到这个资源并返回;
    45             我们前端控制器的/禁用了tomcat服务器中的DefaultServlet
    46 
    47 
    48         1)服务器的大web.xml中有一个DefaultServlet是url-pattern=/
    49         2)我们的配置中前端控制器 url-pattern=/
    50                 静态资源会来到DispatcherServlet(前端控制器)看那个方法的RequestMapping是这个index.html
    51         3)为什么jsp又能访问;因为我们没有覆盖服务器中的JspServlet的配置
    52         4) /*  直接就是拦截所有请求;我们写/;也是为了迎合后来Rest风格的URL地址
    53     -->
    54     <servlet-mapping>
    55         <servlet-name>springDispatcherServlet</servlet-name>
    56         <!--
    57  /*和/都是拦截所有请求; /:会拦截所有请求,但是不会拦截*.jsp;能保证jsp访问正常;
    58  /*的范围更大;还会拦截到*.jsp这些请求;一但拦截jsp页面就不能显示了;
    59           -->
    60         <url-pattern>/</url-pattern>
    61     </servlet-mapping>
    62 </web-app>

        2)框架自身可能要写什么

          springmvc.xml:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     6         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
     7 
     8     <!-- 扫描所有组件 -->
     9     <context:component-scan base-package="com.atguigu"></context:component-scan>
    10 
    11     <!-- 配置一个视图解析器 ;能帮我们拼接页面地址-->
    12     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    13         <property name="prefix" value="/WEB-INF/pages/"></property>
    14         <property name="suffix" value=".jsp"></property>
    15     </bean>
    16 </beans>

        3)、测试

    HelloController.java:

     1 package com.atguigu.controller;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 
     6 /**
     7  * 1、告诉SpringMVC这个类是一个处理器
     8  * @author lfy
     9  *
    10  * HelloWorld:细节:
    11  * 1、运行流程:
    12  *         1)、客户端点击链接会发送 http://localhost:8080/springmvc/hello 请求
    13  *         2)、来到tomcat服务器;
    14  *         3)、SpringMVC的前端控制器收到所有请求;
    15  *         4)、来看请求地址和@RequestMapping标注的哪个匹配,来找到到底使用那个类的哪个方法来处理
    16  *         5)、前端控制器找到了目标处理器类和目标方法,直接利用反射执行目标方法;
    17  *         6)、方法执行完成以后会有一个返回值;SpringMVC认为这个返回值就是要去的页面地址
    18  *         7)、拿到方法返回值以后;用视图解析器进行拼串得到完整的页面地址;
    19  *         8)、拿到页面地址,前端控制器帮我们转发到页面;
    20  *
    21  *2、@RequestMapping22  *        就是告诉SpringMVC;这个方法用来处理什么请求;
    23  *        这个/是可以省略,即使省略了,也是默认从当前项目下开始;
    24  *        习惯加上比较好    /hello  /hello
    25  *        RequestMapping的使用:?
    26  *3、如果不指定配置文件位置?
    27  *        /WEB-INF/springDispatcherServlet-servlet.xml
    28  *        如果不指定也会默认去找一个文件;
    29  *            /WEB-INF/springDispatcherServlet-servlet.xml
    30  *    就在web应用的/WEB-INF、下创建一个名叫   前端控制器名-servlet.xml
    31  */
    32 @Controller
    33 public class HelloController {
    34 
    35     @RequestMapping("/hello01")
    36     public String nihao(){
    37         return "success";
    38     }
    39 
    40     @RequestMapping("/handle01")
    41     public String hanle01(){
    42         System.out.println("handle01....");
    43         return "error";
    44     }
    45 
    46     @RequestMapping("/hello03")
    47     public String hanle02(){
    48         return "success";
    49     }
    50 }

    RequestMappingTest.java:

     1 package com.atguigu.controller;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.PathVariable;
     5 import org.springframework.web.bind.annotation.RequestMapping;
     6 
     7 /**
     8  *     @RequestMapping模糊匹配功能
     9  * 
    10  * @author lfy
    11  * 
    12  * URL地址可以写模糊的通配符:
    13  *     ?:能替代任意一个字符
    14  *     *:能替代任意多个字符,和一层路径
    15  *     **:能替代多层路径
    16  *
    17  */
    18 @Controller
    19 public class RequestMappingTest {
    20     
    21     @RequestMapping("/antTest01")
    22     public String antTest01(){
    23         System.out.println("antTest01...");
    24         return "success";
    25     }
    26     
    27     /**
    28      * ?匹配一个字符,0个多个都不行;
    29      *         模糊和精确多个匹配情况下,精确优先
    30      * 
    31      * @return
    32      */
    33     @RequestMapping("/antTest0?")
    34     public String antTest02(){
    35         System.out.println("antTest02...");
    36         return "success";
    37     }
    38     
    39     /**
    40      *   *匹配任意多个字符
    41      * @return
    42      */
    43     @RequestMapping("/antTest0*")
    44     public String antTest03(){
    45         System.out.println("antTest03...");
    46         return "success";
    47     }
    48     
    49     /**
    50      *  *:匹配一层路径
    51      * @return
    52      */
    53     @RequestMapping("/a/*/antTest01")
    54     public String antTest04(){
    55         System.out.println("antTest04...");
    56         return "success";
    57     }
    58     
    59     @RequestMapping("/a/**/antTest01")
    60     public String antTest05(){
    61         System.out.println("antTest05...");
    62         return "success";
    63     }
    64     
    65     //路径上可以有占位符:  占位符 语法就是可以在任意路径的地方写一个{变量名}
    66     //   /user/admin    /user/leifengyang
    67     // 路径上的占位符只能占一层路径
    68     @RequestMapping("/user/{id}")
    69     public String pathVariableTest(@PathVariable("id")String id){
    70         System.out.println("路径上的占位符的值"+id);
    71         return "success";
    72     }
    73 
    74 }

    RequestMappingTestController.java:

     1 package com.atguigu.controller;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 import org.springframework.web.bind.annotation.RequestMethod;
     6 
     7 @Controller
     8 @RequestMapping("/haha")
     9 public class RequestMappingTestController {
    10 
    11     /**
    12      * @RequestMapping其它属性13      * method14      *      HTTP协议中的所有请求方式:
    15      *      GET,POST,HEAD,PUT,PATCH,DELETE,OPTIONS,TRACE;
    16      *      method = RequestMethod.POST:只接收这种类型的请求,默认是什么都可以
    17      *          不是规定的方式报错:4xx:都是客户端错误
    18      *
    19      * params:规定请求参数
    20      *      param1:表示请求必须包含名为param1的请求参数
    21      *          eg:params = {"username"}
    22      *              发送请求的时候必须带上一个名为username的参数  没带都会400
    23      *      !param1:表示请求不能包含名为param1的请求参数
    24      *          eg:params={"!username"}
    25      *              发送请求的时候必须不携带上一个名为username的参数;带了都会400
    26      *
    27      *      param1 != value1: 表示请求包含名为 param1 的请求参数,但其值不能为 value1
    28      *          eg:params={"username!=123"}
    29      *              发送请求的时候;携带的username值必须不是123(不带username或者username不是123)
    30      *      {“param1=value1”, “param2”}: 请求必须包含名为 param1 和 param2 的两个请求参数,且 param1 参数的值必须为 value1
    31      *          eg:params={"username!=123","pwd","!age"}
    32      *              请求参数必须满足以上规则;
    33      *              请求的username不能是123,必须有pwd的值,不能有age
    34      * headers:规定请求头;也和params一样能写简单的表达式,可以指定哪个浏览器访问
    35      * consumes:只接收内容类型是哪种的请求,规定请求头中的Content-Type
    36      * produces:告诉浏览器返回的内容类型是什么,给响应头中加上Content-Type:text/html;charset=utf-8
    37      */
    38 
    39    
    40     @RequestMapping(value="/handle02",method=RequestMethod.POST)
    41     public String handle02(){
    42         System.out.println("handle02...");
    43         return "success";
    44     }
    45 
    46     /**
    47      *
    48      * @return
    49      */
    50     @RequestMapping(value="/handle03",params={"username!=123","pwd","!age"})
    51     public String handle03(){
    52         System.out.println("handle03....");
    53         return "success";
    54     }
    55 
    56     /**
    57      * User-Agent:浏览器信息;
    58      * 让火狐能访问,让谷歌不能访问
    59      *
    60      * 谷歌:
    61      * User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
    62      * 火狐;
    63      * User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0
    64      * @return
    65      *
    66      */
    67     @RequestMapping(value="/handle04",headers={"User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0"})
    68     public String handle04(){
    69         return "success";
    70     }
    71 }

    index.jsp:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2          pageEncoding="UTF-8" %>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7     <title>Insert title here</title>
     8 </head>
     9 <body>
    10 <!--以前一个servlet;这个servlet配置一个url-pattern(/hello)  -->
    11 <a href="hello">你好</a><br/>
    12 <h1>RequestMapping测试</h1>
    13 <a href="handle01">test01-写在方法上的requestMapping</a><br/>
    14 <a href="haha/handle01">test01-写在方法上的requestMapping</a>
    15 <h1>测试RequestMapping的属性</h1>
    16 <a href="haha/handle02">handle02</a><br/>
    17 <form action="haha/handle02" method="post">
    18     <input type="submit"/>
    19 </form>
    20 <a href="haha/handle03">handle03-测试params</a><br/>
    21 <a href="haha/handle04">handle04-测试headers</a>
    22 <hr/>
    23 <h1>RequestMapping-Ant风格的URL</h1>
    24 <a href="antTest01">精确请求地址-antTest01</a><br/>
    25 <a href="user/admin">测试PathVariable</a>
    26 </body>
    27 </html>

    error.jsp:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 <h1>嘿嘿</h1>
    11 </body>
    12 </html>

    success.jsp:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 <h1>成功!</h1>
    11 </body>
    12 </html>
  • 相关阅读:
    【Log】【Log4j】【1】log4j日志的输出级别
    【Word&Excel】【1】更新Word的目录
    【服务器】【Windows】【5】让bat执行完后不关闭
    【Mybatis】【5】Oralce in 语句中当in(1,2,3...) 条件数量大于1000将会报错
    【JS插件】【1】移动端(微信等)使用 vConsole调试console
    【Oracle】【10】去除数据中的html标签
    【其他】【前端安全】【1】XSS攻击
    hdu 4433
    hdu 4435
    hdu 4752
  • 原文地址:https://www.cnblogs.com/116970u/p/13154096.html
Copyright © 2011-2022 走看看