简体   繁体   English

未设置JSF SessionScoped属性

[英]JSF SessionScoped Property not set

I'm using a SessionScoped bean for storing my Client object that i retreive from a ClientService thats using JPA. 我正在使用SessionScoped bean存储我从使用JPA的ClientService撤回的Client对象。 When my webpage is loaded i pass a GET Id parameter for retreiving the correct Client object. 加载网页时,我传递了GET Id参数来检索正确的Client对象。 Everything goes fine but when i try too edit the Client object properties, it doesn't change for some reason. 一切都很好,但是当我也尝试编辑Client对象属性时,由于某种原因它没有改变。

Here is my code: 这是我的代码:

View : 查看:

<?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>

Bean : 豆角,扁豆 :

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);
    }
}

Edited form values do not get submitted on an Ajax enabled commandButton unless they are explicity named in the execute attribute. 除非在execute属性中明确命名了已编辑的表单值,否则它们不会在启用Ajax的commandButton上提交。 Try adding this: 尝试添加以下内容:

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

The @form is a reserved keyword in the execute and render properties to instruct an Ajax form submission to include the form naming container and all children within that form. @formexecuterender属性中的保留关键字,用于指示Ajax表单提交包括表单命名容器和该表单中的所有子级。 You are specifying to submit all form values to be applied to the model and then in the render attribute you are instructing to re render all of the form elements after server side processing. 您指定要提交所有要应用于模型的表单值,然后在render属性中指示服务器端处理后重新呈现所有表单元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM