24.1 新建一个MethodController类
package com.ask.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
//添加地址;
@RequestMapping("/user")
@Controller
public class MethodController{
@RequestMapping(value="/get/{id}",method=RequestMethod.GET)
public String getMethod(@PathVariable("id") int id){
System.out.println("get "+id);
return "get";
} @RequestMapping(value="/post/{id}",method=RequestMethod.POST)
public String postMethod(@PathVariable("id") int id){
System.out.println("post "+id);
return "post";
}
@RequestMapping(value="/put/{id}",method=RequestMethod.PUT)
public String putMethod(@PathVariable("id") int id){
System.out.println("put "+id);
return "put";
} @RequestMapping(value="/delete/{id}",method=RequestMethod.DELETE)
public String deleteMethod(@PathVariable("id") int id){
System.out.println("get "+id);
return "delete";
}
}
24.2 在WEB-INF下的views下新建三个四个jsp文件 get put post delete这里只是写了一个get.jsp文件
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
请求方法为get请求
</body>
</html>
24.3 在WEB-INF文件下的下新建一个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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--扫描包-->
<context:component-scan base-package="com.ask"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
24.4 配置web.xml的文件;
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>springMvc-1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 拦截器 -->
<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>
<!--这个servlet-name的值要和springmvc-servlet.xml的文件名保持一致-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
24.5 新建一个index.jsp的文件;
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<a href="user/get/100">click get</a>
<form action="user/post/200" method="post">
<input type="submit" value="post提交方式">
</form>
<form action="user/put/300" method="post">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="put提交方式">
</form>
<form action="user/delete/100" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="delete提交方式">
</form>
</body>
</html>