SPRINGMVC (two): springMVC Request Parameter Description

A request type parameter binding

1, the basic types of binding request parameters

1.1, visit the page to add

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

1.2, added at the processor

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

1.3, the console output

 

2, the request parameter binding entity type

2.1, create entity classes

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, visit the page to add

        <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, added at the processor

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

2.4, the console output

3, a request type parameter set to bind

3.1, created entity

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, visit the page to add

        <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, added at the processor

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

3.4, the console output

Second, the request parameter binding explain some of the problems

1, Chinese garbage problem

The reason: When the form is submitted, the browser will Chinese parameter values ​​are encoded. Server section will default to decode iso-8859-1, if the form is inconsistent encoding and servers, will be garbled.

Solution: Add Chinese garbled filter in web.xml configuration file

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

When the request data transmitted from the browser to the server, and sometimes they are inconsistent with the browser type as the server receives the transmission, may cause type conversion, such as time, sent to the server when the browser format is a string, the server when receiving the data needed is the date type. At this time, we can customize a type converter:

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("数据类型转换出现错误");
        }
    }
}

Then configure custom type converter in 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>

 

Published 134 original articles · won praise 10 · views 7345

Guess you like

Origin blog.csdn.net/yu1755128147/article/details/103898654