Spring复选框参数

时间:2018-03-24 14:09:22

标签: java spring spring-mvc

我的复选框有问题。我有这样的jsp页面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>All Photos</title>
</head>
<body>
    <form action="/photos/deletePhotos">
    <c:forEach var="photo" items="${photoList}">
        <p><input type="checkbox" name="id" value="${photo.key}">${photo.key}</p>
        <img src="/photos/photo/${photo.key}" width="300" height="200">
        <br/><br/>
    </c:forEach>
    <input type="submit" value="Delete" />
    </form>

</body>
</html>

和这样的控制器:

@RequestMapping("/deletePhotos")
    public ModelAndView deleteSomePhotos(@PathVariable(name = "id", required = false) long[] id) {
        System.out.println(id);
        return new ModelAndView("all", "photoList", photos);
    }

问题是id == null,无论是否选中复选框。我的错误在哪里?

1 个答案:

答案 0 :(得分:0)

@PathVariable用于路径变量,例如@RuquestMapping("/deletePhotos/{id}")
使用@RequestParam代替@PathVariable。

请参阅此处:@RequestParam vs @PathVariable