SpringMVC(二):springMVC请求参数详解

一、请求参数绑定的类型

1、请求参数绑定基本类型

1.1、在页面添加访问

        <h3>2、基本类型数据绑定</h3>
        <a href="/request/testBasicTypes?username=zhangsan&password=123">基本类型数据绑定</a><br/>

1.2、在处理器添加处理

    /**
     * 请求参数绑定:基本类型数据绑定
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/testBasicTypes")
    public String testBasicTypes(String username,String password) {
        System.out.println("用户名:" + username + ",密码:" + password);
        return "success";
    }

1.3、控制台输出结果

 

2、请求参数绑定实体类型

2.1、创建实体类

package com.wedu.springmvc.domain;

import java.util.Date;

public class Userinfo {

    private String username;
    private String password;
    private Date date;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public String toString() {
        return "Userinfo{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", date=" + date +
                '}';
    }
}

2.2、在页面添加访问

        <h3>2、JavaBean数据绑定</h3>
        <form action="/request/testJavaBean" method="post">
            用户名:<input type="text" name="username"/>
            密  码:<input type="text" name="password"/>
            <input type="submit" value="提交"/>
        </form>

2.3、在处理器添加处理

    /**
     * 请求参数绑定:JavaBean数据绑定
     * @param userinfo
     * @return
     */
    @RequestMapping("/testJavaBean")
    public String testJavaBean(Userinfo userinfo) {
        userinfo.setDate(new Date());
        System.out.println(userinfo);
        return "success";
    }

2.4、控制台输出结果

3、请求参数绑定集合类型

3.1、创建实体

package com.wedu.springmvc.domain;

import java.util.List;
import java.util.Map;

public class Account {

    private List<Userinfo> list;
    private Map<String,Userinfo> map;

    public List<Userinfo> getList() {
        return list;
    }

    public void setList(List<Userinfo> list) {
        this.list = list;
    }

    public Map<String, Userinfo> getMap() {
        return map;
    }

    public void setMap(Map<String, Userinfo> map) {
        this.map = map;
    }

    @Override
    public String toString() {
        return "Account{" +
                "list=" + list +
                ", map=" + map +
                '}';
    }
}

3.2、在页面添加访问

        <h3>3、集合类型数据绑定</h3>
        <form action="/request/testConllection" method="post">
            用户名:<input type="text" name="list[0].username"/>
            密  码:<input type="text" name="list[0].password"/><br/>

            用户名:<input type="text" name="map['test'].username"/>
            密  码:<input type="text" name="map['test'].password"/>
            <input type="submit" value="提交"/>
        </form>

3.3、在处理器添加处理

    /**
     * 请求参数绑定:集合类型数据绑定
     * @param account
     * @return
     */
    @RequestMapping("/testConllection")
    public String testConllection(Account account) {
        System.out.println(account);
        return "success";
    }

3.4、控制台输出结果

二、请求参数绑定一些问题详解

1、中文乱码问题

原因:在表单提交时,浏览器会对中文参数值进行编码。服务器段默认会使用iso-8859-1来解码,如果表单的编码方式和服务器的不一致,就会产生乱码。

解决方式:在web.xml的配置文件中添加中文乱码过滤器

  <!--配置解决中文乱码的过滤器-->
  <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>

2、类型转换问题

在浏览器发送的请求数据到服务器时,有时因为服务器接收的类型与浏览器发送的类型不一致,可能会导致类型转换问题,如时间,在浏览器发送到服务器时是字符串的格式,服务器中接收数据的时候需要的是日期类型的。这时,我们可以自定义一个类型转换器:

package com.wedu.springmvc.utils;

import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.Nullable;
import sun.security.krb5.internal.PAData;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 字符串转换为日期类型工具类
 */
public class StringToDateConverter implements Converter<String,Date> {

    @Nullable
    @Override
    public Date convert(String source) {
        String pattern = null;
        if(source == null) {
            throw new RuntimeException("请您传入数据");
        } else if (source.contains(".")){
            pattern = "yyyy.MM.dd";
        }else if (source.contains("/")){
            pattern = "yyyy/MM/dd";
        }else if (source.contains("-")) {
            pattern = "yyyy-MM-dd";
        } else {
            throw new RuntimeException("您传入的数据格式不对,请重新输入");
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(source);
        } catch (ParseException e) {
            throw new RuntimeException("数据类型转换出现错误");
        }
    }
}

然后在springmvc.xml中配置自定义类型转换器

    <!-- 配置自定义类型转换器 -->
    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="com.wedu.springmvc.utils.StringToDateConverter"/>
            </set>
        </property>
    </bean>
发布了134 篇原创文章 · 获赞 10 · 访问量 7345

猜你喜欢

转载自blog.csdn.net/yu1755128147/article/details/103898654