使用POJO作为参数
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <!-- 配置org.springframework.web.filer.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 --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置 DispatcherServlet的一个初始化参数:配置SpringMvc 配置文件的位置和名称--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!-- 也可以不通过contextConfigLocation 来配置SpringMvc的配置文件,而使用默认的 默认的配置文件为:/WEB-INF/<servlet-name>-servlet.xml /WEB-INF/springDispatcherServlet-servlet.xml --> <!-- 设置启动 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <!-- 请求处理 --> <servlet-name>springDispatcherServlet</servlet-name> <!-- /:应答所有请求 --> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/tx/spring-mvc-4.0.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="como.springmvc.handlers"></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>
实体类代码:
User.java
package com.springmvc.entity; public class User { private String username; private String password; private String email; private int age; private Address address; 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } 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 + "]"; } }
Address.java
package com.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 + "]"; } }
Pojo.java
package como.springmvc.handlers;
import java.io.IOException;
import java.io.Writer;
import javax.servlet.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.springmvc.entity.User;
@Controller
@RequestMapping("/Pojo")
public class Pojo {
/*
* SpringMVC会按请求参数名和POJO属性名进行自动匹配
* 自动为该对象填充属性,支持级联属性
* 如:dept.deptId、dept.address.tel
* */
@RequestMapping("/testPojo")
public String testPojo(User user){
System.out.println("testPojo"+user);
return "success";
}
/*
* 可以使用Servlet原生的API作为目标方法的参数,具体支持一下类型
* HttpServletRequest
* HttpServletResponse
* HttpSession
* java.security.Principal
* Locale InputStream
* OutputStream
* Reader
* Writer
* */
@RequestMapping("/testServletAPI")
public void testServletAPI(HttpServletRequest request,
HttpServletResponse response,Writer out) throws IOException{
System.out.println("testServletAPI,"+request+","+response);
out.write("hello");
//return "success";
}
}
pojo.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'pojo.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action="Pojo/testPojo" method="post"><br>
username:<input type="text" name="username" /><br>
password:<input type="password" name="password" /><br>
email:<input type="text" name="email" /><br>
age:<input type="text" name="age" /><br>
<!-- 级联属性 :属性的属性 -->
city:<input type="text" name="address.city" /><br>
province:<input type="text" name="address.province" /><br>
<input type="submit" value="Value" />
</form>
<a href="Pojo/testServletAPI">Test ServletAPI</a>
</body>
</html>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isErrorPage="true"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> success page. <br> </body> </html>