2013-07-28 29 views
0

我正在使用Spring MVC和Hibernate4開發我的應用程序。我有2個有相關性的課程OneToMany:Lot可以有很多Question。在我的singleLot.jsp中,我只想顯示指定的所有QuestionsLot。當我調試應用程序時,我可以看到一切都很好地從數據庫中提取,但我無法弄清楚如何在我的singleLot.jsp頁面中顯示它。如何從.JSP中的PersistentBag訪問數據?

我認爲這個問題可能是Questions不直接我Lot對象爲List<Question>正如我在實體類中聲明裏面,但他們裏面的東西叫做PersistentBag

Debugging screenshot

Lot實體:

@Entity 
@Table(name = "lot") 

public class Lot { 

private Long lotId; 
private String name; 
private String description; 
private Timestamp dateAdded; 

private List<Question> questions = new ArrayList<Question>(0); //One to many relation 

@Id 
@Column(name = "lot_id") 
@GeneratedValue(strategy = GenerationType.AUTO) 
public Long getLotId(){ return lotId; } 
public void setLotId(Long id) { this.lotId = id; } 

@NotNull 
@Length(min = 1, max = 50) 
@Column(name = "name") 
public String getName() {return name;} 
public void setName(String name) {this.name = name;} 

@NotNull 
@Length(min = 1, max = 500) 
@Column(name = "description") 
public String getDescription() {return description;} 
public void setDescription(String description) {this.description = description;} 

@Column(name = "date_added") 
public Timestamp getDateAdded() {return dateAdded;} 
public void setDateAdded(Timestamp dateAdded) {this.dateAdded = dateAdded;} 

@OneToMany(mappedBy = "lotId", fetch = FetchType.EAGER) 
public List<Question> getQuestions() {return questions;} 
public void setQuestions(List<Question> questions) {this.questions = questions;} 
} 

Question實體:

@Entity 
@Table(name = "question") 
public class Question { 

private Long questionId; 
private Long lotId; 
private String content; 
private List<Answer> answer = new ArrayList<Answer>(0); //One to many relation 

@Id 
@Column(name = "question_id") 
@GeneratedValue(strategy = GenerationType.AUTO) 
public Long getQuestionId(){ return questionId; } 
public void setQuestionId(Long id) { this.questionId = id; } 

@NotNull 
@Length(min = 1, max = 10) 
@Column(name = "lot_id") 
public Long getLotId(){return lotId;} 
public void setLotId(Long lotId) { this.lotId = lotId; } 

@NotNull 
@Length(min = 1, max = 5000) 
@Column(name = "content") 
public String getContent(){return content;} 
public void setContent(String content) { this.content = content; } 

@OneToMany(mappedBy = "questionId", fetch = FetchType.LAZY) 
public List<Answer> getAnswer() {return answer;} 
public void setAnswer(List<Answer> answers) {this.answer = answers;} 

@Override 
public String toString() { 
    return "Question nr. [" + questionId + "] - " + content + "and "+ getAnswer().size() + " possible answers."; 
} 
} 

LotController的方法:

@RequestMapping(value = "{lotId}", method = RequestMethod.GET) 
public ModelAndView viewSingleLot(@PathVariable Long lotId){ 
    ModelAndView modelAndView; 

    Lot lot = lotService.getLot(lotId); 

    modelAndView = new ModelAndView("singleLot"); 
    modelAndView.addObject("lot", lot); 

    return modelAndView; 
} 

而且在那裏我試圖訪問控制器提供的數據我singleLot.jsp的片段:

<c:forEach items="${lot.questions}" var="question" varStatus="rowCounter"> 
<div > 
    <div> 
     <h3>${rowCounter}</h3> 
    </div> 
    <div> 
     <h5>question.name</h5> 
    </div> 
</c:forEach> 

有誰知道我做錯了嗎?

回答

1

應該

<h5>${question.content}</h5> 
+0

我不能相信我是多麼盲目...謝謝你的好先生! – Disper