其他分享
首页 > 其他分享> > SpringMvc(一)-初识

SpringMvc(一)-初识

作者:互联网

1、环境搭建

1.1 jar包

<spring.version>4.3.18.RELEASE</spring.version>

<!-- spring-mvc begin -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${spring.version}</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
</dependency>
<!-- spring-mvc end -->

1.2 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         id="WebApp_ID" version="3.1">

    <!--前端核心控制器  springMvc框架提供的 -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置初始化参数,指定springMvc的核心配置文件,目的就是自动创建容器对象 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!-- web容器启动,优先创建前端核心控制器实例对象 -->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <!-- 拦截所有的请求处理,放行jsp -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- Rest风格 请求转换过滤器-->
    <filter>
        <filter-name>hiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>hiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

1.3spring-mvc.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">

    <!-- 组件扫描 -->
    <context:component-scan base-package="com.kgc.mvc"></context:component-scan>

    <!-- jsp 视图解析器 -->
    <!-- 作用:当请求处理方法返回一个目标视图页面的名称,视图解析器会自动将返回的视图名称解析为真实的物理视图(prefix + 目标视图页面名称 + suffix) -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 统一前缀,请求处理方法返回目标视图名称时,会被统一增加此前缀目录(目录视图目录),结尾的 / 不能少 -->
        <property name="prefix" value="/WEB-INF/views/"></property>
        <!-- 统一后缀,请求处理方法返回目标视图名称时,会被统一增加此后缀 -->
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

2、Mvc 使用流程

需求,浏览器发送请求helloMvc,请求后端处理方法,返回目标success。jsp页面,并在页面中显示 Hello Mvc in KH96;

2.1 HelloMvcController

@Controller //不可以用其他的分层注解替换,只能用controller  (1.可以作为一个控制层组件扫描到容器  2.代表一个控制器)
public class HelloMvcController {

    //接收helloMvc,跳转到目标success.jsp
    @RequestMapping("/helloMvc") //定义当前请求控制器中,可以处理请求的映射url字符串,前面的 / 可写 可不写
    public String helloSpringMvc(){
        System.out.println("------ 调用 HelloMvcController控制器的 helloSpringMvc() 请求处理方法------");

        //目标jsp视图页面的名称(不带后缀),区分大小写
        return "success";
    }
}

2.2 success.jsp

webapp/WEB-INF/views/success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>成功页面</title>
</head>
<body>
    <h2>KH96 Success Page</h2>
</body>
</html>

2.3 测试

2.4 总结

springMvc使用流程小结:

3、@RequestMapping

3.1 @RequestMapping可以写的位置

类上方法上

3.1.1 只用方法上有

@RequestMapping("/testSpringMvcRequestMappingTarget")
public String testMvcRequestMappingTarget(){
    //返回成功页面
    return "hello";
}

测试

3.1.2 类上 和 方法上都有

@Controller
@RequestMapping("/kh96")
public class SpringMvcController {

    @RequestMapping("/testSpringMvcRequestMappingTarget")
    public String testMvcRequestMappingTarget(){
        //返回成功页面
        return "hello";
    }
    
}

测试

3.2 method属性

3.2.1 method = RequestMethod.GET

@RequestMapping(value = "/testSpringMvcRequestMappingMethodGet",method = RequestMethod.GET)
public String testSpringMvcRequestMappingMethodGet(){

    System.out.println("----- @RequestMapping method = RequestMethod.GET ------");

    //返回成功页面
    return "hello";
}

使用PostMan测试

Get测试

Post测试

3.2.2 method = RequestMethod.POST

@RequestMapping(value = "/testSpringMvcRequestMappingMethodPost",method = RequestMethod.POST)
public String testSpringMvcRequestMappingMethodPost(){

    System.out.println("----- @RequestMapping method = RequestMethod.Post ------");

    //返回成功页面
    return "hello";
}

Get测试

Post测试

