处理请求数据
1.项目目录
2.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>SpringMVC02</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<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>
3.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.atguigu.controller"></context:component-scan>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
4.param.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>
<form action="param" method="post">
username:<input type="text" name="username" /><br>
password:<input type="text" name="password" /><br>
age:<input type="text" name="age"><br>
<!-- 测试RequestParam取消这段代码的注释 -->
<!-- province:<input type="text" name="province" /><br>
city:<input type="text" name="city" /><br>
country:<input type="text" name="country" /> -->
<!-- 使用POJO获取客户端数据 -->
province:<input type="text" name="address.province" /><br>
city:<input type="text" name="address.city" /><br>
country:<input type="text" name="address.country" />
<input type="submit" value="添加"/>
</form>
</body>
</html>
5.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>
<h1>成功</h1>
</body>
</html>
6.User.java
package com.atguigu.bean;
public class User {
private Integer id;
private String username;
private String password;
private Address address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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 Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", address=" + address + "]";
}
}
7.Address.java
package com.atguigu.bean;
public class Address {
private String province;
private String city;
private String country;
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;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "Address [province=" + province + ", city=" + city + ", country=" + country + "]";
}
}
8.ParamController.java
package com.atguigu.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.atguigu.bean.User;
@Controller
public class ParamController {
/**
* 在springMVC获取客户端传递的数据的方式
* 1、在处理请求的方法中,加入相对应的形参,保证形参参数名和传递的数据的参数名保持一致,就可以自动赋值
* @RequestParam:可以把请求参数传递给请求方法
* 当不满足赋值条件时,可以使用value属性,指定映射关系
* required:设置形参是否必须被赋值,默认为ture,必须赋值,若设置为false,则不必须赋值,
* 因此形参的值为null
* defaultValue:若形参所获得的值为null,则设置一个默认值,用在分页和模糊查询中
*/
// @RequestMapping(value="/param", method=RequestMethod.POST)
// public String testRequestParam(@RequestParam(value="name", required=false, defaultValue="admin")String username, String password, String age){
// System.out.println("username="+username+",password="+password+",age="+age);
// return "success";
// }
/**
* @RequestHeader:在处理请求的方法上,获取请求头信息,用户和@RequestParam一致
*/
// @RequestMapping(value="/testRequestHeader")
// public String testRequestHeader(@RequestHeader(value="Accept-Language")String al){
// System.out.println(al);
// return "success";
// }
/**
* @CookieValue:在处理请求的方法上,获取cookie信息,用法和RequestParam一致
*/
// @RequestMapping("/testCookieValue")
// public String testCookieValue(@CookieValue("JSESSIONID")String sessionId){
// System.out.println("testCookieValue:sessionId:"+sessionId);
// return "success";
// }
/**
* 可以使用POJO获取客户端数据,要求实体类对象中的属性名一定要和页面中表单元素的name属性值一致,且支持级联关系
*/
// @RequestMapping(value="/param", method=RequestMethod.POST)
// public String testPojo(User user){
// System.out.println(user);
// return "success";
// }
/**
* 可以通过设置形参的方式,获取servletAPI
*/
@RequestMapping("/param")
public String testServletAPI(HttpServletRequest request, HttpServletResponse response){
System.out.println("testServletAPI,"+request+","+response);
String username = request.getParameter("username");
System.out.println(username);
return "success";
}
}