2

我是 JSF 及其同事的新手。

我的开发环境如下 Java 6 Tomcat 6 Eclipse Primeface 2.5

我正在尝试即时 primefaces starter 中描述的测试 Web 应用程序。

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">

<display-name>PrimeFaces Web Application</display-name>

<!-- Change to "Production" when you are ready to deploy -->
<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>

<!-- Welcome page -->
<welcome-file-list>
    <welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>

<!-- JSF mapping -->
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- Map these files with JSF -->
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

面孔-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config 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/web-facesconfig_2_1.xsd" 
          version="2.1">

豆类.xml

是一个空文件

索引.xhtml

<!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:p="http://primefaces.org/ui">
<h:head />
<body>
<ui:composition template="/WEB-INF/templates/main.xhtml">
    <ui:define name="title">Welcome to our site</ui:define>
    <ui:define name="content">
        <h:form>
            <h:panelGrid columns="2" border="0">
                <p:panel header="Please Sign In">
                    <p:panelGrid columns="2" styleClass="no-border"
                        columnClasses="no-border,no-border">
                        <p:outputLabel value="Username" for="username" />
                        <p:inputText id="username" label="Username"
                            value="#{credentials.username}" />
                        <p:outputLabel value="Password" for="password" />
                        <p:password id="password" label="Password"
                            value="#{credentials.password}" />
                        <h:outputText value="" />
                        <p:commandLink value="Forgot your password?" />
                        <h:outputText value="" />
                        <p:commandButton value="Login" action="members"
                            actionListener="#{loginController.login}" />
                        <h:outputText value="" />
                    </p:panelGrid>
                </p:panel>
                <ul>
                    <li>Find out what's happening</li>
                    <li>Rate and review shows</li>
                    <li>Invite your friends</li>
                    <li><a href="/faces/register.xhtml">Join today!</a>
                    </li>
                </ul>
            </h:panelGrid>
        </h:form>
    </ui:define>
</ui:composition>
</body>
</html>

登录控制器.java

package com.example.starterexample.controller;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;

import com.example.starterexample.model.Credentials;

/**
 * Controller class for desktop and mobile login functionality.
 * 
 * @uml.dependency supplier="com.mycompany.controller.AbstractController"
 */
@ManagedBean(name="loginController", eager=true)
//@ViewScoped
@RequestScoped
/*
 * @ViewScoped  means the object is created by JSF 
 *              and its lifecycle is scoped to the current view.
 *          Once the user navigates to another view, the LoginControllerobject     is released and 
 *          will eventually be garbage-collected by the JVM
 */
public class LoginController extends AbstractController {

private static final long serialVersionUID = -6710063228036238737L;

@ManagedProperty(value = "#{credentials}")
private Credentials credentials;

public String login() {
    String outcome = null;
    try {
        String username = credentials.getUsername();
        String password = credentials.getPassword();
        //User user = userService.findUser(username, password);
        //if (user == null) {
        //  addErrorMessage("Invalid login");
        //  userSession.setUser(null);
        //} else {
        //  userSession.setUser(user);
        //  if (!activeUsers.contains(user)) {
        //      activeUsers.add(user);
        //  }
            //outcome = "members";
        //}

            addErrorMessage("Invalid login");
    } catch (Exception e) {
        logger.error("Unable to login:", e);
    }
    return outcome;
}

public void setCredentials(Credentials credentials) {
    this.credentials = credentials;
}


}

凭据.java

package com.example.starterexample.model;

import javax.enterprise.inject.Model;

@Model
public class Credentials {

private String username;
private String password;

public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}


}

错误:

Sep 21, 2013 5:03:34 PM com.sun.faces.lifecycle.ProcessValidationsPhase execute
WARNING: /index.xhtml @19,67 value="#{credentials.username}": Target Unreachable, identifier 'credentials' resolved to null
javax.el.PropertyNotFoundException: /index.xhtml @19,67 value="#{credentials.username}": Target Unreachable, identifier 'credentials' resolved to null
at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
at             org.primefaces.renderkit.InputRenderer.findImplicitConverter(InputRenderer.java:170)
at org.primefaces.renderkit.InputRenderer.findConverter(InputRenderer.java:190)
at org.primefaces.renderkit.InputRenderer.getConvertedValue(InputRenderer.java:196)
at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
at javax.faces.component.UIInput.validate(UIInput.java:960)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
at javax.faces.component.UIInput.processValidators(UIInput.java:698)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at org.primefaces.component.panel.Panel.processValidators(Panel.java:297)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIForm.processValidators(UIForm.java:253)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)

请帮我找出我缺少的东西。

我一直在寻找类似的问题,对我没有任何帮助。

我检查了创建的战争文件和类文件在 WEB-INF/classes

提前致谢。

4

3 回答 3

2

我也将此示例用于自学等。实际上,我遇到了与您相同的问题。此示例存在错误,因此您不应盲目使用它。这篇文章帮助我更好地理解了 bean 类型(CDI、EJB、JSF bean)并编写了可行的代码。

您不应将 JSF 托管 bean 与 CDI 一起使用。CDI bean 本身更强大。这对我有用。

LoginController 开始像

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;


@Named
@javax.faces.view.ViewScoped
public class LoginController extends AbstractController {


    private static final long serialVersionUID = -6710063228036238737L;


        @Inject
    private ActiveUsers activeUsers;

        @Inject
    private Credentials credentials;

        @Inject
    private UserService userService;

....
}

ActiveUsers 看起来像

import java.util.ArrayList;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

@Named("activeUsers")
@ApplicationScoped
public class ActiveUsers extends ArrayList<User> {

    private static final long serialVersionUID = -3068333669859836249L;

}

证书:

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named("credentials")
@RequestScoped
public class Credentials {

    private String password;

    private String username;

    public String getPassword() {
        return password;
    }

    public String getUsername() {
        return username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

}

希望对你有帮助

于 2014-02-26T17:24:22.917 回答
1

您不能做您想做的事情:使用 JSF 注释访问 CDI bean。javax.enterprise.inject.model是基于 CDI 的注释,因此,根据您的 JSF 版本,您有以下选项:

  1. 使用 @Inject转换loginController为 CDI bean @Named,然后使用 @Inject 获取模型 - JSF 2.1

  2. @Inject升级到 JSF 2.2 ,让您可以使用@ManagedPropertyCDI bean

于 2013-09-22T22:54:53.480 回答
0

在 faces 上下文中只能访问 ManagedBeans。因此,您将不得不通过 访问credentialsloginController例如#{loginController.credentials.username}。您还需要向getCredentials()LoginController 添加一个方法。

于 2013-09-21T13:36:47.870 回答