2013-06-06 4 views
0

등록 후 체크 박스를 바인딩 할 수 없습니다 나는 팀 등록하는 다음과 같은 일련의 단계했습니다 :심지어 사용자 정의 속성 편집기

  1. 선택 팀 -이 체크 박스 등이 팀의 플레이어의 목록이 표시됩니다에게 (JSP 페이지 하단)
  2. 표시 할 플레이어를 하나 이상 선택할 수 있습니다.
  3. newdTeam 요청 처리 방법은 위의 2 단계에서 선택한 플레이어를 설정해야합니다. 처리기가 호출되고 있지만 2 단계에서 플레이어를 선택한 경우에도 players 세트가 비어 있습니다. 문제가 어디에 있는지 알 수 없습니다.

속성 편집기가 호출되지 않습니다. 어떤 도움을 주셔서 감사합니다.

@NodeEntity 
public class Team 
{ 
    @GraphId 
    private Long nodeId; 

    @GraphProperty 
    @Indexed (unique = true) 
    private String name; 

    @Fetch 
    @RelatedTo (type = "PLAYED_WITH_TEAM", direction = Direction.INCOMING) 
    private final Set<Player> players = new HashSet<Player>(); 

    public String getName() 
    { 
     return name; 
    } 

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

    public Long getNodeId() 
    { 
     return nodeId; 
    } 

    public Collection<Player> getPlayers() 
    { 
     return players; 
    } 

    public void setPlayers(Set<Player> plyrs) 
    { 
     System.err.println("called set plrs"); 
     players.addAll(plyrs); 
    } 
} 

플레이어

@NodeEntity 
public class Player 
{ 
    @GraphId 
    private Long nodeId; 

    @Indexed (unique = true) 
    private String name; 

    @GraphProperty 
    @Indexed 
    private String firstName; 

    @GraphProperty 
    private String email; 

     //getters and setters 
} 

컨트롤러

@Controller 
@RequestMapping ("/registration") 
public class RegistrationController 
{ 
    private transient final Logger LOG = LoggerFactory.getLogger(getClass()); 

    @Autowired 
    private LeagueRepository leagueRepo; 

    @Autowired 
    private TeamRepository teamRepo; 

    @RequestMapping (method = RequestMethod.GET) 
    public String get() 
    { 
     return "/registration/start"; 
    } 

    @Transactional 
    @RequestMapping (value = "/start", method = RequestMethod.POST) 
    public String hasParticipatedEarlier(@RequestParam boolean participatedInEarlierLeague, Model model) 
    { 
     if (participatedInEarlierLeague) 
     { 
      LOG.debug("Participated in earlier leagues. Retrieving the past league teams."); 
      Iterable<League> allLeagues = leagueRepo.findAll(); 
      Set<League> sortedLeagues = new TreeSet<League>(); 
      for (League l: allLeagues) 
      { 
       sortedLeagues.add(l); 
      } 
      LOG.debug("Past leagues sorted by start date {}", sortedLeagues); 
      model.addAttribute("pastLeagues", sortedLeagues); 
     } 
     else 
     { 
      LOG.debug("Did not participate in earlier leagues. Redirecting to register the new one."); 
     } 
     return "/registration/leagues"; 
    } 

    @RequestMapping (value = "/selectTeam", method = RequestMethod.POST) 
    public String selectTeam(@RequestParam Long selectedTeam, Model model) 
    { 
     LOG.debug("Participated as team {} in previous league", selectedTeam); 
     Team team = teamRepo.findOne(selectedTeam); 
     model.addAttribute("team", team); 
     model.addAttribute("players", team.getPlayers()); 
     return "registration/players"; 
    } 

    @RequestMapping (value = "/newTeam", method = RequestMethod.POST) 
    public String newdTeam(@ModelAttribute Team team, Model model) 
    { 
     LOG.debug("Selected players from existing list {}", team.getPlayers()); 

     return "registration/registrationConfirmation"; 
    } 

    @InitBinder 
    public void initBinder(WebDataBinder binder) 
    { 
     binder.registerCustomEditor(Player.class, new PlayerPropertyEditor()); 
    } 
} 

따라서 playe 내가 볼 rPropertyEditor

public class PlayerPropertyEditor extends PropertyEditorSupport 
{ 
    @Autowired 
    PlayerRepository playerRepo; 

    @Override 
    public String getAsText() 
    { 
     System.err.println("get as txt"); 
     return ((Player) getValue()).getNodeId().toString(); 
    } 

    @Override 
    public void setAsText(String incomingId) throws IllegalArgumentException 
    { 
     System.err.println(incomingId); 
     Player player = playerRepo.findOne(Long.valueOf(incomingId)); 
     setValue(player); 
    } 
} 

JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %> 
<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form" %> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Players of ${team.name}</title> 

</head> 
<body> 
    <f:form action="newTeam" method="post" modelAttribute="team"> 
     <f:checkboxes items="${players}" path="players" itemLabel="name" itemValue="nodeId" delimiter="<br/>"/> 
     <input type="submit" value="Submit"> 
    </f:form> 
</body> 
</html> 

답변

0

한 가지 문제는 스프링 컨텍스트에서하지 않기 때문에 당신이 아무런 효과가없는, PlayerPropertyEditor 내부 PlayerRepository를 주입한다는 것입니다. 생성자를 통해 전달해야합니다. 당신이 equals를 오버라이드 (override) 할 필요가 있음을 그리고

@Autowired 
private PlayerRepository playerRepo; 

@InitBinder 
public void initBinder(WebDataBinder binder) 
{ 
    binder.registerCustomEditor(Player.class, new PlayerPropertyEditor(playerRepo)); 
} 

둘째 컨트롤러 내부의 Controller

PlayerPropertyEditor

public class PlayerPropertyEditor extends PropertyEditorSupport 
{ 
    private PlayerRepository playerRepo; 

    public PlayerPropertyEditor(PlayerRepository playerRepo) { 
     this.playerRepo = playerRepo; 
    } 
    // other methods 
} 

에 주입하고, 나는이 주요 문제가 있다는 생각입니다 Player 클래스의 경우 hashCode입니다. 나는 그것을 테스트하지 않았지만 의존하고있다. this answer