0
  • There are two ways to configure managed beans, one is Using the "faces-config.xml" file, another is using "annotations".
  • So in this demo, I want to configure beans using annotations in MyEclipse, but it didn't work.
  • Here is the code:

1.UserBean.java

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


As you can see, I didn't configure the managed bean in the "faces-config.xml" file, I just use "@Named("userBean")" and "@RequestScoped" in my "UserBean.java" file to configure the bean.


1.I open the login.xhtml on the website

http://localhost:8080/JSF/


2.When I click the button to submit the data, it comes up this page:

After click the submit button


I started to learn JSF these days, there are many confused things I need to figure out, thanks a lot if you can give me some notes or guidance on this question ^_^


(Ps.This is the first question I ask on stackoverflow, so I couldn't upload pictures directly, if you cannot see the pictures by hperlinks, please let me know.Thanks!)

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Liu Lou
  • 1
  • 1
  • 1

2 Answers2

0

You need to set @Named bean annotation to the class instead of the method. The error basically says that server failed to find the managed bean class. So your code should be like:

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

    public String getUserid() {
        return userid;
    }

I saw your Welcome.xhtml. You should use # instead of $. so your welcome page should have something like this

<h:outputLabel value="#{userBean.password}" />
Harsh
  • 336
  • 2
  • 13
  • Thank you for the response, I changed the code you point out, but it still comes up error page, even the same error: Target Unreachable, identifier 'userBean' resolved to null. – Liu Lou Nov 25 '17 at 16:36
  • did you specify faces servlet entry in web.xml? – Harsh Nov 25 '17 at 16:41
  • Login.xhtml Faces Servlet javax.faces.webapp.FacesServlet 1 Faces Servlet *.xhtml javax.faces.PROJECT_STAGE Development – Liu Lou Nov 25 '17 at 16:55
  • I think the error is in expression language. Try using `"#{userBean.VALUE}"` every where instead of "${userBean.VALUE}" – Harsh Nov 25 '17 at 17:06
  • I have tried, never work. Anyway, thank you a lot, I may ask my teacher tommorrow.GN. – Liu Lou Nov 25 '17 at 17:09
  • It only works when I configure the UserBean in faces-config.xml file like this: userBean Beans.UserBean request – Liu Lou Nov 25 '17 at 17:12
  • You **do** have cdi configured/loaded as a lib do you? – Kukeltje Nov 25 '17 at 18:01
  • Could you tell me specific steps about how to configure/load cdi as a lib? – Liu Lou Nov 26 '17 at 04:43
  • @Kukeltje, You are right, I didn't configure the cdi, now it works. – Liu Lou Nov 26 '17 at 07:16
0
  • I think the reason why I couldn't use annotation bean is that I didn't configure CDI in my application, because Tomcat doesn't support CDI by itself, you should manually add some external jar files to make it support. So here is the step I configure it.

  1. Download weld-servlet.jar file, here is the link I downloaded it, you can also download it from internet. http://www.jsf2.com/using-cdi-and-jsf-2.2-faces-flow-in-tomcat/samples/weld-servlet.jar
  2. Add this jar file to the directory ”/WEB-INF/lib“('d be better to build path)
  3. Create a beans.xml file under "/WEB-INF", replace the existing code of the beans.xml file with the following code snippet:<?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. One more thing, you must implement Serializble interface.


  • Give you my whole program outline. image
Liu Lou
  • 1
  • 1
  • 1