【JSF心得】JAVA的对象传递是引用传递

博客介绍基于jsf - component的portlet构建要点,指出jsf - component是单一实例,每个session周期内一个UI组件仅一个实例。还说明了UI组件中变量的引用情况,对于外部对象和自身对象在赋值及encode时的引用规则,并给出了相关代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于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);// 这里是重新改变值的地方

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值