Spring请求参数绑定
param.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="param/testParam?username=hehe&password=123">请求参数绑定</a>
<form action="param/saveAccount" method="post">
姓名:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
金额:<input type="text" name="money"><br>
用户姓名:<input type="text" name="user.name"><br>
用户年龄:<input type="text" name="user.age"><br>
用户姓名:<input type="text" name="userList[0].name"><br>
用户年龄:<input type="text" name="userList[0].age"><br>
用户姓名:<input type="text" name="userList[1].name"><br>
用户年龄:<input type="text" name="userList[1].age"><br>
<%--用户姓名:<input type="text" name="userMap[0].name"><br>--%>
<%--用户年龄:<input type="text" name="userMap[0].age"><br>--%>
<%--用户姓名:<input type="text" name="userMap[1].name"><br>--%>
<%--用户年龄:<input type="text" name="userMap[1].age"><br>--%>
<input type="submit" value="提交">
</form>
<%--自定义类型转换器--%>
<form action="param/saveUser">
用户姓名:<input type="text" name="name"><br>
用户年龄:<input type="text" name="age"><br>
出生日期:<input type="text" name="date"><br>
<input type="submit" value="提交">
</form>
<a href="param/testServlet">获取Servlet原生API</a>
</body>
</html>
ParamController.java
package com.controller;
import com.domain.Account;
import com.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 请求参数绑定
*/
@Controller
@RequestMapping("/param")
public class ParamController {
@RequestMapping("/testParam")
public String testParam(String username,String password) {
System.out.println("执行了testParam()");
System.out.println("用户名:"+username+" 密码:"+password);
return "success";
}
@RequestMapping("/saveAccount")
public String saveAccount(Account account) {
System.out.println("saveAccount执行了");
System.out.println(account);
return "success";
}
@RequestMapping("/saveUser")
public String saveUser(User user) {
System.out.println(user);
return "success";
}
@RequestMapping("/testServlet")
public String testServlet(HttpServletRequest request, HttpServletResponse response) {
System.out.println(request);
HttpSession session = request.getSession();
System.out.println(session);
System.out.println(session.getServletContext());
System.out.println(response);
return "success";
}
}
解决中文乱码
web.xml
<!--配置解决中文乱码的过滤器-->
<!--过滤器要放在Servlet前面,不然会报错-->
<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>
自定义类型转换器转换日期格式
创建工具类
建立工厂类StringToDateConverter,继承Converter<String , Date>
package com.utils;
import org.springframework.core.convert.converter.Converter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 把字符串转换成日期
*/
public class StringToDateConverter implements Converter<String , Date> {
@Override
public Date convert(String s) {
if (s == null) {
throw new RuntimeException("数据为空");
}
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
return df.parse(s);
} catch (ParseException e) {
e.printStackTrace();
throw new RuntimeException("数据错误");
}
}
}
springmvc.xml中配置自定义类型转换器
<!--配置自定义类型转换器-->
<bean id="conversionServiceFactoryBean" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.utils.StringToDateConverter"></bean>
</set>
</property>
</bean>
开启自定义类型转换器
<!--开启SpringMVC框架注解的支持-->
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"></mvc:annotation-driven>