Hello.
I got idea in example with the same name. Only difference is that I don`t have static List<Contact> like there.
This is basic:
Database have table Person with information about some people. I have entity class Person with all fields and one Bollean forGrouping field more. My class od List<Person> is PersonList.
Person.java
PersonList.javaCode:package net.jdj.coffee.entity; import java.io.Serializable ; public class Person implements Serializable { private static final long serialVersionUID = 200620131705L; private int id; private String name; … // Getter and Setter methods… }
PersonGroupingControllerCode:package net.jdj.coffee.entity; import java.io.Serializable; import java.util.List; import net.jdj.coffee.entity.Person; public class PersonList implements Serializable { private static final long serialVersionUID = 200620131711L; private List<Person> persons; public List<Person> getPersons() { return persons; } public void setPersons(List<Person> persons) { this.persons = persons; } }
personGrouping.jspCode:package net.jdj.coffee.web; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.bind.annotation.*; import net.jdj.coffee.entity.Person; import net.jdj.coffee.entity.PersonList; import net.jdj.coffee.session.UserSession; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Set; import java.util.ArrayList; @Controller @SessionAttributes({"userSession"}) @RequestMapping("/personGrouping.htm") public class PersonGroupingController { protected final Log logger = LogFactory.getLog(getClass()); private static List<Person> persons = new ArrayList<Person>(); @Autowired private PersonGroupingService personGroupingService; public void setpersonGroupingService(PersonGroupingService personGroupingService) { this.personGroupingService = personGroupingService; } @RequestMapping(method=RequestMethod.GET) public String showForm(ModelMap model, @ModelAttribute UserSession userSession, @ModelAttribute(value="conditionCity") ConditionCity conditionCity) { Date now = new java.util.Date(); SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); String formatNow = sdf.format(now); model.addAttribute("now", formatNow); String condition = this.personGroupingService.myConditionCity(conditionCity); PersonList personList = new PersonList(); List<Persons> tempList = this.personGroupingService.allPersons(condition); personList.setPersons(tempList); model.addAttribute("personList", personList); return "personGrouping"; } @RequestMapping(params="condition=true", method=RequestMethod.POST) public ModelAndView openPersons(@ModelAttribute(value="personList") PersonList personList, @ModelAttribute(value="conditionCity") ConditionCity conditionCity, BindingResult result, @ModelAttribute UserSession userSession) { ModelAndView model = new ModelAndView("personGrouping"); String condition = this.personGroupingService.myConditionCity(conditionCity); List<Persons> tempList = this.personGroupingService.allPersons(condition); personList.setPersons(tempList); model.addObject("personList", personList); return model; } @RequestMapping(params="group=true", method=RequestMethod.POST) public String group(@ModelAttribute(value="personList") PersonList personList, BindingResult result, @ModelAttribute UserSession userSession) { String redir = "wrongGrouping.htm"; List<Person> persons = personList.getPersons(); if (null != persons && persons.size() > 0) { this.persons = persons; for (Person person : persons) { //add person to special group ... redir = "okGrouping.htm" } return "redirect:" + redir; } }
When I choose city I got ok data but when I check some records in table and click button Group my persons is empty. Problem is inHTML Code:<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page session="false"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head>...</head> <body> <!-- Main Page Container --> <div class="page-container"> <!-- B.1 MAIN CONTENT --> <div class="main-content"> <!-- Content unit - One column --> <div class="column1-unit"> ... <p class="center">Choose city:</p> <form:form commandName="conditionCity" action="personGrouping.htm?condition=true" name="conditionForm"> <fieldset> <table> <tbody> <tr> <td><form:input type="checkbox" name="cbBG" id="cbBG" path="cbBG" value="cbBG" /></td> <p><label for="cbBG">Beograd</label> <td><form:input type="checkbox" name="cbNS" id="cbNS" path="cbNS" value="cbNS" /></td> <p><label for="cbNS">Novi Sad</label> </tr> </tbody> </table><p><input type="submit" value="Open" /></p> </fieldset> </form:form> ... <form:form commandName="personList" action="personGrouping.htm?group=true" name="groupForm"> <fieldset> <label>Check person for special group</label> <c:if test="${!empty personList}"> <br /> <center> <div class="siva"> <table width="90%"> <thead> <tr> <th width="15px" style="display: none"></th> <th width="15px">?</th> <th width="40px">name</th> ... </tr> </thead> <tbody> <c:forEach items="${personList.persons}" var="per" varStatus="status"> <tr class="belo" id="${per.id}" > <td style="display: none"><c:out value="${per.id}"/></td> <td><c:out value="${stavkaPlana.kljucOrgjedNaziv}"/></td> <td><c:out value="${stavkaPlana.kontoSifra}"/></td> </tr> </c:forEach> </tbody> </table> </div> </center> <br /> </c:if><p><input type="submit" value="Group" /></p> </fieldset> </form:form> ... </div> <hr class="clear-contentunit" /> </div> </div> ... </body> </html>
List<Person> persons = personList.getPersons();
, personList is not null but persons are. Why? What is wronge with my getPerson? How to take personList from page? Or, how to fil persons with all persons shown in table?
Today I post one question (How to read checkboxes from table in form (jsp)?) but I was wrong. Later I found a similar example on net. But something I do wrong.
Please help. Sorry for my English.
Thanks, Ela


Reply With Quote