Home  >  Article  >  Java  >  SpringMVC Learning Series (4) Data Binding-1

SpringMVC Learning Series (4) Data Binding-1

黄舟
黄舟Original
2017-03-03 10:52:081476browse

In series (SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1) we introduced how a request is mapped to an action. The next step is of course how to obtain the data in the request, which leads to the content of this article - data binding.

First take a look at the annotations for binding data:

SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1.@RequestParam, binds a single request data, which can be the data in the URL, the data submitted by the form or the uploaded file ;
SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1.@PathVariable, bind URL template variable value;
SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1.@CookieValue, bind cookie data;
SpringMVC Learning Series (4) Data Binding-1.@RequestHeader, bind request header data;
5.@ModelAttribute , bind data to Model;
6.@SessionAttributes, bind data to Session;
7.@RequestBody, used to process content whose Content-Type is not application/x-www-form-urlencoded, for example application/json, application/xml, etc.;
8.@RequestPart, binds "multipart/data" data, and can enter object conversion according to the data type;

Let's see how to use it:

SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1.@RequestParam:

In order to verify file binding we need to do the following first:

a. Put commons-fileupload-SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1.SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1.SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1.jar and commons-io- SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1.SpringMVC Learning Series (4) Data Binding-1.jar Two jar packages were added to our project.

b. Configure the springservlet-config.xml file in our project to support file upload. The content is as follows:


<!-- 支持上传文件 -->  <bean>  
    <!-- 设置上传文件的最大尺寸为SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1MB -->  
    <property>  
        <value>SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-10SpringMVC Learning Series (4) Data Binding-18576</value>  
    </property>
    <property> 
        <value>UTF-8</value> 
    </property></bean>


maxUploadSize is used to limit the maximum size of uploaded files, or it can be left unset, which means there is no limit to the size of uploaded files. defaultEncoding is used to set the encoding format of uploaded files and is used to solve the problem of garbled Chinese names of uploaded files.

Let’s see how to use it:

Add a DataBindController, which has two paramBind actions corresponding to get and post requests respectively:

package com.demo.web.controllers;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.web.bind.ServletRequestUtils;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.multipart.MultipartHttpServletRequest;import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/databind")public class DataBindController {

    @RequestMapping(value="/parambind", method = {RequestMethod.GET})    public ModelAndView paramBind(){
        
        ModelAndView modelAndView = new ModelAndView();  
        modelAndView.setViewName("parambind");  
        return modelAndView;
    }
    
    @RequestMapping(value="/parambind", method = {RequestMethod.POST})    public ModelAndView paramBind(HttpServletRequest request, @RequestParam("urlParam") String urlParam, @RequestParam("formParam") String formParam, @RequestParam("formFile") MultipartFile formFile){        
        //如果不用注解自动绑定,我们还可以像下面一样手动获取数据
         String urlParamSpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1 = ServletRequestUtils.getStringParameter(request, "urlParam", null);
        String formParamSpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1 = ServletRequestUtils.getStringParameter(request, "formParam", null);
        MultipartFile formFileSpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1 = ((MultipartHttpServletRequest) request).getFile("formFile"); 
        
        ModelAndView modelAndView = new ModelAndView();  
        modelAndView.addObject("urlParam", urlParam);  
        modelAndView.addObject("formParam", formParam);  
        modelAndView.addObject("formFileName", formFile.getOriginalFilename());  
        
        modelAndView.addObject("urlParamSpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1", urlParamSpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1);  
        modelAndView.addObject("formParamSpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1", formParamSpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1);  
        modelAndView.addObject("formFileNameSpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1", formFileSpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1.getOriginalFilename());  
        modelAndView.setViewName("parambindresult");  
        return modelAndView;
    }
        
}


Add two views, parabind.jsp and parabindresult.jsp, in the views folder. The contents are as follows:


##

nbsp;html PUBLIC "-//WSpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1C//DTD HTML SpringMVC Learning Series (4) Data Binding-1.0SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1 Transitional//EN" "http://www.wSpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1.org/TR/htmlSpringMVC Learning Series (4) Data Binding-1/loose.dtd"><meta><title>Insert title here</title>
    
          
          
             
  


nbsp;html PUBLIC "-//WSpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1C//DTD HTML SpringMVC Learning Series (4) Data Binding-1.0SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1 Transitional//EN" "http://www.wSpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1.org/TR/htmlSpringMVC Learning Series (4) Data Binding-1/loose.dtd"><meta><title>Insert title here</title>
    自动绑定数据:

    ${urlParam}
    ${formParam}
    ${formFileName}



    手动获取数据:

    ${urlParamSpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1}
    ${formParamSpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1}
    ${formFileNameSpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1}


