spring中的RequestMapping的四种请求方式

本文介绍了一个使用Spring MVC框架实现不同HTTP方法(GET, POST, PUT, DELETE)请求映射的例子,通过创建MethodController类来处理不同的请求类型,并且通过视图解析器返回相应的JSP页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值