【问题标题】:target unreachable 'client' returned null [duplicate]目标无法访问的“客户端”返回 null [重复]
【发布时间】:2014-07-10 14:20:07
【问题描述】:

我创建了一个添加方法,我使用了 jsf 和 hibarnate 框架,但出现了一个明显的异常,她阻止了我。

这是我的课豆

package controller;

import java.io.Serializable;
import javax.faces.bean.RequestScoped; 
import org.springframework.beans.factory.annotation.Autowired;
import model.Client;
import javax.faces.bean.ManagedBean;
import service.ClientService;

@ManagedBean(name="clientBean")
@RequestScoped
public class ClientBeanManger implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final String SUCCESS = "success";
    private static final String ERROR   = "error";

    //Spring User Service is injected...
    @Autowired
    ClientService clientService;
    private Client client;




    public Client getClient() {
        return client;
    }


    public void setClient(Client client) {
        this.client = client;
    }


    public ClientBeanManger() {

    }

    private int idClient;
    private String nomClient;
    private String adresseClient;
    private String telephoneClient;
    private String mailClient; 



    // Add client @return String - Response Message

    public void addClient(Client client) {
        client.setIdClient(idClient);
        client.setNomClient(nomClient);
        client.setAdresseClient(adresseClient);
        client.setTelephoneClient(telephoneClient);
        client.setMailClient(mailClient);
        clientService.add(client);          
    }


    //Getter and  setter 
    public ClientService getClientService() {
        return clientService;
    }

    public void setClientService(ClientService clientService) {
        this.clientService = clientService;
    }

    public int getIdClient() {
        return idClient;
    }

    public void setIdClient(int idClient) {
        this.idClient = idClient;
    }

    public String getNomClient() {
        return nomClient;
    }

    public void setNomClient(String nomClient) {
        this.nomClient = nomClient;
    }

    public String getAdresseClient() {
        return adresseClient;
    }

    public void setAdresseClient(String adresseClient) {
        this.adresseClient = adresseClient;
    }

    public String getTelephoneClient() {
        return telephoneClient;
    }

    public void setTelephoneClient(String telephoneClient) {
        this.telephoneClient = telephoneClient;
    }

    public String getMailClient() {
        return mailClient;
    }

    public void setMailClient(String mailClient) {
        this.mailClient = mailClient;
    }


}

她是我的观点

 <html xmlns="http://www.w3.org/1999/xhtml"
         xmlns:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core"
         xmlns:p="http://primefaces.org/ui">
     <h:body>
         <h:form  action="" method="post">
          <table border="1">
           <tr>
            <td><h:outputLabel value="Id :"/></td>
            <td><h:inputText id="id" value="#{clientBean.idClient}"/>  
            </td>
           </tr>
           <tr>
            <td><h:outputLabel for="name" value="Name :" /></td>
            <td><h:inputText id="name" value="#{clientBean.nomClient}"/>

            </td>
           </tr>
            <tr>
            <td><h:outputLabel for="adresse" value="Adresse :" /></td>
            <td><h:inputText id="adresse" value="#{clientBean.adresseClient}"/>
            </td>
           </tr>
            <tr>
            <td><h:outputLabel for="telephone" value="Telephone :" /></td>
            <td><h:inputText id="telephone" value="#{clientBean.telephoneClient}"/>
           </td>
           </tr>
              <tr>
            <td><h:outputLabel for="mail" value="Mail :" /></td>
            <td><h:inputText id="mail" value="#{clientBean.mailClient}"/>
           </td>
           </tr>
             <tr>
           <td><h:commandButton id="addclient" value="Add" action="#{clientBean.addClient}"/></td>
            <td><h:commandButton id="reset" value="Lister"  action="Submit"/></td> 
           </tr>
          </table>
         </h:form>
    </h:body>
    </html>

错误:

Etat HTTP 500 - javax.el.PropertyNotFoundException: /pages/index.xhtml @35,97 action="#{clientBean.client.addClient}": Target Unreachable, 'client' returned null

我知道还有很多同名的问题,但没有一个有用。请帮助我,我开始放手了

【问题讨论】:

  • 发生错误时,页面加载或提交表单?
  • 尝试实例化属性client
  • @Omar :我可以在其中实例化对象客户端
  • @user3766709,就是上面在托管bean中定义的地方,即private Client client = new Client();

标签: hibernate jsf


【解决方案1】:

@AutoWired 不会将 spring bean 注入 jsf bean;他们处于两种不同的环境中。

只有当 ClientBeanManger 也是 Spring MVC 管理的 bean 时,Spring 才能使用 @Autowired 注入 bean。从您的问题的编写方式和 cmets 来看,在我看来您需要探索连接 Spring bean 和 JSF bean 的基础知识。

看看下面的

【讨论】:

  • 我修改了文件 spring beans 和 jsf beans 但问题仍然存在
猜你喜欢
  • 2011-04-14
  • 1970-01-01
  • 2011-11-10
  • 2014-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多