一、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 作为参数的更多相关文章

  1. 003 RequestMapping——Ant路径

    一: 1.介绍 Ant风格资源地址支持3中配配符 ?:匹配文件名中的一个字符 *  :匹配文件名中的任意字符 **:匹配多层路径 2.RequestMapping支持的Ant风格的路径 二:程序说明 ...

  2. SpringMVC的controller方法中注解方式传List参数使用@RequestBody

    在SpringMVC控制器方法中使用注解方式传List类型的参数时,要使用@RequestBody注解而不是@RequestParam注解: //创建文件夹 @RequestMapping(value ...

  3. SpringMVC(二):RequestMapping修饰类、指定请求方式、请求参数或请求头、支持Ant路径

    @RequestMapping用来映射请求:RequestMapping可以修饰方法外,还可以修饰类 1)SpringMVC使用@RequestMapping注解为控制指定可以处理哪些URL请求: 2 ...

  4. @RequestMapping 和@ResponseBody 和 @RequestBody和@PathVariable 注解 注解用法

    接下来讲解一下 @RequestMapping  和@ResponseBody 和 @RequestBody和@PathVariable 注解 注解用法 @RequestMapping 为url映射路 ...

  5. Spring注解@RequestMapping请求路径映射问题

    @RequestMapping请求路径映射,假设标注在某个controller的类级别上,则表明訪问此类路径下的方法都要加上其配置的路径.最经常使用是标注在方法上.表明哪个详细的方法来接受处理某次请求 ...

  6. SpringMvc中@PathVariable注解简单的用法

    @PathVariable /** * @PathVariable 可以来映射 URL 中的占位符到目标方法的参数中. * @param id * @return */ jsp页面请求 <a h ...

  7. SpringMVC RequestMapping注解

    1.@RequestMapping 除了修饰方法,还可以修饰类 2.类定义处:提供初步的请求映射信息.相对于WEB应用的根目录  方法处:提供进一步细分映射信息  相对于类定义处的URL.若类定义处未 ...

  8. SpringMVC RequestMapping & 请求参数

    SpringMVC 概述 Spring 为展现层提供的基于 MVC 设计理念的优秀的Web 框架,是目前最主流的 MVC 框架之一 Spring3.0 后全面超越 Struts2,成为最优秀的 MVC ...

  9. SpringMVC RequestMapping 详解

    SpringMVC RequestMapping 详解 RequestMapping这个注解在SpringMVC扮演着非常重要的角色,可以说是随处可见.它的知识点很简单.今天我们就一起学习Spring ...

随机推荐

  1. MATLAB中的函数句柄及其应用

    1.函数句柄的创建 函数句柄(function handle)是MATLAB中的一类特殊的数据结构,它的地位类似于其它计算机语言里的函数对象(Javascript,Python),函数指针(C++), ...

  2. cdn工作原理

    cdn工作原理 1.用户向浏览器输入www.web.com这个域名,浏览器第一次发现本地没有dns缓存,则向网站的DNS服务器请求: 2.网站的DNS域名解析器设置了CNAME,指向了www.web. ...

  3. js实现倒计时(分:秒)

    上代码: //倒计时start 需要传入的参数为秒数,此方法倒计时结束后会自动刷新页面 function resetTime(timetamp){ var timer=null; var t=time ...

  4. [工具] BurpSuite--快速生成CSRF POC

    我们使用工具分析出存在csrf漏洞时,可以快速生成这个请求的poc,下面我们来看看怎么快速生成 0x00 上图是通过proxy,点击action,选择上图的选项即可生成这个请求的CSRF Poc了 当 ...

  5. SQLSERVER调用OPENROWSET的方法

    前言:正好这两天在同步生产环境的某张表数据到测试环境,之前用过一些同步数据软件,感觉不太可靠,有时候稍有操作不当,就会出现生产环境数据被清空等情况,还要去恢复数据.如果能恢复还好,不能恢复那么.... ...

  6. Linux中关闭SSH的DNS解析

    在操作中,我们都会用SSH协议来远程控制虚拟机,但是在输入用户名时候,会有一段时间的卡顿,此时正在进行SSH协议的DNS解析,我们为了快速的连接到虚拟机上,就要关闭这个解析过程,如下是具体配置: 1. ...

  7. SQL语句 运算符

    6.2 运算符   6.2.1 算术运算符 加 / 减 / 乘 / 除 6.2.2 连接运算符 是用来连接字符串的.跟java中的 + 是一致的. select 'abc' || ' bcd ' as ...

  8. 使用注解方式实现账户的CRUD

    1 需求和技术要求 1.1 需求 实现账户的CRUD. 1.2 技术要求 使用Spring的IOC实现对象的管理. 使用QueryRunner作为持久层的解决方案. 使用C3p0作为数据源. 2 环境 ...

  9. 循环结构——for语句、seq语句、while语句、break语句

    1.for语句: 运行结果: 2.seq命令生成整数序列: 3.while语句: 执行结果: 4.break语句: break语句是正常结束之前退出当前循环. 执行结果: 5.continue语句: ...

  10. Mac系统Pycharm永久激活

    网上找了很多Pycharm永久激活的方法,前面几步几乎都一样,最后激活的那步却总行不通,于是这边记录下 一.本人下载的是2018.2.7版本,官方有很多版本可供下载,下载地址http://www.je ...