什么是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 转发与重定向
关键词:
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"; //转发
}
}