2015-01-12 64 views
-3

我使用struts2框架動態獲取記錄。 例如。 考試1有6個科目。 ExamId 2有8個科目。 ExamId 3有2個科目。 等 但我沒有getSubject1(問題)setSubject1()..... getSubjectN()setSubjectN()無動態獲取記錄

將帖子 現在的問題是我應該如何retreive它wihtout getter和setter方法。我是使用JQuery的JTable 參考:http://www.simplecodestuffs.com/crud-operations-in-struts-2-using-jtable-jquery-plugin-via-ajax/

我Bean類

@Entity 
public class SubjectBean 
{ 
@Id 
@GeneratedValue 
private int SubjectId; 
private String paperCode; 
int totalNumber; 

@OneToMany(cascade=CascadeType.ALL) 
private Collection<Subject> subjectList; 

private int examId; 
    //setters and getters of all 
} 

主題類

@Entity 
public class Subject 
{ 
@Id 
@GeneratedValue 
private int Id; 
private String subjectId; 
private int capacity; 
    //setters and getters 
} 

動作類。

class CrudAction 
{ 
public String insert() throws IOException 
{ 

    String examid =(String) httpSession.getAttribute("examId"); 
    Integer examId =Integer.parseInt(examid); 

    Map<String, String[]> requestParams = request.getParameterMap(); 

    //inserting data using hibernate 

    return "success"; 
} 
punlic String getAll() 
{ 
     // how can i access? 
    } 
} 
+0

你能取悅更加清晰。如果有可能添加一些代碼,並解釋你在哪裏發現的困難.. – Babel

+0

請檢查編輯。 – gracy

+0

爲什麼不使用'ArrayList'?在該列表中保留'任何持有數據的Bean類對象。..並創建'ArrayList' getter和setter ..它將在您的JSP中可用。 – Babel

回答

0

您忽略了Struts2和OGNL的基本原理。這是一個完美的案例the XY problem

您唯一需要的東西是getter和爲集合/目錄的制定者,這顯然必須以一流的水平,而不是方法級別聲明,否則一旦方法執行結束它會被銷燬。因爲在你的情況下,收集/ List是另一個對象內,則該對象必須在類級別上聲明,你需要的getter/setter來說太:

class CrudAction { 
    private SubjectBean sb; 
    // getter and setter 

還使用了列表有秩序和使用在[]符號點符號在網頁中指定,IteratorStatus對象的幫助

<s:iterator value="sb.subjectList" status="ctr"> 
    <s:property value="sb.subjectList[%{#ctr.index}].id" /> 
    <s:hidden name="sb.subjectList[%{#ctr.index}].id" /> 
    <s:textfield name="sb.subjectList[%{#ctr.index}].subjectId" /> 
    <s:textfield name="sb.subjectList[%{#ctr.index}].capacity" type="number" /> 
</s:iterator>