基于jsf-component的portlet的构建通常需要了解一下几点
1. 所有的jsf-component都是单一实例的,也就是说在每一个session周期内,一个ui组件只存在一个实例
2 对于ui组件中的变量,对于不属于ui组件的对象(ftpclient),在ui对象构建初期通过构造函数传递,之后,无论这些对象在其他地方发生任何变化,ui中引用的仍然是该对象的实例。对于ui组件自己的对象(uistringinput),如果用setxx方法对其赋值了,ui对象在encode的时候,引用的也是该对象的当前值。
public class uifileform extends uisimpleform {
static final public string save_action = "save";
static final public string cancel_action = "cancel";
private ftpfile ftpfile_;
private ftpclient ftpclient_;
private uistringinput nameinput_;
private string filename_;
public uifileform(ftpclient ftp,resourcebundle res) throws exception {
super("fileform", "post", null);
setid("uifileform");
setclazz("uifileform");
ftpclient_ = ftp; //引用外部对象
int idx = ftpfile_.getname().lastindexof("/");
string filename = ftpfile_.getname().substring(idx++);
nameinput_ = new uistringinput("name", filename);
add(
new headerrow().add(
new cell(res.getstring("header.edit-file")).addcolspan("2")));
add(
new row().add(new labelcell(res.getstring("label.file-name"))).add(
new componentcell(this, nameinput_))); //尽管这里是在构造函数里面,但是nameinput是对象,所有即使它的值变化了,encode的时候仍然得到的是变化后的值
add(
new row().add(
new listcomponentcell()
.add(
new formbutton(
res.getstring("button.save"),
save_action))
.add(
new formbutton(
res.getstring("button.cancel"),
cancel_action))
.addcolspan("2")
.addalign("center")));
addactionlistener(saveactionlistener.class, save_action);
addactionlistener(cancelactionlistener.class, cancel_action);
}
public void setfilename(string s) {
filename_ = s;
int idx = ftpfile_.getname().lastindexof("/");
string filename = ftpfile_.getname().substring(idx++);
nameinput_.settext(filename);// 这里是重新改变值的地方
}
