Свойство JSF SessionScoped не задано

Я использую bean-компонент SessionScoped для хранения моего объекта Client, который я получаю из ClientService, использующего JPA. Когда моя веб-страница загружается, я передаю параметр GET Id для получения правильного объекта Client. Все идет нормально, но когда я тоже пытаюсь отредактировать свойства объекта Client, они почему-то не меняются.

Вот мой код:

Вид :

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">


        <body>

            <ui:composition template="./../../WEB-INF/templates/master.xhtml">

                <ui:define name="functions">
                    <li><h:link outcome="clients/overview"><i class="icon-chevron-right"></i> <h:outputText value="#{trans['sidebar.links.overview']}" /> </h:link></li> 
                </ui:define>

                <ui:define name="page-header">
                    Edit - Clients
                </ui:define>

                <ui:define name="content">

                    <f:metadata>
                        <f:viewParam name="id" value="#{editClientBean.id}" required="false"/>
                        <f:event type="preRenderComponent" listener="#{editClientBean.findClient()}"/>
                    </f:metadata>


                    <h:form class="form-horizontal">
                        <div class="control-group">
                            <label class="control-label" for="inputEmail">Firstname</label>
                            <div class="controls">
                                <h:inputText  value="#{editClientBean.client.firstname}" />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="inputPassword">Lastname</label>
                            <div class="controls">
                                <h:inputText value ="#{editClientBean.client.lastname}" />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="inputPassword">Birthdate</label>
                            <div class="controls">
                                <h:inputText value ="#{editClientBean.client.dateToString()}" />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="inputPassword">City</label>
                            <div class="controls">
                                <h:inputText value ="#{editClientBean.client.city}" />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="inputPassword">Address</label>
                            <div class="controls">
                                <h:inputText value ="#{editClientBean.client.address}" />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="inputPassword">Zip</label>
                            <div class="controls">
                                <h:inputText value ="#{editClientBean.client.zipcode}" />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="inputPassword">Info</label>
                            <div class="controls">
                                <h:inputText value ="#{editClientBean.client.info}" />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="inputPassword">Info</label>
                            <div class="controls">
                                <h:inputText value ="#{editClientBean.client.info}" />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="inputPassword">Email</label>
                            <div class="controls">
                                <h:inputText value ="#{editClientBean.client.email}" />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="inputPassword">Password</label>
                            <div class="controls">
                                <h:inputSecret value="#{editClientBean.client.password}" />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="inputPassword">Cartrack ID(s)</label>
                            <div class="controls">

                            </div>
                        </div>

                        <div class="control-group">
                            <div class="controls">

                                <h:commandButton  value="Change" >
                                    <f:ajax listener="#{editClientBean.edit}" />
                                </h:commandButton>
                            </div>
                        </div>

                    </h:form>

                </ui:define>

            </ui:composition>

        </body>
    </html>

Фасоль:

package Beans;

import Entities.Cartrack;
import Entities.Client;
import Services.CarTrackService;
import Services.ClientService;
import java.io.Serializable;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author Roel
 */
@Named
@SessionScoped
public class EditClientBean implements Serializable {

    @Inject
    private ClientService clientService;
    @Inject 
    private CarTrackService cartrackService;
    private Client client;
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void findClient(){
        this.client = this.clientService.Find(this.id);
    }

    /**
     * @return the client
     */
    public Client getClient() {
        return client;
    }

    /**
     * @param client the client to set
     */
    public void setClient(Client client) {
        this.client = client;
    }

    public List<Cartrack> getCarTracks(){
        return this.cartrackService.FindAll();
    }

    public void edit(){
        System.out.println(this.client.getFirstname());
        System.out.println(this.client.getCartracks().size());


        //this.clientService.Edit(this.client);
    }
}

person user2281918    schedule 15.04.2013    source источник
comment
это не меняется Не могли бы вы рассказать об этом подробнее? Что не меняется? Объект, который вы сохраняете в сеансе, или страница не становится редактируемой?   -  person Apurv    schedule 15.04.2013


Ответы (1)


Отредактированные значения формы не отправляются на commandButton с поддержкой Ajax, если они явно не указаны в атрибуте execute. Попробуйте добавить это:

<h:commandButton  value="Change" >
  <f:ajax execute="@form" render="@form" listener="#{editClientBean.edit}" />
</h:commandButton>

@form — это зарезервированное ключевое слово в свойствах execute и render, чтобы указать отправке формы Ajax включить контейнер именования формы и все дочерние элементы в этой форме. Вы указываете отправить все значения формы, которые будут применены к модели, а затем в атрибуте render вы указываете повторно отображать все элементы формы после обработки на стороне сервера.

person maple_shaft    schedule 15.04.2013
comment
Так глупо ... вчера решил это, одноклассник обнаружил, что я забыл поместить type=submit в командную кнопку! - person user2281918; 16.04.2013