2015-01-12 60 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>