【SpringMVC】---RequestMapping、Ant 路径、PathVariable 注解、HiddenHttpMethodFilter 过滤器、用 POJO 作为参数
一、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">
<display-name>SpringMVC_001_001</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>
<!-- 配置 DispatcherServlet -->
<servlet>
<!-- springDispatcherServlet 在应用启动的时候被创建,不是调用的时候被创建。 -->
<!-- 实际上也可以不通过 contextConfigLocation 来配置 springmvc 的配置文件,而使用默认的。 -->
<!-- 默认的配置文件为:/WEB-INF/<servlet-name>-servlet.xml -->
<!-- 把 SRC 下的 spring-mvc.xml 移动在/WEB-INF/下并改名为 springDispatcherServlet-servlet.xml的文件,并注释 R20~R23 -->
<!--配置 DispatcherServlet 初始化参数,作用是配置 SpringMVC 配置文件的位置和名称--> <servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <!-- 将所有请求映射到DispatcherServlet处理 --> <servlet-mapping> <!--配置 DispatcherServlet 初始化参数,作用是配置 SpringMVC 配置文件的位置和名称-->
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!-- 配置HiddenHttpMethodFilter,可以把POST请求转换为DELETE或PUT请求 -->
<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>
</web-app>
二、HelloWorld.java
package com.chinasofti.springmvc.handlers; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 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 com.chinasofti.springmvc.entity.User; @Controller
@RequestMapping(value="/springmvc")
public class HelloWorld {
/**
* 1、使用 RequestMapping 注解来映射请求的 URL(统一资源定位)
* 2、返回值会通过视图解析器解析为实际的物理视图,对于 InternalResourceViewResolver 而言,
* 视图解析器会做如下的解析:
* 2.1、通过 prefix + returnVal + 后缀这样的方式得到实际的物理视图,然后做转发操作。
* 如:/WEB-INF/views/success.jsp
* @return
*/ public static final String SUCCESS="success"; /*
* 测试跳换页面
*/
@RequestMapping("/helloword")
public String hello(){
System.out.println("hello word");
return SUCCESS;
} /*
* 测试Post提交
*/
@RequestMapping(value="/helloword",method=RequestMethod.POST)
public String testPost(){
System.out.println("hello word");
return SUCCESS;
} /*
* 测试Params参数 params表示要传递的参数,username表示传递的第一个参数,值不确定,第二个参数是
age并且年龄不能是等于10的
* */
@RequestMapping(value="testParamaAndHeaders",params={"username","age!=10"},
headers={"Accept-Language=zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3"})
public String testParamaAndHeaders(){
System.out.println("testParamaAndHeaders");
return SUCCESS;
} /*
* 测试Ant格式请求路径
* */
@RequestMapping(value="testAnt/*/abc",params={"username","age!=10"},
headers={"Accept-Language=zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3"})
public String testAnt(){
System.out.println("testAntPath");
return SUCCESS;
} /*
* @PathVariable可以映射URL中的占位符到目标方法中
*/
@RequestMapping("/testPathVariable/{id}")
public String testPathVariable(@PathVariable("id") Integer id){
System.out.println("testPathVariable:"+id);
return SUCCESS;
} /*
*测试GET方法 --------------》查
*/
@RequestMapping(value = "/testRest/{id}", method = RequestMethod.GET)
public String testRest(@PathVariable Integer id) {
System.out.println("testRest GET:" + id);
return SUCCESS;
} // POST是新增,没有参数。------》增
@RequestMapping(value = "/testRest", method = RequestMethod.POST)
public String testRest() {
System.out.println("testRest POST");
return SUCCESS;
}
/*
* 测试DELETE方法-----------》删
*/
@RequestMapping(value = "/testRest/{id}", method = RequestMethod.DELETE)
public String testDeleteRest(@PathVariable Integer id) {
System.out.println("testRest DELETE:" + id);
return SUCCESS;
}
/*
* 测试PUT方法--------------》改
*/
@RequestMapping(value = "/testRest/{id}", method = RequestMethod.PUT)
public String testPutRest(@PathVariable Integer id) {
System.out.println("testRest PUT:" + id);
return SUCCESS;
} /*
* 测试POJO
*/
@RequestMapping("/testPojo")
public String testPojo(User user){
System.out.println(user.getUsername()+"\t"+user.getPassword()+"\t"+user.getEmail()+"\t"+user.getAge()
+"\t"+user.getAddress().getProvince()+"\t"+user.getAddress().getCity());
return SUCCESS;
} /*
* 使用 Servlet 原生 API 作为参数
* **HttpServletRequest
* **HttpServletResponse
* **HttpSession
* Java.security.Principal
* Locale
* InputStream
* OutputStream
* Reader
* Writer
*/
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request,HttpServletResponse response){
System.out.println("ServletAPI:"+request+"\t\r"+response);
return SUCCESS;
}
}
三、Address 类
package com.chinasofti.springmvc.entity;
public class Address {
private String province;
private String city;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Address [province=" + province + ", city=" + city + "]";
}
}
User类
package com.chinasofti.springmvc.entity;
public class User {
private String username;
private String password;
private String email;
private Integer age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", email=" + email + ", age=" + age
+ ", address=" + address + "]";
}
}
四、spring-mvc.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.chinasofti"></context:component-scan> <!-- 配置视图解析器:如何把 handler 方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean> </beans>
五、success.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>
测试成功!
</body>
</html>
六、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="springmvc/helloword">跳转页面</a>
<br/>
<br/>
<hr />
<form action="springmvc/helloword" method="post">
<input type="submit" value="POST提交">
</form>
<br/>
<hr />
<a href="springmvc/testParamaAndHeaders?username='huazai'&age=11">测试params</a>
<br/>
<br/>
<hr />
<a href="springmvc/testAnt/xxxxxxxx/abc?username=huazai&age=11">测试Ant请求路径</a>
<br/>
<br/>
<hr />
<a href="springmvc/testPathVariable/11">测试PathVariable 注解请求路径</a>
<br/>
<br/>
<br/>
<hr />
<form action="springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="Test Rest DELETE">
</form>
<hr />
<form action="springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="Test Rest PUT">
</form>
<hr />
<form action="springmvc/testRest" method="post">
<input type="submit" value="Test Rest Post">
</form>
<hr>
<a href="springmvc/testRest/1">Test Rest Get</a>
<hr>
<form action="springmvc/testPojo" method="post">
username:<input type="text" value="" name="username"><br/>
password:<input type="password" value="" name="password"><br/>
email: <input type="text" value="" name="email"><br/>
age: <input type="text" value="" name="age"><br/>
province:<input type="text" value="" name="address.province"><br/>
city: <input type="text" value="" name="address.city"><br/>
<input type="submit" value="gogogo">
</form>
<hr>
<a href="springmvc/testServletAPI">testServletAPI</a> </body>
</html>
关于-RequestMapping、Ant 路径、PathVariable 注解、HiddenHttpMethodFilter 过滤器、用 POJO 作为参数
【SpringMVC】---RequestMapping、Ant 路径、PathVariable 注解、HiddenHttpMethodFilter 过滤器、用 POJO 作为参数的更多相关文章
- 003 RequestMapping——Ant路径
一: 1.介绍 Ant风格资源地址支持3中配配符 ?:匹配文件名中的一个字符 * :匹配文件名中的任意字符 **:匹配多层路径 2.RequestMapping支持的Ant风格的路径 二:程序说明 ...
- SpringMVC的controller方法中注解方式传List参数使用@RequestBody
在SpringMVC控制器方法中使用注解方式传List类型的参数时,要使用@RequestBody注解而不是@RequestParam注解: //创建文件夹 @RequestMapping(value ...
- SpringMVC(二):RequestMapping修饰类、指定请求方式、请求参数或请求头、支持Ant路径
@RequestMapping用来映射请求:RequestMapping可以修饰方法外,还可以修饰类 1)SpringMVC使用@RequestMapping注解为控制指定可以处理哪些URL请求: 2 ...
- @RequestMapping 和@ResponseBody 和 @RequestBody和@PathVariable 注解 注解用法
接下来讲解一下 @RequestMapping 和@ResponseBody 和 @RequestBody和@PathVariable 注解 注解用法 @RequestMapping 为url映射路 ...
- Spring注解@RequestMapping请求路径映射问题
@RequestMapping请求路径映射,假设标注在某个controller的类级别上,则表明訪问此类路径下的方法都要加上其配置的路径.最经常使用是标注在方法上.表明哪个详细的方法来接受处理某次请求 ...
- SpringMvc中@PathVariable注解简单的用法
@PathVariable /** * @PathVariable 可以来映射 URL 中的占位符到目标方法的参数中. * @param id * @return */ jsp页面请求 <a h ...
- SpringMVC RequestMapping注解
1.@RequestMapping 除了修饰方法,还可以修饰类 2.类定义处:提供初步的请求映射信息.相对于WEB应用的根目录 方法处:提供进一步细分映射信息 相对于类定义处的URL.若类定义处未 ...
- SpringMVC RequestMapping & 请求参数
SpringMVC 概述 Spring 为展现层提供的基于 MVC 设计理念的优秀的Web 框架,是目前最主流的 MVC 框架之一 Spring3.0 后全面超越 Struts2,成为最优秀的 MVC ...
- SpringMVC RequestMapping 详解
SpringMVC RequestMapping 详解 RequestMapping这个注解在SpringMVC扮演着非常重要的角色,可以说是随处可见.它的知识点很简单.今天我们就一起学习Spring ...
随机推荐
- 设备中LPC2368芯片个例参数问题导致故障的分析
最近公司的设备客户报告在终端客户那里出现了板卡加热不受控,出现了持续加热导致设备一些贵重部件损坏.由于历史上很多现场问题,板卡什么拆到别的地方搭复现平台,基本都是以失败告终,所以出差去现场分析. 过程 ...
- Azure云服务托管恶意软件
微软Azure云服务被用于托管恶意软件,可控制多达90台电脑 BleepingComputer称,在早期报道中,5月份陆续出现了两起与Azure相关的恶意软件攻击事件: 1.自5月10日以来,Azur ...
- DataWorks参数配置
https://help.aliyun.com/document_detail/30281.html?spm=a2c6h.13066369.0.0.7bef69daI5ajKt
- SetConsoleTextAttribute和SetConsoleScreenBufferInfoEx的使用
主要是作用于控制台文本下划线和改变文本颜色 #include "pch.h" #include <iostream> #include <Windows.h> ...
- CSS中filter属性的使用
filter 属性定义了元素的可视效果 blur 给图像设置高斯模糊."radius"一值设定高斯函数的标准差,或者是屏幕上以多少像素融在一起, 所以值越大越模糊. 如果没有设定值 ...
- Linux系统中的硬件问题如何排查?(1)
在Linux系统中,对于硬件故障问题的排查可能是计算机管理领域最棘手的工作,即使是经验相当丰富的用户有时也会遇上自己搞不定的状况,本文分享一些实用的技巧与处理方法,希望有助于读者朋友理解.查明并最终搞 ...
- Python服务器开发一:python基础
Python服务器开发一:python基础 Python(蟒蛇)是一种动态解释型的编程语言.Python可以在Windows.UNIX.MAC等多种操作系统上使用,也可以在Java..NET开发平 ...
- 微信小程序-饮食日志_开发记录01
今天主要了解微信小程序的框架结构以及环境部署等. 小程序的框架主要分为: js.json.wxss.wxml等 和java web的内容相似,主要了解内部代码的使用情况和语言方式. 主要写了页面的框架 ...
- POJ-2104-Kth Number(主席树)
链接: https://vjudge.net/problem/POJ-2104#author=malic 题意: 给定一个数组 a[1...n],数组元素各不相同,你的程序要对每次查询Q(i,j,k) ...
- C#与JAVA的互通
C#与JAVA的互通 https://blog.csdn.net/sinat_35165183/article/details/68936910