SpringMVC:前后台传递参数

上一篇我们了解了如何从url中获取参数,通过@PathVariable以及@RequestParam标注都可以获取到。那我们今天看看其他的方式获取参数。

后台获取请求参数:

动匹配参数,如:
当想要查询信息时,通过post一个form表单后,通过下面的方式会自动匹配到form中的userName,以及email参数。

<form action="query" method="post">
    <ul>
        <li>姓名: </label><input type="text" name="userName" /></li>
        <li>电话: <input type="text" name="email" /></li>
    </ul>
    <input type="submit" value="查询" />
</form>
@RequestMapping("/query")
    public String queryUser1(String userName,String email){
        System.out.println(userName+" "+email);
        return "success";
    }

通过HttpServletRequest来获取参数

@RequestMapping("/query")
public String queryUser2(HttpServletRequest request){
    String userName= request.getParameter("userName");
    String email= request.getParameter("email");
    System.out.println(userName+" "+email);
    return "success";
}

动将请求的数据装箱,如:
我们有两个简单的类型:User,Address

public class User {
    private String userName;
    private String email;
    private Address address;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "User{userName:" + this.userName + ", email=" + this.email +"}";
    }
}
public class Address {
    private String province;
    private String city;
    public String getProvince() {
        return province;
    }
    public void setProvince(String province) {
        this.province = province;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    @Override
    public String toString() {
        return "Address{province:" + province + ", city:" + city + "}";
    }
}

我们的表单形式如下:

<form action="save" method="post">
    <ul>
        <li>姓名: <input type="text" name="userName" /></li>
        <li>邮箱: <input type="password" name="email" /></li>
        <li>省份: <input type="text" name="address.province" /></li>
        <li>城市: <input type="text" name="address.city" /></li>
    </ul>
    <input type="submit" value="提交" />
</form>

当我们提交表单时,SpringMvc会自动将表单数据装箱为我们的实体参数

@RequestMapping("/save")
public String saveUser(User user){
    System.out.println(user.toString());
    return "success";
}

那如何向前台传递参数呢?

通过ModelAndView添加数据

@RequestMapping("/info")
public ModelAndView getUser(){
    Address addr= new Address();
    addr.setCity("ShenYang");
    addr.setProvince("LiaoNing");

    User user= new User();
    user.setUserName("admin");
    user.setEmail("aa@aa.aa");
    user.setAddress(addr);

    ModelAndView modelAndView = new ModelAndView("user");
    modelAndView.addObject("user", user);
    return modelAndView;
}
<form>
    <ul>
        <li>姓名: ${requestScope.user.userName}</li>
        <li>邮箱: ${requestScope.user.email}</li>
        <li>省份: ${requestScope.user.address.province}</li>
        <li>城市: ${requestScope.user.address.city}</li>
    </ul> 
</form>

注:ModelAndView返回值类型,在方法体内我们可以通过ModelAndView对象来向请求域中添加模型数据。

使用Map参数对象

@RequestMapping("/find1")
public String findUser1(Map<String,Object> map){
    map.put("name", "admin");
    return "find";
}

注:页面取值写法 ${requestScope.name}

使用org.springframework.ui.Model参数对象

@RequestMapping("/find2")  
public String findUser2(Model model){  
    model.addAttribute("time", new Date());
    return "success";  
} 

注:页面取值写法 ${requestScope.time}

使用org.springframework.ui.ModelMap参数对象

@RequestMapping("/find3")  
public String findUser3(ModelMap modelMap){  
    modelMap.addAttribute("user", "Admin");
    modelMap.put("gender", "male");
    return "success";  
} 

注:页面取值写法 ${requestScope.user}, ${requestScope.gender}

当然,还有一些其他的传递参数方式,如Session 等,后面学习的过程当中,再继续补充。

发表评论

电子邮件地址不会被公开。 必填项已用*标注