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!)

4

2 回答 2

0
  • 我认为我不能使用注释bean的原因是我没有在我的应用程序中配置CDI,因为Tomcat本身不支持CDI,你应该手动添加一些外部jar文件使其支持。所以这是我配置它的步骤。

  1. 下载weld-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 接口。


  • 给你我的整个程序大纲。 图片
于 2017-11-26T07:48:58.197 回答
0

您需要将@Namedbean 注释设置为类而不是方法。该错误基本上表示服务器未能找到托管 bean 类。所以你的代码应该是这样的:

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

    public String getUserid() {
        return userid;
    }

我看到了你的 Welcome.xhtml。您应该使用# 而不是$。所以你的欢迎页面应该是这样的

<h:outputLabel value="#{userBean.password}" />
于 2017-11-25T16:13:54.180 回答