SpringMVC
一、获取前台页面参数
1、通过原生HttpServletRequest获取前端参数
前台 jsp:
<fieldset>
<legend>参数传递方式1--使用HTTPServletRequest</legend>
<form action="param/param1" method="get">
账号:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
<input type="submit" value="提交"><br>
</form>
</fieldset>
后台 controller:
@Controller
@RequestMapping("/param")
//1、通过原生HttpServletRequest获取前端参数
public class ParamController {
@RequestMapping("/param1")
public String param1(HttpServletRequest request){
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println(username+" "+password);
return "login";
}
2、前端页面参数和Controller方法参数名一致的获取方式
前台 jsp:
<fieldset>
<legend>参数传递方式2--方法的参数名和页面参数一致</legend>
<form action="param/param2" method="post">
账号:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
<input type="submit" value="提交"><br>
</form>
</fieldset>
后台 controller:
//2、前端页面参数和Controller方法参数名一致
@RequestMapping("/param2")
public String param2(String username,String password){
System.out.println(username+" "+password);
return "login";
}
前端页面参数和Controller方法参数名不一致的获取方式
前台 jsp:
<fieldset>
<legend>参数传递方式3--方法参数名和页面参数名称不一致</legend>
<form action="param/param3" method="post">
账号:<input type="text" name="username1"><br>
密码:<input type="text" name="password1"><br>
<input type="submit" value="提交"><br>
</form>
</fieldset>
后台 controller:
//2、前端页面参数和Controller方法参数名一致/不一致的获取方式
@RequestMapping("/param3")
public String param3(@RequestParam("username1")String username, @RequestParam("password1")String password){
System.out.println(username+" param3 "+password);
return "login";
}
3、通过对象接收前端参数(包含对象类型参数和集合类型参数)中文乱码的配置
前台 jsp:
<fieldset>
<legend>参数传递方式3--参数过多使用对象作为参数</legend>
<form action="param/param4" method="post">
账号:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
<input type="submit" value="提交"><br>
</form>
</fieldset>
后台 controller:
//3、通过对象接收前端参数(包含对象类型参数和集合类型参数)中文乱码的配置
@RequestMapping("/param4")
public String param4(User user){
System.out.println(user);
return "login";
}
4、时间类型参数的转换问题 ConversionServiceFactoryBean的配置
(1) springmvc资源文件配置mvc注解驱动和配置支持静态页面访问
<?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="com.yzx.controller"/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置全局时间转换器-->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.yzx.util.TimeUtil"/>
</set>
</property>
</bean>
<!--mvc注解驱动,支持mvc的注解 (当前注解驱动,可以解决大多数问题,但是类型转换还需手动引入)-->
<!--<mvc:annotation-driven/>-->
<!--使用全家转换器的话,mvc注解写法-->
<mvc:annotation-driven conversion-service="conversionService"/>
<!--配置支持静态页面访问 (针对 web.xml中 拦截所有路径的配置)-->
<mvc:default-servlet-handler/>
</beans>
(2) 具体实现:
① 第一种实现(实体类注解实现)
//时间类型
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date time;
② 第二种实现(数据库和实体类通过string类型记录时间)
③ 第三种实现(通过创建时间类型转换的类,然后在springmvc中配置全局日期时间转换器)
package com.yzx.util;
import cn.hutool.core.util.StrUtil;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeUtil implements Converter <String,Date>{
@Override
public Date convert(String s) {
if(StrUtil.isEmpty(s)){
throw new RuntimeException("时间类型不允许为空");
}
// else if(s.length()!=10){
// throw new RuntimeException("时间格式错误,应该是年月日的格式!");
// }
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date parse = null;
try {
parse=sdf.parse(s);
return parse;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
前台jsp:
<fieldset>
<legend>参数传递方式5--包含时间类型,需要类型转换</legend>
<form action="param/param5" method="post">
账号:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
日期:<input type="datetime-local" name="time"><br>
<input type="submit" value="提交"><br>
</form>
</fieldset>
后台 controller:
//4、时间类型参数的转换问题 ConversionServiceFactoryBean的配置
@RequestMapping("/param5")
public String param5(User user){
System.out.println(user);
return "login";
}