3.2.3 method = {RequestMethod.GET,RequestMethod.POST})

@RequestMapping(value = "/testSpringMvcRequestMappingMethodGetAndPost",method = {RequestMethod.GET,RequestMethod.POST})
public String testSpringMvcRequestMappingMethodGetAndPost(){

    System.out.println("----- @RequestMapping method = RequestMethod.Post ------");

    //返回成功页面
    return "hello";
}

Get测试

Post测试

3.3 params属性

指定当前请求处理方法对于请求,必要携带的参数

3.3.1 params ={"uname"}

指定一个参数,不指定参数值;

@RequestMapping(value = "/testSpringMvcRequestMappingParams",params ={"uname"} )
public String testSpringMvcRequestMappingParams(){

    System.out.println("----- @RequestMapping 属性 params------");

    //返回成功页面
    return "hello";
}

测试,不带参数

测试,带参数,不带值

测试,带参数,带值

3.3.2 params ={"uname=kh96"}

指定一个参数,指定值;

@RequestMapping(value = "/testSpringMvcRequestMappingParams",params ={"uname=kh96"} )
public String testSpringMvcRequestMappingParams(){

    System.out.println("----- @RequestMapping 属性 params------");

    //返回成功页面
    return "hello";
}

测试,带参数,带值,值不匹配

测试,带参数,带值,值匹配

3.3.3 params ={"uname=kh96","upwd=123"}

指定两个参数,并指定值;

@RequestMapping(value = "/testSpringMvcRequestMappingParams",params ={"uname=kh96","upwd=123"})
public String testSpringMvcRequestMappingParams(){

    System.out.println("----- @RequestMapping 属性 params------");

    //返回成功页面
    return "hello";
}

测试,带一个参数,并指定值

测试,带两个参数,并指定第一个值

测试,带两个参数,并指定第两个值

3.4 headers属性

指定当前请求处理方法对于请求,必要携带的请求头参数,用法和params类似,参数位置不同;

@RequestMapping(value = "/testSpringMvcRequestMappingHeaders",headers = {"token=123"})
public String testSpringMvcRequestMappingHeaders(){

    System.out.println("----- @RequestMapping 属性 headers------");

    //返回成功页面
    return "hello";
}

测试,主要观察参数位置,其他参数规则跟params一致

3.5 @RequestMapping + @PathVariable

请求映射注解:参数占位符注解 @PathVariable:

3.5.1 测试

3.5.1.1 占位符与@PathVariable注解指定的参数名不一致
@RequestMapping("/testSpringMvcRequestMappingPathVariable/{utel#}/{uemail}")
public String testSpringMvcRequestMappingPathVariable(@PathVariable("utel") String utel,@PathVariable("uemail") String uemail){

    System.out.println("----- @RequestMapping + @PathVariable utel:"+utel+",uemail"+uemail+" ------");

    //返回成功页面
    return "hello";
}

测试结果

3.5.1.2 占位符与@PathVariable注解指定的参数名一致
@RequestMapping("/testSpringMvcRequestMappingPathVariable/{utel}/{uemail}")
public String testSpringMvcRequestMappingPathVariable(@PathVariable("utel") String utel,@PathVariable("uemail") String uemail){

    System.out.println("----- @RequestMapping + @PathVariable utel:"+utel+",uemail"+uemail+" ------");

    //返回成功页面
    return "hello";
}

测试结果

3.5.1.3 @PathVariable不指定参数名

测试,占位符与形参不一致

@RequestMapping("/testSpringMvcRequestMappingPathVariable/{utel}/{uemail#}")
public String testSpringMvcRequestMappingPathVariable(@PathVariable("utel") String utel,@PathVariable String uemail){

    System.out.println("----- @RequestMapping + @PathVariable utel:"+utel+",uemail"+uemail+" ------");

    //返回成功页面
    return "hello";
}

测试结果

测试,占位符与形参一致

