2013-05-06 21 views
0

在我的頁面上我只想得到一個用戶的詳細信息。問題是我在顯示頁面上用戶的詳細信息時遇到問題。我試圖檢索的對象與另一個類具有一對一的關係。所以我想列出關聯的對象。檢索一個對象的春天

型號

@Entity 
@Table(name = "user") 
@Component 
public class UserEntity implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "user_id") 
    private Integer userId; 

     @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="setter") 
     private Set<Module> sModule = new HashSet<Module>(); 

     @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="checker") 
     private Set<Module> cModule = new HashSet<Module>(); 

控制器

 @RequestMapping(value = "/main/user/testing", method = RequestMethod.GET) 
    public String getRecords(@RequestParam("userId") Integer userId, ModelMap 

     model) { 


    if(userId !=null) 
    { 
    UserEntity user = userService.getUserByID(userId); 

     model.addAttribute("user", user); 
    } 

    return "/main/user/testing"; 
} 

Jsp頁面

 <table> 
     <tr> 
      <th>User Id</th> 
      <th>Name</th> 
    <th>Module Code</th> 
    <th>Module Name</th> 
      </tr> 



     <c:forEach items="${user}" var="obj" > 
     <c:forEach items="${obj.sModule}" var="module" > 


      <tr> 
       <td><c:out value="${obj.userId}" escapeXml="true" /></td> 
       <td><c:out value="${obj.name}" escapeXml="true" /></td> 

       <td><c:out value="${module.moduleCode}" escapeXml="true" /></td> 
       <td><c:out value="${module.moduleName}" escapeXml="true" /></td> 



      </tr> 
      </c:forEach> 
     </c:forEach> 
    </table> 

使用控制器代碼,當我試圖訪問該頁面。用戶詳細信息不包括在內。所以我想知道是否有一種方法可以爲一個用戶而不是用戶列表呈現對象。

回答

0

爲什麼使用<c:forEach items="${user}" var="obj" >?看起來UserEntity是一個對象,但不是List。因此,除去<c:forEach items="${user}" var="obj" >並嘗試

<c:out value="${user.userId}" escapeXml="true" /> 
+0

我已經修改了我的開場白後,想要澄清的對象是有關係的家長,我想獲得子對象也是如此。所以我想獲得持久集和一個對象。有沒有辦法可以實現? – user2259555 2013-05-06 03:17:54