多行复选框

时间:2015-03-05 09:42:29

标签: java spring jsp spring-mvc

以下是我的pojo Class

public class Person {

        private int id;
        private String name;
        private boolean check;

        public Person() {
            // TODO Auto-generated constructor stub
        }

        public Person(int id, String name,boolean check){
            this.id = id;
            this.name = name;
            this.check = check;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean isCheck() {
            return check;
        }

        public void setCheck(boolean check) {
            this.check = check;
        }
    }

这是我的formClass

public class PersonForm {

   @SuppressWarnings("unchecked")
   private List<Person> personList = LazyList.decorate(new ArrayList<Person>(), FactoryUtils.instantiateFactory(Person.class));

    public PersonForm() {
       // TODO Auto-generated constructor stub
    }

    public List<Person> getPersonList() {
       return personList;
    }

    public void setPersonList(List<Person> personList) {
       this.personList = personList;
    } 
  }

这是我的list.jsp

   <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

    <body>

    <form:form action="sent-list" method="post" modelAttribute="personForm">
        <table id="item-list">
            <thead>
                <tr>
                    <th align="justify">
                                     CheckBox
                    </th>
                    <th align="justify">id</th>
                    <th align="justify">name</th>
                </tr>
            </thead>
            <tbody>
            <c:forEach items="${personForm.personList}" var="listItem" varStatus="status">
                <tr>
                   <td style="text-align:center">
                        <form:checkbox path="personList[${status.index}].id" value="${listItem.id}"/>
                    </td> 
                    <td>${listItem.id}</td>
                    <td>${listItem.name}</td>          
                </tr>
            </c:forEach>
            </tbody>
        </table>  
         <button type="submit">POST</button>
    </form:form>
    </body>
    </html>

显示-的List.jsp

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


<body>
<form:form action="#" method="post" modelAttribute="personForm">
        <table id="item-list">
            <thead>
                <tr>
                    <th align="justify">id</th>
                    <th align="justify">name</th>
                </tr>
            </thead>
            <tbody>
            <c:forEach items="${resultList}" var="personItem" varStatus="status">
                <tr>
                    <td>${personItem.id}</td>
                    <td>${personItem.name}</td>
                </tr>
            </c:forEach>
            </tbody>
        </table>  
    </form:form>
</body>
</html>

以下是我的控制器

@Controller
public class PersonController {

    @RequestMapping(value = "/sent-list", method = RequestMethod.POST)
    public String save(@ModelAttribute("personForm") PersonForm personForm,
            Model model) {

        System.out.println(personForm);
        System.out.println(personForm.getPersonList());
        return "show-list";
    }


    @RequestMapping(value = "/")
    public String getList(ModelMap modelMap){

        Person person1 = new Person(101, "ABCD", true);
        Person person2 = new Person(102, "XYXZ", false);
        Person person3 = new Person(103, "GHI", false);
        List<Person> persons = new ArrayList<Person>();
        persons.add(person1);
        persons.add(person2);
        persons.add(person3);
        PersonForm personForm = new PersonForm();
        personForm.setPersonList(persons);
        modelMap.addAttribute(personForm);
        return "list";
    }
}

显示一个有一些价值的表格。我想要的是当检查该特定行的复选框时,该行对象的id应该传递给我的控制器。请帮忙

0 个答案:

没有答案