Road SSM learning --springMVC first day _ @ requestMapping and request parameter binding

@requestMapping

1, @requestMappingmay act on the class, this class represents a sub-path is the path in the
benefits: This class is the operation such as the user, to / user acting on the class, the following is the corresponding / add, / update etc. , easy to manage and clear, clear.
Here Insert Picture Description
2, method attribute: Specify what method is used to request
Here Insert Picture Description
a hyperlink is to use GET requests , if specified here with a POST request to perform this method, then click the hyperlink, although the request mapping is no problem, but the requested method is not legitimate It is not the last jump
Here Insert Picture Description

Here Insert Picture Description

. 3, the params (with less)
conditions for the request parameter specified limits. It supports simple expression. Required request parameters and the key and value must be exactly the same configuration.
For example:
Here Insert Picture Description
When sending a request, you must name the parameters to be passed, otherwise the method is not performed
if the value is inside the params name = hehe , if the parameter is passed over name = xixi , it does not work, must be exactly the same in

. 4, headers (with less)
condition for specifying a restriction request message header, if there is a specific value, must be exactly the same
Here Insert Picture Description
representation in the request header, this property must Accept, or Mo was accessed.

Second, the parameter binding

1、简单绑定
springMVC会根据传递过来的参数,和方法的形参对应的接收并绑定
表单中请求参数都是基于 key=value 的。SpringMVC 绑定请求参数的过程是通过把表单提交请求参数,作为控制器中方法参数进行绑定的。
比如:
Here Insert Picture Description
Here Insert Picture Description
其中,形参和传递过来的参数必须一一对应,一模一样(顺序可以不一样),如果方法中,将形参 String usernameInteger age调换位置,改passwordpass,再打印,发现username和age可以正常打印(说明调换位置不影响参数绑定),但是pass变成了null,说明无法绑定

2、实体类绑定
前置:
(1)配置编码转换过滤器
get请求输入中文是不会乱码的,post请求输入中文就会乱码,所以我们要对每个请求都设置UTF-8编码。在以前的servlet中,我们是在每个servlet开始都写上request.setcharacterencoding,现在不用了,科技进步了,直接在web.xml里面配置filter就完事了

<!--配置编码转换过滤器-->
  <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)编写自定义类型转换器(此处为String转Date)
由于默认的String转Date只能是yyyy/mm/dd形式的,如果输入yyyy-mm-dd就会出现异常,我们现在想变成yyyy-mm-dd,就得自己自定义一个类,注册到springmvc的转换器中
Here Insert Picture Description

package com.ssn.utils;

import org.springframework.core.convert.converter.Converter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//实现Converter接口,接收类型为String,返回类型为Converter
public class StringToDate implements Converter<String, Date> {
    
    @Override
    public Date convert(String s) {
        if (s == null) throw new RuntimeException("没有输入");
        DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
        try {
            Date result = df.parse(s);
            return result;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

springmvc.xml中进行注册
Here Insert Picture Description
这样一来,我们原本的yyyy/mm/dd格式就失效,变为yyyy-mm-dd格式了。

To test various types of data binding, we wrote a User Account and two entity classes, and references User Account object class, and a List and a Map, and generate respective get and set methods and toString

Here Insert Picture Description
Here Insert Picture Description

Writing a new class, used to test whether the parameter binding success
is worth noting here is that we can write directly on the Account class parameter indicating parameters are taken over to a package inside the Account class, then the required data directly through tostrng call the object's method can print out all the information. (Otherwise have to write a number of parameter ah, trouble is dead, so it is convenient)
Here Insert Picture Description
index.jsp, by submission form
because the binding that account type, so to get the username by the user object, and then bind the objects inside account name user entity classes (pojo wording)
noted that the wording of the list and map, map filled it with a key, the key that is bound to be of value
Here Insert Picture Description

Say no good, run a try!
Here Insert Picture Description
Here Insert Picture Description
Fill in the date format, the success of a custom conversion
list and map successfully bound

Published 31 original articles · won praise 0 · Views 1219

Guess you like

Origin blog.csdn.net/SixthMagnitude/article/details/104253906