SpringMVC-02-Restful风格

Restful风格

  • 创建一个web项目
  • 在web.xml中配置DispatcherServlet
<?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">

    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
  • 在resource目录下配置springmvc-servlet.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.wang.controller"/>
    <!--这俩东西也可以不用-->
   <!-- <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>-->

    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
    </bean>
</beans>
  • 创建包和类
package com.wang.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class RestFul {

    //原始风格
    //localhost8080://项目名/r1?a=1&b=2
    @RequestMapping("/r1")
    public String test1(int a, int b, Model model){

        int s=a+b;
        model.addAttribute("msg",s);

        return "restful01";
    }

    //RestFul风格
    //localhost8080://项目名/r2/1/5(1和5是a,b的值)
    @RequestMapping("/r2/{a}/{b}")
    public String test2(@PathVariable int a,@PathVariable int b, Model model){

        int s=a+b;
        model.addAttribute("msg",s);
        return "restful02";
    }

    //用RestFul风格以get方式请求  方式一
    @RequestMapping(value="/r3/{a}/{b}",method = RequestMethod.GET)
    public String test3(@PathVariable int a,@PathVariable int b,Model model){
        int s=a+b;
        model.addAttribute("msg",s);
        return "restful03";
    }

    //用RestFul风格以get方式请求  方式二
    @GetMapping("/r4/{a}/{b}")
    public String test4(@PathVariable int a,@PathVariable int b,Model model){
        int s=a+b;
        model.addAttribute("msg",s);
        return "restful03";
    }


    //用RestFul风格以post方式请求  方式一(我们需要一个表单restful.jsp)
    //localhost8080://项目名/r5(因为是post请求,所以a,b不需要我们在url上赋值)
    @RequestMapping(value = "/r5/{a}/{b}",method = RequestMethod.POST)
    public String test5(@PathVariable int a,@PathVariable int b,Model model){
        int s=a+b;
        model.addAttribute("msg",s);
        return "restful05";
    }

    //方式二就相当于get请求的方式二 (@PostMapping)

}

  • 视图jsp文件
    在这里插入图片描述
    上面的包自行测试

  • restful.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

//直接点提交
<form action="/smvc3/r5/1/5" method="post">

    <input type="submit">
</form>
</body>
</html>

最后进行测试

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

QQ星小天才

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值