@RequestMapping("/testSpringMvcRequestMappingPathVariable/{utel}/{uemail}")
public String testSpringMvcRequestMappingPathVariable(@PathVariable("utel") String utel,@PathVariable String uemail){

    System.out.println("----- @RequestMapping + @PathVariable utel:"+utel+",uemail"+uemail+" ------");

    //返回成功页面
    return "hello";
}

测试结果

4.Rest风格

相同请求路径,通过请求方式判断请求方法

自定义请求方式,一定要带一个名字为_method的参数

4.1 Rest风格 请求转换过滤器

<!-- Rest风格 请求转换过滤器-->
<filter>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

4.1 get------查询 select

请求方法

//请求映射注解:rest风格处理-get,查询select
//@RequestMapping(value = "/testSpringMvcRequestMappingRestGet/{uid}",method = RequestMethod.GET)
@RequestMapping(value = "/testSpringMvcRequestMappingRest/{uid}",method = RequestMethod.GET)
public String testSpringMvcRequestMappingRestGet(@PathVariable("uid") String uid){

    System.out.println("----- Rest Get 根据 uid:"+uid+" 查询用户详情 ------");

    //返回成功页面
    return "hello";
}

jsp

<h3>Get 请求映射注解:rest风格处理-get,查询select</h3>
<h3><a href="${pageContext.request.contextPath}/testSpringMvcRequestMappingRest/KH96_01">Test SpringMvc Rest Get</a> </h3>

测试

4.2post------新增 insert

//请求映射注解:rest风格处理-post,新增insert
//@RequestMapping(value = "/testSpringMvcRequestMappingRestPost",method = RequestMethod.POST)
@RequestMapping(value = "/testSpringMvcRequestMappingRest",method = RequestMethod.POST)
public String testSpringMvcRequestMappingRestPost(){

    System.out.println("----- Rest Post 新增用户详情  ------");

    //返回成功页面
    return "hello";
}

jsp

<h3>Post 求映射注解:rest风格处理-post,新增insert</h3>
<form action="testSpringMvcRequestMappingRest" method="post">
    <input type="submit" value="Test SpringMvc Rest Post">
</form>

测试

4.3 put------更新 update

//请求映射注解:rest风格处理-put,更新update
//@RequestMapping(value = "/testSpringMvcRequestMappingRestPut",method = RequestMethod.PUT)
@RequestMapping(value = "/testSpringMvcRequestMappingRest",method = RequestMethod.PUT)
public String testSpringMvcRequestMappingRestPut(){

    System.out.println("----- Rest Put 更新用户详情 ------");

    //返回成功页面
    return "hello";
}

jsp

<h3>Put 请求映射注解:rest风格处理-put,更新update</h3>
<form action="testSpringMvcRequestMappingRest" method="post">
    <input type="hidden" name="_method" value="put">
    <input type="submit" value="Test SpringMvc Rest put">
</form>

测试

4.4 delete------删除 delete

//请求映射注解:rest风格处理-delete,删除delete
//@RequestMapping(value = "/testSpringMvcRequestMappingRestDelete/{uid}",method = RequestMethod.DELETE)
@RequestMapping(value = "/testSpringMvcRequestMappingRest/{uid}",method = RequestMethod.DELETE)
public String testSpringMvcRequestMappingRestDelete(@PathVariable("uid") String uid){

    System.out.println("----- Rest Delete 根据 uid:"+uid+" 删除用户  ------");

    //返回成功页面
    return "hello";
}

jsp

<h3>Delete 请求映射注解:rest风格处理-delete,删除delete</h3>
<form action="testSpringMvcRequestMappingRest/KH96_01" method="post">
    <input type="hidden" name="_method" value="delete">
    <input type="submit" value="Test SpringMvc Rest delete">
</form>

测试

标签:utel,String,SpringMvc,PathVariable,初识,------,请求,RequestMapping
来源: https://www.cnblogs.com/xiaoqigui/p/16651065.html