2017-11-25 115 views
0
  • 有兩種配置託管bean的方法,一種是使用「faces-config.xml」文件,另一種使用「annotations」。
  • 所以在這個演示中,我想在MyEclipse中使用註釋來配置bean,但它不起作用。
  • 下面是代碼:

1.UserBean.javaTarget Unreachable,標識符'userBean'解析爲空

public class UserBean { 
String userid; 
String password; 

@Named("userBean") 
@RequestScoped 
public String getUserid() { 
    return userid; 
} 
public void setUserid(String userid) { 
    this.userid = userid; 
} 
public String getPassword() { 
    return password; 
} 
public void setPassword(String password) { 
    this.password = password; 
}} 

2.Login.xhtml the page users enter the id and password

3.Welcome.xhtml when user click the submit button, the page comes

4.faces-config.xml中 faces-config.xml


正如你所看到的,我並沒有在 「faces-config.xml中」 文件中配置管理的bean,我只是用 「@Named(」 的UserBean」 )「和」@RequestScoped「在我的」UserBean.java「文件中配置bean。


1.I打開login.xhtml網站

http://localhost:8080/JSF/


2.當我按一下按鈕,提交數據時,它出現這個頁面:

After click the submit button


我開始學習JSF,這些天,有很多困惑的事情,我需要弄清楚,非常感謝,如果你能給我一些注意事項或指導在這個問題上^ _^


(Ps.This是我在stackoverflow上問的第一個問題,所以我無法直接上傳圖片,如果您無法通過hperlinks查看圖片,請告訴我。謝謝!)

回答

0

您需要設置@Named bean註釋的方法。 該錯誤基本上說服務器無法找到託管的bean類。所以你的代碼應該是這樣的:

@Named("userBean") 
@RequestScoped 
public class UserBean { 
    String userid; 
    String password; 

    public String getUserid() { 
     return userid; 
    } 

我看到了你的Welcome.xhtml。你應該使用#而不是$。 所以歡迎頁面應該有這樣的事情

<h:outputLabel value="#{userBean.password}" /> 
+0

謝謝你的迴應,我改變了你指出的代碼,但它仍然出現錯誤頁面,即使是同樣的錯誤:目標不可達,標識「的UserBean」解析爲空。 –

+0

是否在web.xml中指定了面向servlet條目? – Harsh

+0

<歡迎文件列表> <歡迎文件> Login.xhtml 面臨的Servlet < servlet-class> javax.faces.webapp.FacesServlet 1 Faces Servlet * .xhtml <的context-param> ​​javax.faces.PROJECT_STAGE Development

0
  • 我想,爲什麼我不能使用註釋豆的原因是,我沒有在我的應用程序配置CDI,因爲Tomcat不支持CDI本身你應該手動添加一些外部jar文件來支持它。所以這是我配置它的步驟。

  1. 下載焊接的servlet.jar文件,這裏是我下載的鏈接,也可以從互聯網上下載它。 http://www.jsf2.com/using-cdi-and-jsf-2.2-faces-flow-in-tomcat/samples/weld-servlet.jar
  2. 這個jar文件添加到目錄「/ WEB-INF/lib目錄「(倒是更好地構建路徑)
  3. 創建 「/ WEB-INF」 下的beans.xml文件,替換現有的beans.xml文件的代碼,代碼片段如下:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"></beans>

  4. 還有一件事,你必須實現Serializble接口。


  • 給你我的整個方案概要。 image
相關問題