动态读取记录而不使用getter [英] fetch records dynamically without getter
问题描述
我使用struts2框架动态地获取记录。
例如。
考试1有6个科目。
ExamId 2有8个科目。
考试3有2个科目。
等
但问题是我没有getSubject1()setSubject1()..... getSubjectN()setSubjectN()
<
现在的问题是我应该如何检索它,而不是setter和getters.I使用JQuery Jtable
reference: http://www.simplecodestuffs.com/crud-operations-in-struts-2 -using-jtable-jquery-plugin-via-ajax /
My Bean class
@Entity
public class SubjectBean
{
@Id
@GeneratedValue
private int SubjectId;
private String paperCode;
int totalNumber;
@OneToMany(cascade = CascadeType.ALL)
私人收藏< Subject> subjectList;
private int examId;
//所有
的setter和getters
$ bSubject class
@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);
地图< String,String []> requestParams = request.getParameterMap();
//使用hibernate插入数据
返回success;
}
punlic String getAll()
{
//我怎样才能访问?
$ div $解析方案
你忽略了Struts2和OGNL的基本面。这是 XY问题的完美案例。
你需要的唯一东西就是集合/列表的getter和setter,显然必须在类级别而不是方法级别声明,否则一旦方法执行结束,它就会被销毁。因为在你的情况下,Collection / List在另一个对象内,那么这个对象必须在类级别声明,你也需要getter / setter:
class CrudAction {
private SubjectBean sb;
// getter和setter
另外使用List来排序并使用 []符号和点符号以在IteratorStatus对象的帮助下在页面中指定它
< s:iterator value =sb.subjectListstatus =ctr>
< / s:iterator>
I am fetching records dynamically using struts2 framework. eg. ExamId 1 has 6 subject. ExamId 2 has 8 subject. ExamId 3 has 2 subject. etc. but the problem is i dont have getSubject1() setSubject1().....getSubjectN() setSubjectN()
[EDITED] Now issue is how should i retreive it wihtout setters and getters.I am using JQuery Jtable reference : http://www.simplecodestuffs.com/crud-operations-in-struts-2-using-jtable-jquery-plugin-via-ajax/
My Bean class
@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
}
Subject class
@Entity
public class Subject
{
@Id
@GeneratedValue
private int Id;
private String subjectId;
private int capacity;
//setters and getters
}
action class.
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?
}
}
You are ignoring fundamentals of both Struts2 and OGNL. And this is a perfect case of the XY problem.
The only things you need are the getter and the setter for the Collection/List, that obviously must be declared at class level, not method level, or it'll be destroyed once the method execution is over. Since in your case the Collection/List is inside another object, then that object must be declared at class level, and you will need getter/setter for it too:
class CrudAction {
private SubjectBean sb;
// getter and setter
Also use a List to have order and use the [] notation and the dot notation to specify it in page, with the help of IteratorStatus object
<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>
这篇关于动态读取记录而不使用getter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!