0056 Spring MVC如何接收浏览器传递来的请求参数--request--形参--实体类封装

浏览器总会向服务器传递一些参数,那么Spring MVC如何接收这些参数?

先写个简单的html,向服务器传递一些书籍信息,如下:

<!DOCTYPE html>
<html>
<head>
    <title>Spring MVC如何接受浏览器传递来的参数</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <form action="addbook" method="POST">
        <fieldset>
            <legend>要上传的书籍信息</legend>
             书名:<input name="bookname" /><br>
            作者:<input name="author" /><br>
            出版社:<input name="press" /><br>
            ISBN:<input name="isbn" /><br>
            豆瓣评分:<input name="douban" /><br>
                <input type="submit" value="提交" />
        </fieldset>
    </form>
</body>
</html>

再写个视图result.jsp

<%@page pageEncoding="utf-8" 
        contentType="text/html;charset=utf-8" %>
<html>
    <head>
        <title>返回结果</title>
    </head>
    <body style="color:blue;font-size:18px">
        您输入的书籍信息为:<br>
        书籍的名称:${bookname }<br><br>
        书籍的作者:${author }<br><br>
        书籍的出版社:${press }<br><br>
        书籍的ISBN:${isbn }<br><br>
        书籍的豆瓣评分:${douban }<br><br>
    </body>
</html> 

1. 通过HttpServletRequest获得请求参数和数据

tomcat类容器会自动将请求的参数封装到HttpServletRequest中

package net.sonng.mvcdemo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class BookController {
    @RequestMapping(value="/addbook",method=RequestMethod.POST)
    public ModelAndView addbook(HttpServletRequest request,HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");    //将请求的编码设为utf-8
        String bookname=request.getParameter("bookname");  //从request中或参数值
        String author=request.getParameter("author");
        String press=request.getParameter("press");
        String isbn=request.getParameter("isbn");
        float douban=Float.parseFloat(request.getParameter("douban"));
        ModelAndView mav=new ModelAndView();
        mav.addObject("bookname",bookname);
        mav.addObject("author",author);
        mav.addObject("press",press);
        mav.addObject("isbn",isbn);
        mav.addObject("douban", douban);
        mav.setViewName("result");
        return mav;
    }
}

2. 处理方法形参名==请求参数名

将处理方法的形参名设为跟传递来的参数名称相同,Spring会自动将参数值传递进来

package net.sonng.mvcdemo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class BookController {
    
    @RequestMapping(value="/addbook",method=RequestMethod.POST)
    public ModelAndView addBook(String bookname,String author,String press,String isbn,float douban){
                                //将形参名跟请求参数名设为相同,自动将值传递进来
        ModelAndView mav=new ModelAndView();
        mav.addObject("bookname",bookname);
        mav.addObject("author",author);
        mav.addObject("press",press);
        mav.addObject("isbn",isbn);
        mav.addObject("douban", douban);
        mav.setViewName("result");
        return mav;
    }
}

用这种方式可能导致编码错误,这里没有request参数,那就在web.xml中添加个过滤器

    <filter>
        <filter-name>utf8Filter</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>utf8Filter</filter-name>
       <servlet-name>springmvc</servlet-name>
    </filter-mapping>

3. 如果形参名跟请求参数名不一样怎么办呢?用@RequestParam注解

处理方法该成这样:

package net.sonng.mvcdemo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sonng.mvcdemo.entity.Book;

import org.springframework.stereotype.Controller;
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.servlet.ModelAndView;

@Controller
public class BookController {
    
    @RequestMapping(value="/addbook",method=RequestMethod.POST)
    public ModelAndView addBook(
            @RequestParam(name="bookname",required=true) String b, 
            @RequestParam(value="author") String a,
            @RequestParam("press") String p,
            @RequestParam("isbn") String i,
            @RequestParam(value="douban",defaultValue="0.0") float d){
            //用@RequestParam注解将请求参数值赋值给形参
        ModelAndView mav=new ModelAndView();
        mav.addObject("bookname",b);
        mav.addObject("author",a);
        mav.addObject("press",p);
        mav.addObject("isbn",i);
        mav.addObject("douban", d);
        mav.setViewName("result");
        return mav;
    }
}

@RequestParam可以将指定的请求参数赋值给形参,该注解有4个参数
----name:指定请求参数名
----value:跟name属性相同
----required:boolean类型,该参数是否必须绑定。注意这是指是否必须有这个参数,而不是参数的值是否为null。如果该属性设置为true,但是又没有对应的参数传递来,那么会抛出异常:Required String parameter '参数名' is not present
----defaultValue:如果没有该参数,那么使用使用默认值

4. 用实体类接收

写个实体类,其实例变量名称跟传递来的参数名称相同,并将该实体类作为处理方法的形参,Spring会自动将请求参数封装到该实体类对象中

先写个Book实体类,注意实例变量名跟传递的参数名相同

package net.sonng.mvcdemo.entity;

public class Book {
    private String bookname; //实体类的形参名跟请求参数名相同
    private String press;
    private String author;
    private String isbn;
    private float douban;
    //省略getter、setter、toString  
}

处理方法写成这样:

package net.sonng.mvcdemo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sonng.mvcdemo.entity.Book;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class BookController {
    
    @RequestMapping(value="/addbook",method=RequestMethod.POST)
                                //用实体类封装请求参数
    public ModelAndView addBook(Book book){
        ModelAndView mav=new ModelAndView();
        mav.addObject("book", book);
        mav.setViewName("result");
        return mav;
    }
}

result.jsp改一改

<%@page pageEncoding="utf-8" 
        contentType="text/html;charset=utf-8" %>
<html>
    <head>
        <title>返回结果</title>
    </head>
    <body style="color:blue;font-size:18px">
        您输入的书籍信息为:<br>
        书籍的名称:${book.bookname }<br><br>  <!-- 从名为book的模型中拿到bookname数据 -->
        书籍的作者:${book.author }<br><br>
        书籍的出版社:${book.press }<br><br>   
        书籍的ISBN:${book.isbn }<br><br>
        书籍的豆瓣评分:${book.douban }<br><br>
    </body>
</html> 

最后,如何选择?

如果参数比较少,就用:形参名跟请求参数名相同的办法
如果参数比较多,就用:实体类封装
如果浏览器端没有做参数检查,那就从request拿值

posted @ 2017-03-30 10:48  sonng  阅读(9415)  评论(0编辑  收藏  举报