SpringMVC之参数请求映射实例

一、配置对应的jar包

– commons-logging-1.1.3.jar
– spring-aop-4.0.0.RELEASE.jar
– spring-beans-4.0.0.RELEASE.jar
– spring-context-4.0.0.RELEASE.jar
– spring-core-4.0.0.RELEASE.jar
– spring-expression-4.0.0.RELEASE.jar
– spring-web-4.0.0.RELEASE.jar
– spring-webmvc-4.0.0.RELEASE.jar

二、配置web.xml

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_2_5.xsd"
	id="WebApp_ID" version="2.5">

	<!-- 配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 
		请求转为 DELETE 或 POST 请求 -->
	<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>

	<!-- 配置 DispatcherServlet -->

	<!-- 配置 DispatcherServlet 的一个初始化参数: 配置 SpringMVC 配置文件的位置和名称 -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>


</web-app>
  • 配置 DispatcherServlet :DispatcherServlet 默认加载 Spring 配置文件, 启动Spring 容器。
  • 通过 contextConfigLocation 初始化参数自定
    义配置文件的位置和名称
<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		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
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
	<!-- 配置自定扫描的包 -->
	<context:component-scan base-package="com.qst"></context:component-scan>


	<!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>

请求处理类

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorld {
    
    

	/**
	 * 1. 使用 @RequestMapping 注解来映射请求的 URL
	 * 2. 返回值会通过视图解析器解析为实际的物理视图, 对于 InternalResourceViewResolver 视图解析器, 会做如下的解析:
	 * 通过 prefix + returnVal + 后缀 这样的方式得到实际的物理视图, 然会做转发操作
	 * 
	 * /WEB-INF/views/success.jsp
	 * 
	 * @return
	 */
	@RequestMapping("/helloworld")
	public String hello(){
    
    
		System.out.println("hello world");
		return "success";
	}
	
}

index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

<a href="Spring/helloworld">Hello World</a>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>success
</body>
</html>

处理流程:
@RequestMapping 注解为控制器指定可以处理URL 请求

  1. 类定义处理:提供初步的请求映射信息。
  2. 方法处理:提供进一步的细分映射信息。
    DispatcherServlet 截获请求后,通过控制器上
    @RequestMapping 提供的映射地址确定请求所对应的处理
    方法。

RequestMapping_HiddenHttpMethodFilter过滤器

@PathVariable具体应用
POST/GET/PUT/DELETE请求

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>


	<form action="Spring/SHOWTEST/1" method="POST">
		名字:<input type="hidden" name="_method" value="PUT"> <input type="submit"
			value="PUT">
	</form>
	<br />
	
	<form action="Spring/SHOWTEST/1" method="POST">
		名字:<input type="hidden" name="_method" value="DELETE"> <input type="submit"
			value="DELETE">
	</form>
	<br />
	
	<a href="Spring/SHOWTEST/1">GET</a>

	<br />
	

	<form action="Spring/SHOWTEST" method="POST">
		<input type="submit" value="POST">
	</form>

<br />
	
</body>
</html>

在web.xml中配置

<!-- 配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 
		请求转为 DELETE 或 POST 请求 -->
	<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>
package com.qst;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/Spring")
@Controller
public class Show {
    
    



	/**
	 * 
	 *	新增 POST
	 *	修改 PUT
	 *	获取 GET
	 *	删除 DELETE
	 *	 思路:发送PUT和DELETE请求
	 *     需要在发送post请求时携带name=“_method”的隐藏域,value值为DELETE或PUT
	 */

	@ResponseBody
	@RequestMapping(value = "/SHOWTEST/{id}", method = RequestMethod.PUT)
	public String PUT(@PathVariable("id") Integer id) {
    
    

		System.out.println("PUT请求:" + id);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/SHOWTEST/{id}", method = RequestMethod.DELETE)

	public String DELETE(@PathVariable("id") Integer id) {
    
    

		System.out.println("DELETE请求:" + id);
		return "success";
	}

	@RequestMapping(value = "/SHOWTEST/{id}", method = RequestMethod.GET)
	public String GET(@PathVariable("id") Integer id) {
    
    

		System.out.println("GET请求:" + id);
		return "success";
	}

	@RequestMapping(value = "/SHOWTEST", method = RequestMethod.POST)
	public String POST() {
    
    

		System.out.println("POST请求");
		return "success";
	}
}

在这里插入图片描述
通过@RequestParam绑定请求参数值

  • value:参数名
  • required:是否必须。默认为 true, 表示请求参数中必须包含对应的参数,若不存在,将抛出异常
  • defaultValue = “0” 设置默认参数
<form action="Spring/REP" method="POST">
		名字:<input type="text" name="user"> <input type="submit"
			value="PUT">
  </form>
	@RequestMapping("/REP")
	public String R(@RequestParam("user") String n) {
    
    
		System.out.println("user:"+n);
		return "success";
		
	}
	

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44763595/article/details/107999389