RESTful风格、转发与重定向

什么是RESTful

RESTful就是一个资源定位及资源操作的风格,它不是协议也不是标准。基于这种风格设计更简洁、更有层次感,更加易于实现缓存机制。

资源:就是网络上的一个实体,或者是信息

资源操作:
1. GET:获取资源(查询)
2. POST:用来新建资源或更新资源(增加)
3. PUT:更新资源 (更新)
4. DELETE:删除资源 (删除)

例:

package com.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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

    //restful
    @RequestMapping("/hello/{a}/{b}")
    public String test1(@PathVariable int a, @PathVariable int b, Model model){
        int result = a + b;
        model.addAttribute("msg","结果为"+result);
        return "test"; //WEB-INF/jsp/test.jsp
    }

    @PostMapping("/hello/{a}/{b}")
    public String test2(@PathVariable int a, @PathVariable int b, Model model){
        int result = a + b;
        model.addAttribute("msg","结果为"+result);
        return "test"; //WEB-INF/jsp/test.jsp
    }

    @GetMapping("/hello/{a}/{b}")
    public String test3(@PathVariable int a, @PathVariable int b, Model model){
        int result = a + b;
        model.addAttribute("msg","结果为"+result);
        return "test"; //WEB-INF/jsp/test.jsp
    }
}

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.controller" />
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

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

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

Spring MVC 转发与重定向

Spring MVC转发与重定向

关键词:
forward:转发
redirect:重定向

package com.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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

    //restful
    @RequestMapping("/hello/{a}/{b}")
    public String test1(@PathVariable int a, @PathVariable int b, Model model){
        int result = a + b;
        model.addAttribute("msg","结果为"+result);
        return "test"; //转发
    }

    @PostMapping("/hello/{a}/{b}")
    public String test2(@PathVariable int a, @PathVariable int b, Model model){
        int result = a + b;
        model.addAttribute("msg","结果为"+result);
        return "redirect:index"; //重定向
    }

    @GetMapping("/hello/{a}/{b}")
    public String test3(@PathVariable int a, @PathVariable int b, Model model){
        int result = a + b;
        model.addAttribute("msg","结果为"+result);
        return "forward:test";  //转发
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值