SSM应用(五)--参数绑定

参数绑定

  通过方法的输入获得参数的值叫做参数绑定;参数绑定可以分为简单参数绑定、pojo类型参数绑定、vo类型参数绑定、数组类型参数绑定以及集合类型参数绑定;

简单参数绑定

    页面:

<form action="reg.action" method="post">
    用户名:<input type="text" name="username" /><br/>
    密码:<input type="text" name="password" /><br/>
    <input type="submit" value="注册" />
</form>

  当页面传递的参数名和传递的参数名一致时,可以直接绑定输入参数:  

//文本框的名字与参数的名字一样的时候,可以直接实现参数绑定
@RequestMapping("/reg.action")
public String reg2(String username,String password) {
    System.out.println(username+","+password);
    return "result";    
}

  页面所传递的参数名和输入参数名不一致时,可以使用:@PequestParam;

//文本框与参数的名字不一致的时候:使用:@RequestParam:匹配页面所产地的参数的名字
@RequestMapping("/reg.action")
public String reg(@RequestParam(value="username")String name,String password) {
    System.out.println(name+","+password);
    return "result";    
}

pojo类型参数绑定

  pojo 类:

public class User {
    private String username;
    private String password;
}

  对象类型:参数名与pojo类的属性名一致的时候,参数值作为对象的属性值,会直接绑定为对象;

<form action="reg.action" method="post">
    用户名:<input type="text" name="username" /><br/>
    密码:<input type="text" name="password" /><br/>
    <input type="submit" value="注册" />
</form>

  controller层:

@RequestMapping("/reg.action")
public String reg(User user) {
    System.out.println(user);
    return "result";
}

vo类参数绑定

  vo类:

public class UserVO {
    private int id;
    private User user;
}

  在参数传递页面,可以使用:对象.对象属性名 做匹配

<form action="reg6.action" method="post">
    <!-- id参数名和vo的id属性名一致的时候,可以直接绑定到vo对象 -->
    id:<input type="text" name="id" /><br/>
    <!--vo的另外一个属性时user,参数名:类.属性名 -->
    用户名:<input type="text" name="user.username" /><br/>
    密码:<input type="text" name="usser.password" /><br/>
    <input type="submit" value="注册" />
</form>

  controller层:

@RequestMapping("/reg.action")
public String reg(UserVO userVO) {
    System.out.println(userVO);
    return "result";
}

数组类型参数的绑定

  和基本参数类型的绑定类似,可以直接通过数组名进行绑定;

    页面:

<form action="reg.action" method="post">
    用户名:<input type="text" name="username" /><br/>
    密码:<input type="text" name="password" /><br/>
    爱好:
    <input type="checkbox" name="intrests" value="1">踢球
    <input type="checkbox" name="intrests" value="2">阅读
    <input type="checkbox" name="intrests" value="3">编程
    <br/>
    <input type="submit" value="注册" />
</form>

    controller层:

@RequestMapping("/reg.action")
public String reg(Integer[] intrests,User user) {
    System.out.println(intrests.length+","+user);
    return "result";
}

List类型参数绑定

  和VO对象参数绑定类似,在页面中使用 list[i].属性名 进行绑定;

    页面:

<form action="reg.action" method="post">
    <!-- id参数名和vo的id属性名一致的时候,可以直接绑定到vo对象 -->
    id:<input type="text" name="id" /><br/>
    用户名:<input type="text" name="userlist[0].username" /><br/>
    密码:<input type="text" name="userlist[1].password" /><br/>
    <input type="submit" value="注册" />
</form>

    vo类:

public class UserVO {
    private int id;
    private List<User> userlist;
}

    controller层:

@RequestMapping("/reg.action")
public String reg(UserVO userVO) {
    System.out.println(userVO.getUserlist());
    return "result";
}

Map类型参数绑定

  在页面中利用键值对的方式绑定参数;

    页面:

<form action="reg.action" method="post">
    <!-- id参数名和vo的id属性名一致的时候,可以直接绑定到vo对象 -->
    id:<input type="text" name="id" /><br/>

    用户名:<input type="text" name="userMap['user1'].username" /><br/>
    密码:<input type="text" name="userMap['user1'].password" /><br/>

    用户名:<input type="text" name="userMap['user2'].username" /><br/>
    密码:<input type="text" name="userMap['user2'].password" /><br/>
    <input type="submit" value="注册" />
</form>

    vo类:

public class UserVO {
    private int id;
    private Map<String,User> userMap;
}

    controller层:

@RequestMapping("/reg8.action")
public String reg8(UserVO userVO) {
    System.out.println(userVO.getUserMap());
    return "result";
}

以上就是参数绑定方法的一些介绍了;

PS:因作者能力有限,如有误还请谅解;

猜你喜欢

转载自www.cnblogs.com/WHL5/p/9001695.html