1.绑定机制
表达提交的数据都是k=v格式的
springMVC属性绑定很智能 前端提交的数据 后台会根据前端(K)name的属性值对应后端的相同的name值进行自动绑定 当绑定实体类的时候 前端name属性值,类型对应实体类的属性和类型,然后就可以自动绑定了
2 .支持的绑定数据类型
1)基本数据类型和字符串类型(区分大小写)
2)实体类型
3)集合数据类型:list、map集合
首先写一个实体类
package com.lwh.pojo;
public class User {
private int id;
private String name;
private int age;
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
然后写一个控制类
package com.lwh.controller;
import com.lwh.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ParamController {
@RequestMapping("/param")
public String testParam(String username){
System.out.println("执行l"+username);
return "success";
}
@RequestMapping("/param/form")
public String test2(String name,int age, String sex){
System.out.println(name+age+sex);
return "success";
}
@RequestMapping("/param/form2")
public String test3(User user){
System.out.println(user);
return "success";
}
}
接着写一个springmvc配置文件
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.lwh"></context:component-scan>
<!--视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--开启springMvc框架注解的支持-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
然后配置一下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>Archetype Created Web Application</display-name>
<servlet>
<!--配置前置控制器-->
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--初始化加载springmvc配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--配置拦截区-->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
加上这个filter配置可以解决前端提交数据后台控制台接受乱码问题
<!--配置字符集过滤器-->
<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>
最后在一个jsp文件中测试
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="param?username=ll">adfsa</a><%--对应非实体类的属性绑定--%>
<form action="/param/form2" >
name:<input name="name" type="text"><br> <%--name属性对应你的实体类的属性--%>
age:<input name="age" type="number"><br>
id:<input name="id" type="number">
<button>sub</button>
</form>
</body>
</html>