0

I spent already more time as is good about some saving or updating issue in inputtext field, i go right to the point: i have basic single input text with some basic attributtes

<h:inputText id="name" value="#{salesController.selectedSalesName}" />

here is a getter for inputText value

public String getSelectedSalesName(){
    for(DealerListView dealer : dealerList){
        if(dealer.getDealerId() == getSelectedDealerId()){
            return dealer.getName();
        }
    }
    return "";  
}   

nested in there i use ajax tag

<f:ajax event="change" render="name" listener="#{salesController.updateSelectedSalesName()}" />

a here is back bean method for updating a input text field

public void updateSelectedSalesName() {     

        DealerData dealDat = BeanFactory.getHotelDAOService(DealerData.class).findOne(selectedDealerId);
        dealDat.setName(name);
        BeanFactory.getHotelDAOService(DealerData.class).update(dealDat);     
}

whole result of this is stack trace which say

value="#{salesController.selectedSalesName}": Property 'selectedSalesName' not writable on type sk.hotel.web.controller.SalesController

I know that something changes is need for that getter method but try some combinations without result which make corect update of value to database.

(I dont use any commands buttons for submit,update only response on pressing Enter in that inputText field.) I want some guide how can be modified this save/update process whether on back-bean or jsf layout or maybe someone solved similar situation already,and can share his solution.

Thanks all of you for advice posts. Regards and nice day

2
  • 1
    1. <h:inputText> value attribute binding needs both setter and getter of the bean's property and the former should cause the prblems you face. 2. You will face performance problems, as the getter may be called many times and the way you wrote it is resource-consuming (alternative: use other String property that you evaluate in Constructor / @PostConstruct method / preRenderView event. 3. Ajax listener must be of the following type public void ajaxListener(AjaxBehaviorEvent event), so change your method declaration in your bean. Commented Mar 1, 2013 at 10:59
  • thank you for post,also for these advices,yes before i thinking about binding both od setter/getter,but this was just light overview,not go into very deeply about this,i try to modified based on this my code,i see how far i get:) Commented Mar 1, 2013 at 12:35

2 Answers 2

4

First, add a field:

  String selectedSalesName;

Add a setter and setter:

  public String getSelectedSalesName() {
     return selectedSalesName;
  }
  public void setSelectedSalesName(String selectedSalesName) {
     this.selectedSalesName = selectedSalesName;
  }

Add a ajaxListener(AjaxBehaviurEvent event) to create a new Dealer or Update current Dealer

  public void ajaxListener(AjaxBehaviorEvent event) {
     Dao dao = BeanFactory.getHotelDAOService(DealerData.class)
     if (selectedDealerId == null) {
        DealarData dealerData= new DealerData();
        dealerDate.setName(getSelectedSalesName());
        dao.add(dealerData);
        setDealer(dealerData);
     } else {
        DealerData dealDat = dao.findOne(selectedDealerId);
        dealDat.setName(name);
        dao.update(dealDat);
     }
  }

A setter to the current dealer

  int selectedDealerId;
  public void setDealer(DealerData dealer) {
        selectedDealerId = dealer.getId();
        selectedSalesName = dealer.getName();
  }

And the xhtml page:

  <h:inputText value="#{salesController.selectedSalesName}" id="idSalesInput">
        <a4j:ajax event="keyup" listener="#{salesController.ajaxListener}" 
               execute="idSalesInput"/>
  </h:inputText>

Change "keyup" for the event you want to listen.

When you press a key, the listener is called, and the value of idSalesInput is submitted (the setSelectedSalesName() method is called here, for this reason you got the Property 'selectedSalesName' not writable exception),and the listener create or update a new DealerData.

Have a nice Day and sorry for my bad english!

Sign up to request clarification or add additional context in comments.

14 Comments

Thanks a lot for this very nice presentation which show how provide submiting value in Jsf,something like this i want found but without result,ok i made some modifications based on this and let you know how is going,regards
Do you want to scare the database with so many operations (keyup)? It is bad practice. Change placeholders instead.
@skuntsel i think the best event to listen is "onblur", can you specify (or a link) what is a PlaceHolder please?
Placeholder is simply a blank class instance that is used to store data during user interaction with the object. It means that no business actions are triggered during interaction with the object, but are later confirmed via action methods. This way, for example, setName method will literally set name in a placeholder, while save (etc.) methods will trigger database manipulation with the stored placeholder.
@AVolpe i tried your solution,i get a bit forward with this, and show me this stacktrace: com.sun.faces.lifecycle.InvokeApplicationPhase execute listener="#{salesController.ajaxListener}": java.lang.NullPointerException at com.sun.faces.facelets.el.TagMethodExpression.invoke i noticed here about "InvokeApplicationPhase" so this means my listener cant get into a RenderResponse phase?is due to NullPointerException?
|
1

Binding value in your inputText is two way, when it is rendered than getter is called to calculate value, when it is submited (like in your AJAX event) setter is called for that property to set value in your backing bean. So JSF tries to call setSelectedSalesName(String value). JSF can't write your property, which means can't call setter.

See also:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.