SpringMVC-RESTful

index.html

html 复制代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a th:href="@{/user}">user</a>
<!--根据id查询用户信息-->
<a th:href="@{/user/1}">restful</a>
<form th:action="@{user}" method="post">
    <input type="submit" value="添加用户信息">
</form>
<!--put,delete需设置xml文件过滤器-->
<form th:action="@{user}" method="post">
    <input type="hidden" name="_method" value="put">
    <input type="submit" value="修改用户信息">
</form>
<form th:action="@{user/1}" method="post">
    <input type="hidden" name="_method" value="delete">
    <input type="submit" value="删除用户信息">
</form><hr>
<a th:href="@{/employees}">查询所有员工信息</a>
</body>
</html>

Controller

java 复制代码
@Controller
public class Restful {
//    @RequestMapping("/")
//    public String indexTest(){
//        System.out.println("over");
//        return "index";
//    }
//    @RequestMapping(value = "/user",method = RequestMethod.GET)
    @GetMapping("/user")
    public String userTest(){
        System.out.println("user");
        return "success";
    }
//    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    @GetMapping("/user/{id}")
    public String restfulTest(@PathVariable Integer id){
        System.out.println("根据id查询用户信息"+id+"");
        return "success";
    }
//    添加用户信息
//    @RequestMapping(value = "/user",method = RequestMethod.POST)
    @PostMapping("/user")
    public String postrestfulTest(){
        System.out.println("添加用户信息");
        return "success";
    }
    //    修改用户信息
//    @RequestMapping(value = "/user",method = RequestMethod.PUT)
    @PutMapping("/user")
    public String putrestfulTest(){
        System.out.println("修改用户信息");
        return "success";
    }
    //    删除用户信息
//    @RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
    @DeleteMapping("/user/{id}")
    public String deleterestfulTest(@PathVariable Integer id){
        System.out.println("根据id删除用户信息"+id+"");
        return "success";
    }

}

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">
<!--    配置Spring的编码过滤器-->
    <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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
<!--    设置请求方式过滤器put,delete-->
    <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>
<!--    配置SpringMVC前端控制器DispatcherServlet-->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--        设置SpringMVC配置文件的位置和名称-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springRestful.xml</param-value>
        </init-param>
<!--        将DispatcherServlet的初始化时间提前到服务器启动时-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

springRESTful.xml

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.zmx"></context:component-scan>
<!--    配置Thymeleaf视图解析器-->
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!--                        视图前缀-->
                        <property name="prefix" value="/WEB-INF/templates/"/>
<!--                        视图后缀-->
                        <property name="suffix" value=".html"/>
                        <property name="templateMode" value="HTML5"/>
                        <property name="characterEncoding" value="UTF-8"/>
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
<!--    开启mvc注解驱动-->
    <mvc:annotation-driven/>
<!--    配置默认的servlet处理静态资源--><!--    开启mvc注解驱动-->
    <mvc:default-servlet-handler/>
<!--    视图控制器:为当前的请求直接设置视图名称实现页面跳转--><!--    开启mvc注解驱动-->

    <mvc:view-controller path="/" view-name="index"></mvc:view-controller>
    <mvc:view-controller path="/to/add" view-name="employees_add"></mvc:view-controller>
</beans>
相关推荐
王二端茶倒水12 分钟前
大龄程序员兼职跑外卖第五周之亲身感悟
前端·后端·程序员
像污秽一样12 分钟前
Spring MVC初探
java·spring·mvc
LuckyLay16 分钟前
Spring学习笔记_36——@RequestMapping
java·spring boot·笔记·spring·mapping
RainbowSea1 小时前
4. Spring Cloud Ribbon 实现“负载均衡”的详细配置说明
java·spring·spring cloud
夜色呦1 小时前
现代电商解决方案:Spring Boot框架实践
数据库·spring boot·后端
爱敲代码的小冰1 小时前
spring boot 请求
java·spring boot·后端
java小吕布3 小时前
Java中的排序算法:探索与比较
java·后端·算法·排序算法
开心工作室_kaic3 小时前
ssm111基于MVC的舞蹈网站的设计与实现+vue(论文+源码)_kaic
前端·vue.js·mvc
2401_857600953 小时前
深入剖析:Spring MVC与Struts的较量
struts·spring·mvc
被猫枕的咸鱼3 小时前
springmvc通过使用map来进行数据的接收和使用
spring