setter方法的返回类型是否必须为空? [英] Does setter method return type have to be void?

查看:193
本文介绍了setter方法的返回类型是否必须为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了方便起见,我将setter方法的返回类型更改为一个对象,例如:obj.setValue(newVal).setName(newName).setId(newId);

I change setter method return type to an object for ease eg: obj.setValue(newVal).setName(newName).setId(newId);

但是在更改之后,tomcat给出了

but after this change tomcat gives

javax.el.PropertyNotFoundException:

javax.el.PropertyNotFoundException:

    /WEB-INF/flows/materialorder/newOrder.xhtml @99,182 
value="#{materailOrdService.mofEntity.nmExplanation.value}": 
Property 'value' not writable on type tr.com.hydron.softexpert.order.model.MainFormEntity$MofField

要摆脱此异常,我将修饰符从private更改为 公开,但仍然出现相同的错误.所以我有两个问题;

To get rid of this exception I changed modifier from private to public but still getting the same error. So I have two questions;

  1. jsf是否需要setter来修改对象,即使它是修饰符 是公开的吗?
  2. jsf的setter方法返回类型是否必须为空 修改对象值?
  1. Does jsf need setter for modifying the object even if it's modifier is public?
  2. Does setter method return type have to be void for jsf modifying an object value?

这是我的对象类

public static class MofField implements Serializable{

    private static final long serialVersionUID = 1L;
    public Object value;
    ...    


    public Object getValue() {
        return value;
    }
    public MofField setValue(Object value) {
        this.value = value;
        return this;
    }       
    ...     
}

修改: 这是我的xhtml代码:

edit: here is my xhtml code:

<p:inputTextarea valueChangeListener="#{materailOrdService.onExplanationChange}"  value="#{materailOrdService.mofEntity.nmExplanation.value}" rows="3" cols="38" >
    <p:ajax event="valueChange" global="false" immediate="true" partialSubmit="true" process="@this"  />
</p:inputTextarea>

推荐答案

jsf是否需要setter来修改对象,即使其修改器为 公开吗?

Does jsf need setter for modifying the object even if it's modifier is public?

简短的回答:是的.

按照惯例,Bean的每个字段都应为私有字段,并由各自的getter和setter进行访问/突变.

By convention, each field of a Bean should be private and be accessed/mutated by there respective getters and setters.

将字段更改为公共字段不会更改任何内容,因为当您在EL代码中键入字段名称时,如果您的字段是布尔值,服务器将搜索setFieldName(param)getFieldNameisFieldName

Changing the field to public won't change anything, as when you type in the field name in your EL code, the server will search for the setFieldName(param) or getFieldName or isFieldName if your field is a boolean.

setter方法的返回类型是否必须为jsf修改的无效类型 对象值?

Does setter method return type have to be void for jsf modifying an object value?

是,服务器将使包含void的签名无效.试试看.

Yes, the server will search void a signature containing void. Try it and see.

这篇关于setter方法的返回类型是否必须为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