Run the project, enter the content, and select the upload file:

SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1

Submit to view the results:

SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1

You can see that the bound data has been obtained.

Above we demonstrated how to bind data to a single variable, but in actual applications we usually need to obtain the model object. Don’t worry, we don’t need to bind data to individual variables and then compare them. When assigning a value to a model, you only need to add the model to the corresponding action parameters (there is no need to specify annotations for binding data here). Spring MVC will automatically convert the data and bind it to the model object. Everything is as simple as that. The test is as follows:

添加一个AccountModel类作为测试的model:


package com.demo.web.models;public class AccountModel {    
    private String username;    private String password;    
    public void setUsername(String username){        this.username=username;
    }    public void setPassword(String password){        this.password=password;
    }    
    public String getUsername(){        return this.username;
    }    public String getPassword(){        return this.password;
    }
}

在DataBindController里面添加SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1个modelAutoBind的action分别对应get和post请求:


@RequestMapping(value="/modelautobind", method = {RequestMethod.GET})public String modelAutoBind(HttpServletRequest request, Model model){
    
    model.addAttribute("accountmodel", new AccountModel());    return "modelautobind";
}

@RequestMapping(value="/modelautobind", method = {RequestMethod.POST})public String modelAutoBind(HttpServletRequest request, Model model, AccountModel accountModel){
    
    model.addAttribute("accountmodel", accountModel);    return "modelautobindresult";
}


在views文件夹中添加modelautobind.jsp和modelautobindresult.jsp SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1个视图用于提交数据和展示提交的数据:

modelautobind.jsp:

nbsp;html PUBLIC "-//WSpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1C//DTD HTML SpringMVC Learning Series (4) Data Binding-1.0SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1 Transitional//EN" "http://www.wSpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1.org/TR/htmlSpringMVC Learning Series (4) Data Binding-1/loose.dtd"><meta><title>Insert title here</title>
    
              用户名:
        密 码:
             
  

modelautobindresult.jsp :

nbsp;html PUBLIC "-//WSpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1C//DTD HTML SpringMVC Learning Series (4) Data Binding-1.0SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1 Transitional//EN" "http://www.wSpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1.org/TR/htmlSpringMVC Learning Series (4) Data Binding-1/loose.dtd"><meta><title>Insert title here</title>
    用户名:${accountmodel.username}
    密 码:${accountmodel.password}


运行测试:

SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1

用户名 输入AAA 密码 输入BBB,提交:

SpringMVC Learning Series (4) Data Binding-1

可以看到结果显示正确,说明自动绑定成功。

注:

SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1.关于@RequestParam的参数,这是一个@RequestParam的完整写法@RequestParam(value="username", required=true, defaultValue="AAA")。

value表示要绑定请求中参数的名字;

required表示请求中是否必须有这个参数,默认为true这是如果请求中没有要绑定的参数则返回SpringMVC Learning Series (4) Data Binding-10SpringMVC Learning Series (4) Data Binding-1;

defaultValue表示如果请求中指定的参数值为空时的默认值;

要绑定的参数如果是值类型必须要有值否则抛异常,如果是引用类型则默认为null(Boolean除外,默认为false);

 

SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1.在刚才添加的SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1个action中可以看到返回类型和以前的不一样了由ModelAndView变成了String,这是由于Spring MVC 提供Model、ModelMap、Map让我们可以直接添加渲染视图需要的模型数据,在返回时直接指定对应视图名称就可以了。同时Map是继承于ModelMap的,而Model和ModelMap是继承于ExtendedModelMap的。

 

SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1. In the view modelautobind.jsp you just added, you can see form:formform:input and other tags, these are the form tags provided by Spring MVC. With the help of these tags, we can easily bind the model data to the form (of course you can also choose to continue to use the native HTML form tags). To use Spring MVC just needs to add a reference to the view @ taglib prefix="form" uri="http://www.php.cn/" %>That’s it, about Spring The specific content of MVC form tags will be introduced in future articles.

The above is the content of Data Binding-SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1) Data Binding-1 of SpringMVC Learning Series (SpringMVC Learning Series (4) Data Binding-1). For more related content, please pay attention to the PHP Chinese website (www. php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn