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>
最后进行测试