1、配置文件applicationContext.xml(自动生成) (约束需要自己配置)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<!-- 开启注解扫描 -->
<context:component-scan base-package="controller"/>
<!-- 视图解析器对象 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--视图解析器 -->
<!--mvc框架支持 -->
<mvc:annotation-driven/>
</beans>
2、jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>hello</title>
</head>
<body>
<h1>绑定参数</h1>
<%-- <a href="param/test?username=王优秀&password=110">进入params</a>--%>
<!-- name后面跟bean属性名一致 绑定参数传递类-->
<form action="param2/test2" method="post">
姓名:<input type="text" name="name"><br>
密码:<input type="password" name="password"><br>
金额:<input type="text" name="money"><br>
用户名:<input type="text" name="user.username"><br>
用户年龄:<input type="text" name="age"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
3、controller类1是传递普通参数,2是传递类(包含嵌套类)
1、
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/param")
public class ParamController {
@RequestMapping("/test")
public String params(String username,String password) {
System.out.println("执行成功了!");
System.out.println("用户名:"+username);
System.out.println("密码:"+password);
return "success";
}
}
2、
package controller;
import beans.Info;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/param2")
public class ParamController2 {
@RequestMapping("/test2")
public String save(Info info){
System.out.println(info.getName());
System.out.println(info.getPassword());
System.out.println(info.getMoney());
System.out.println(info.toString());
return "success";
}
}
4、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置前端控制器 --!>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置解决中文乱码过滤器 --!>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
5、实体类
1、info
package beans;
import java.io.Serializable;
public class Info implements Serializable {
private String name;
private String password;
private String money;
private User user;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMoney() {
return money;
}
public void setMoney(String money) {
this.money = money;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String toString() {
return "Info{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
", money='" + money + '\'' +
", user=" + user +
'}';
}
}
2、user
package beans;
import java.io.Serializable;
public class User implements Serializable {
private String username;
private Integer age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", age=" + age +
'}';
}
}