springMVC---resultful风格

目录

一、创建项目

pom.xml

二、配置文件

1.web.xml

2.spring-mvc.xml

三、图解

四、controller


一、创建项目

pom.xml

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.qcby</groupId>
    <artifactId>springMVC14</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!--版本锁定-->
        <spring.version>5.0.2.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <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>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

</project>

二、配置文件

1.web.xml

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">

    <!-- 配置过滤器,解决中文乱码的问题 -->
    <filter>
        <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!--前端控制器-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!--配置启动加载-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <!--<url-pattern>*.do</url-pattern>-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

2.spring-mvc.xml

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置spring创建容器时要扫描的包 -->
    <context:component-scan base-package="com.qcby.controller"></context:component-scan>

    <!-- 配置视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- 配置spring开启注解mvc的支持-->
    <!--    <mvc:annotation-driven></mvc:annotation-driven>-->
</beans>

三、图解

Get对应select(查询)

Post对应insert(添加)

Put对应update(修改)

Delete对应delete(删除)

他们的请求路径一样,是根据不同的请求方式去执行后台的不同方法

四、controller

1.Get和Post请求就是在form表单里设置method="get"/method="post"

2.发起put和delete请求:

(1)有post请求的form标签

(2)在form表单中添加一个额外的隐藏域 _method="put"/_method="delete"

(3)web.xml中配置一个Filter过滤器org.springframework.web.filter.HiddenHttpMethodFilter(注:这个Filter必须要在处理乱码的Filter)

3.web.xml

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">

    <!-- 配置过滤器,解决中文乱码的问题 -->
    <filter>
        <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--负责把隐藏域中的put..,改为请求的put请求..-->
    <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>


    <!--前端控制器-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!--配置启动加载-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <!--<url-pattern>*.do</url-pattern>-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

controller

java 复制代码
package com.qcby.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class UserController {

    // 发送Get查询所有
    @GetMapping("/user")
    public String getUserAll(){
        System.out.println("查询所有用户");
        return "success";
    }

    // 发送Get查询一个
    @GetMapping("/user/{id}")
    public String getUserById(@PathVariable(value = "id") Integer id){
        System.out.println("根据id查询用户信息,id:"+id);
        return "success";
    }

    // 发送Post--添加
    @PostMapping("/user")
    public String UserInsert(String username,String password){
        System.out.println("添加用户,用户名:"+username+",密码:"+password);
        return "success";
    }

    // 发送put--修改
    @PutMapping("/user/{id}")
    public String UserUpdate(@PathVariable(value = "id") Integer id,String username,String password){
        System.out.println("修改id为"+id+"的用户信息,用户名为:"+username+",密码为:"+password);
        return "success";
    }

    // 发送Delete--删除
    @DeleteMapping(value = "/user/{id}")
    public String deleteUserById(@PathVariable String id) {
        System.out.println("删除id为" + id+"的用户信息");
        return "success";
    }
}
相关推荐
sin220113 小时前
springMVC---常用注解
mvc
穷儒公羊1 天前
第三十八章 Spring之假如让你来写MVC——适配器篇
spring·servlet·mvc·web·jsp
小王不会写code1 天前
MVC执行流程
mvc
穷儒公羊1 天前
第三十六章 Spring之假如让你来写MVC——拦截器篇
java·后端·spring·servlet·mvc·jsp
大梦百万秋3 天前
如何使用MVC模式设计和实现校园自助点餐系统的微信小程序
微信小程序·小程序·mvc
JosieBook3 天前
【开源项目】基于ASP.NET MVC开发的、开源的个人博客系统
开源·asp.net·mvc
独孤求败Ace3 天前
第31天:Web开发-PHP应用&TP框架&MVC模型&路由访问&模版渲染&安全写法&版本漏洞
前端·php·mvc
新知图书4 天前
Spring MVC简单数据绑定
java·spring·mvc
杂货铺的小掌柜4 天前
spring mvc源码学习笔记之八
学习·spring·mvc