2013-06-06 101 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()); 
    } 
} 

FLASH播放器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

一個問題,我看到的是,你注入PlayerRepositoryPlayerPropertyEditor,它沒有影響,因爲它不是一個Spring上下文。你應該通過構造函數傳遞它。然後注入其在Controller

PlayerPropertyEditor

public class PlayerPropertyEditor extends PropertyEditorSupport 
{ 
    private PlayerRepository playerRepo; 

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

控制器

@Autowired 
private PlayerRepository playerRepo; 

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

其次,我想這是主要的問題,是你應該重寫equalshashCode爲您的Player類。我還沒有測試過,但我依靠this answer

相關問題