1.@PathVariable 定义在方法上获取请求url路径上的参数数据
1.创建项目完善项目结构
2.导入依赖
<!-- 配置开发SpringMVC所以来的jar包 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
3.配置SpringMVC配置文件applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!--开启自动扫描包 -->
<context:component-scan base-package="com.wangxing.springmvc.controller"></context:component-scan>
</beans>
4.配置web.xml文件
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 配置中央处理器DispatcherServlet-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
5.创建Controller控制层
package com.wangxing.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
//本类的访问地址
@RequestMapping("/test")
public class TestController {
/**
* @PathVariable 定义在方法参数中获取请求url路径上的参数数据
* http://localhost:8080/demo3/test/testPathVariable/zhangsan/123456/23/xian
* web.xml文件的中央控制的<url-pattern></url-pattern>
*/
//http://localhost:8080/PathVariable_war/test/testPathVariable/zhangsan/1234/23/xian.do
@RequestMapping("/testPathVariable/{username}/{password}/{age}/{address}.do")
public void testPathVariable(@PathVariable("username")String name,
@PathVariable("password")String pass,
@PathVariable("age")String age,
@PathVariable("address")String address){
System.out.println("name=="+name);
System.out.println("pass = " + pass);
System.out.println("age=="+age);
System.out.println("address=="+address);
}
}
6.测试结果http://localhost:8080/PathVariable_war/test/testPathVariable/zhangsan/321/11/xian.do
2.@RequestParam 定义在方法上,获取请求中通过key=value方式传递的参数数据
例如:
package com.wangxing.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/hello")
public class HelloController {
/**
* @RequestParam 定义在方法上,获取请求中 通过key=value方式传递的参数数据
* 无论是get请求/post请求都可以使用
*/
@RequestMapping("/testRequestPram.do")
public void testRequestParam(@RequestParam("username")String name,
@RequestParam("password")String pass,
@RequestParam("age")String age,
@RequestParam("address")String address){
System.out.println("name=="+name);
System.out.println("pass=="+pass);
System.out.println("age=="+age);
System.out.println("address=="+address);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="hello/testRequestPram.do" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
年龄:<input type="text" name="age"><br>
地址:<input type="text" name="address"><br>
<input type="submit" value="提交"><br>
</form>
</body>
</html>
测试
http://localhost:8080/Requestparam_war/test1.html
3.HttpServletRequest对象的getParameter()方法接收数据
例如:
package com.wangxing.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/dox")
public class DoxController {
/**
* HttpServletRequest对象的getParameter()方法接收数据
* @param req
*/
@RequestMapping("/testHttpServletReq.do")
public void testHttpServletReq(HttpServletRequest req){
System.out.println("username=="+req.getParameter("username"));
System.out.println("password=="+req.getParameter("password"));
System.out.println("age=="+req.getParameter("age"));
System.out.println("address=="+req.getParameter("address"));
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="dox/testHttpServletReq.do" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
年龄:<input type="text" name="age"><br>
地址:<input type="text" name="address"><br>
<input type="submit" value="提交"><br>
</form>
</body>
</html>
测试
http://localhost:8080/HttpServletRequest_war/test1.html
4.在请求处理方法中定义对应参数变量,参数变量的名称与页面元素的name属性值相同
例如:
package com.wangxing.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class testController {
@RequestMapping("/testName.do")
public void testName(String username, String password, String age, String address){
System.out.println("username=="+username);
System.out.println("password=="+password);
System.out.println("age=="+age);
System.out.println("address=="+address);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="test/testName.do" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
年龄:<input type="text" name="age"><br>
地址:<input type="text" name="address"><br>
<input type="submit" value="提交"><br>
</form>
</body>
</html>
测试
http://localhost:8080/ByName_war/test/testName.do
5.将需要提交的请求参数值封装到java对象中【java对象的成员变量的名称一定要与页面元素的name属性值相同】
例如:
package com.wangxing.springmvc.bean;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
public class UserBean {
private String username;
private String password;
private String age;
private String address;
@DateTimeFormat(pattern = "yyy-MM-dd")
private Date day;
public Date getDay() {
return day;
}
public void setDay(Date day) {
this.day = day;
}
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 getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package com.wangxing.springmvc.controller;
import com.wangxing.springmvc.bean.UserBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.text.SimpleDateFormat;
@Controller
@RequestMapping("/user")
public class testUserBean {
@RequestMapping("/testUserBean.do")
public void testUserBean(UserBean userbean){
System.out.println("username=="+userbean.getUsername());
System.out.println("password=="+userbean.getPassword());
System.out.println("age=="+userbean.getAge());
System.out.println("address=="+userbean.getAddress());
System.out.println("day=="+new SimpleDateFormat().format(userbean.getDay()));
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="user/testUserBean.do" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
年龄:<input type="text" name="age"><br>
地址:<input type="text" name="address"><br>
日期:<input type="text" name="day"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
测试