1、通过原生HttpServletRequest获取前端参数
@RequestMapping("/param1")
public String param1(HttpServletRequest request) {
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username + " param1 " + password);
return "index";
}
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>
2、前端页面参数和Controller方法参数名一致/不一致的获取方式
//2、前端页面参数和Controller方法参数名一致的获取方式
@RequestMapping("/param2")
public String param2(String username, String password) {
System.out.println(username + " param2 " + password);
return "index";
}
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>
3、通过对象接收前端参数(包含对象类型参数和集合类型参数)中文乱码的配置
//2、前端页面参数和Controller方法参数名不一致的获取方式
@RequestMapping("/param3")
public String param3(@RequestParam("username1") String username
, @RequestParam("password1") String password) {
System.out.println(username + " param3 " + password);
return "index";
}
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>
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="cn.hp.ssm.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="cn.hp.ssm.util.TimeUtil"/>
</set>
</property>
</bean>
<!--mvc注解驱动,支持mvc的注解 (当前注解驱动,可以解决大多数问题,但是类型转换还需手动引入)-->
<!--<mvc:annotation-driven />-->
<!--使用全家转换器的话,mvc注解写法-->
<mvc:annotation-driven conversion-service="conversionService"/>
<!--配置支持静态页面访问 (针对 web.xml中 拦截所有路径的配置)-->
<mvc:default-servlet-handler/>
(2) 具体实现:
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date time;
② 第二种实现(数据库和实体类通过string类型记录时间)
③ 第三种实现(通过创建时间类型转换的类,然后在springmvc中配置全局日期时间转换器)
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;
DateTime parse = DateUtil.parse(s,s.length()>10? "yyyy-MM-dd HH:mm:ss":"yyyy-MM-dd");
return parse;
}