Spring(七)参数的自动装配和路径变量

1.首先准备一个车类,书类和一个用户类

@Controller
@RequestMapping("/Logger")
public class UserInfoController {
    @RequestMapping("/login")
    public String doLogin(String username,String password){

        System.out.println(username);
        System.out.println(password);
        return "success";
    }

  
    //对象属性自动装配
    @RequestMapping("/loginproperty")
    public String doProperty(UserInfo userInfo,Model model){
        model.addAttribute("uname",userInfo.getUsername());
        System.out.println(userInfo.getUsername());
        System.out.println(userInfo.getPassword());

        return "success";
    }
    //域属性装配
    @RequestMapping("/loginArea")
    public String doArea(UserInfo userInfo,Model model){
        model.addAttribute("uname",userInfo.getUsername());
        System.out.println(userInfo.getUsername());
        System.out.println(userInfo.getPassword());
        System.out.println(userInfo.getCar().getBrand());
        return "success";
    }
    //集合参数装配
    @RequestMapping("/loginbook")
    public String doBook(UserInfo userInfo,Model model){
        model.addAttribute("uname",userInfo.getUsername());
        System.out.println(userInfo.getUsername());
        System.out.println(userInfo.getPassword());
        System.out.println(userInfo.getCar().getBrand());
        System.out.println(userInfo.getBooks().get(0).getBookname());
        System.out.println(userInfo.getBooks().get(1).getBookname());
        return "success";
    }
}

  在SpringMVC.xml里

在JSP上:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="/Logger/loginbook" method="post">
        用户:<input name="username"/><br/>
        密码:<input name="password"/><br/>
        车1:<input name="car.brand"/><br/>
        书1:<input name="books[0].bookname">
        书2:<input name="books[1].bookname">
        <input type="submit" value="登陆"/>

    </form>
</body>
</html>

  下面是路径变量:@PathVariable

在jsp里:

 

posted @ 2018-03-30 09:43  Gtr_Right  阅读(221)  评论(0编辑  收藏  举报