SpringMVC获取前台页面参数

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;

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值