Java 如何克服JSF中实例方法中写入静态字段的错误?
我非常清楚,在实例方法中写入Java 如何克服JSF中实例方法中写入静态字段的错误?,java,jenkins,jsf-2.2,static-variables,Java,Jenkins,Jsf 2.2,Static Variables,我非常清楚,在实例方法中写入静态变量是一种糟糕的做法。(因为它将被所有实例操纵) 我们正在使用詹金斯进行构建。 我还没有编写代码,但是已经被招募到团队中来修复bug 类TreeBean.java中有一个方法 TreeNode getRootNode(){ // construst the tree here, creating nodes and appending child, all that stuff // here we have this static var
静态变量是一种糟糕的做法。(因为它将被所有实例操纵)
我们正在使用詹金斯进行构建。
我还没有编写代码,但是已经被招募到团队中来修复bug
类TreeBean.java中有一个方法
TreeNode getRootNode(){
// construst the tree here, creating nodes and appending child, all that stuff
// here we have this static variable
MyOverviewBean.tempNewvalue = MyInterface.DEFAULT_PAGE_SIZE;
}
public void changeRowsCount(ValueChangeEvent anEvent){
String tmpNewVal = null;
tmpNewVal = ((String) anEvent.getNewValue());
tempNewvalue= tmpNewVal; // here the new value is assigned
setRowCountValue(tempNewvalue);
...
}
在哪里
在MyInterface.java中
现在我们有了这个静态变量MyOverviewBean.tempNewvalue
,它将被分配一个10
的值,这样用户只要单击其中一个节点,就会显示10条记录
在我所说的JSF页面的底部,有一个下拉列表,允许用户选择记录的数量。目前,他有4种选择
10
50
100
1000
MyOverviewBean.java
有一个值更改侦听器来处理上述事项(用户选择的记录数)
有两种方法进行反向处理。
它改变了我所说的静态变量的值。
TreeNode getRootNode(){
// construst the tree here, creating nodes and appending child, all that stuff
// here we have this static variable
MyOverviewBean.tempNewvalue = MyInterface.DEFAULT_PAGE_SIZE;
}
public void changeRowsCount(ValueChangeEvent anEvent){
String tmpNewVal = null;
tmpNewVal = ((String) anEvent.getNewValue());
tempNewvalue= tmpNewVal; // here the new value is assigned
setRowCountValue(tempNewvalue);
...
}
另一种方法是,
此方法处理页面计数。
如何删除此静态变量
我尝试了将新值分配给changeRowCount中的一个局部变量,但是由于静态变量的原因,我没有将它传递给另一个方法initializeRowCount。
请帮忙
我还尝试将静态变量简化为实例变量,然后在TreeNode getRootNode()中访问该实例变量{
但这是徒劳的,因为用户选择的记录数与数据表中显示的记录数不同步
请给我提些建议
// below alternative as implemented by me.
FacesContext context = FacesContext.getCurrentInstance();
MyOverviewBean myOverviewBean=
context.getApplication().evaluateExpressionGet(context, "#
{myOverviewBean}", MyOverviewBean.class);
// so now this code
MyOverviewBean.tempNewvalue = MyInterface.DEFAULT_PAGE_SIZE;
becomes
myOverviewBean.tempNewvalue = MyInterface.DEFAULT_PAGE_SIZE;