导航案例在 JSF 中不起作用

发布于 2024-10-04 18:08:05 字数 5313 浏览 0 评论 0原文

我们正在开发第一个 JSF 项目,但遇到了一些问题。我们正在制作登录功能,当我们点击登录按钮时,它会抛出:

  • an IllegalArgumentException - null 来源

  • 一个 NullPointerException - Servlet.service() 用于 servlet [Faces Servlet] 在带有路径的上下文中 [/jsf-blank] 抛出异常

你知道它为什么不起作用吗?请赐教。

这是用户 bean:

public class User
{
    private long    id;
    private String  username;
    private String  password;

    public User()
    {
    }

    public User(String username, String password)
    {
        this.username = username;
        this.password = password;
    }

    public User(long id, String username, String password)
    {
        super();
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public String getNextPage()
    {

        return "failure"; //or "admin" or "client"
    }

    public long getId()
    {
        return id;
    }

    public void setId(long id)
    {
        this.id = id;
    }

    public String getUsername()
    {
        return username;
    }

    public String getPassword()
    {
        return password;
    }

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

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

我们的 login.jsp 页面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
</head>
<body>
<f:view>
    <h:form>
        Username: <h:inputText value="#{userBean.username}"></h:inputText>
        <br />
        Password: <h:inputText value="#{userBean.password}"></h:inputText>
        <br />
        <h:commandButton value="Login" action="#{userBean.getNextPage}" ></h:commandButton>
    </h:form>
</f:view>
</body>
</html>

我们的 faces-config.xml:

<?xml version="1.0"?>
<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_0.xsd"
    version="2.0">

    <managed-bean>
        <managed-bean-name>userBean</managed-bean-name>
        <managed-bean-class>data_layer.model.User</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>

    <navigation-rule>
        <from-view-id>/login.jsp</from-view-id>
        <navigation-case>
            <from-action>#{userBean.getNextPage}</from-action>
            <from-outcome>admin</from-outcome>
            <to-view-id>/admin.jsp</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-action>#{userBean.getNextPage}</from-action>
            <from-outcome>failure</from-outcome>
            <to-view-id>/failure.jsp</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-action>#{userBean.getNextPage}</from-action>
            <from-outcome>client</from-outcome>
            <to-view-id>/client.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>
</faces-config>

我们的 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_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>javax.faces.CONFIG_FILES</param-name>
        <param-value>/WEB-INF/faces-config.xml</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <!-- <welcome-file>index.html</welcome-file> -->
    </welcome-file-list>
</web-app>

我们的目录结构是: 替代文字

We're working on our first JSF project, and we have some problems. We are making the login functionality, and when we hit the login button, it throws:

  • an IllegalArgumentException - null
    source

  • a NullPointerException -
    Servlet.service() for servlet [Faces
    Servlet] in context with path
    [/jsf-blank] threw exception

Do you have any idea why it's not working? Please enlighten us.

Here is out User bean:

public class User
{
    private long    id;
    private String  username;
    private String  password;

    public User()
    {
    }

    public User(String username, String password)
    {
        this.username = username;
        this.password = password;
    }

    public User(long id, String username, String password)
    {
        super();
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public String getNextPage()
    {

        return "failure"; //or "admin" or "client"
    }

    public long getId()
    {
        return id;
    }

    public void setId(long id)
    {
        this.id = id;
    }

    public String getUsername()
    {
        return username;
    }

    public String getPassword()
    {
        return password;
    }

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

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

Our login.jsp page:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
</head>
<body>
<f:view>
    <h:form>
        Username: <h:inputText value="#{userBean.username}"></h:inputText>
        <br />
        Password: <h:inputText value="#{userBean.password}"></h:inputText>
        <br />
        <h:commandButton value="Login" action="#{userBean.getNextPage}" ></h:commandButton>
    </h:form>
</f:view>
</body>
</html>

Our faces-config.xml:

<?xml version="1.0"?>
<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_0.xsd"
    version="2.0">

    <managed-bean>
        <managed-bean-name>userBean</managed-bean-name>
        <managed-bean-class>data_layer.model.User</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>

    <navigation-rule>
        <from-view-id>/login.jsp</from-view-id>
        <navigation-case>
            <from-action>#{userBean.getNextPage}</from-action>
            <from-outcome>admin</from-outcome>
            <to-view-id>/admin.jsp</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-action>#{userBean.getNextPage}</from-action>
            <from-outcome>failure</from-outcome>
            <to-view-id>/failure.jsp</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-action>#{userBean.getNextPage}</from-action>
            <from-outcome>client</from-outcome>
            <to-view-id>/client.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>
</faces-config>

Our 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_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>javax.faces.CONFIG_FILES</param-name>
        <param-value>/WEB-INF/faces-config.xml</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <!-- <welcome-file>index.html</welcome-file> -->
    </welcome-file-list>
</web-app>

And our directory structure is:
alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年九夏 2024-10-11 18:08:05

您正在混合 JSF 1.x 和 JSF 2.x 方法。也许您正在阅读一本针对 JSF 1.x 的过时书籍/教程,而 IDE 正在自动生成 JSF 2.x 内容?

JSF 2.0 默认不再支持 JSP。旧版 JSP 已由 Facelets 继承,它提供了更优越的模板功能。尽管您可以配置 JSF 2.0 以再次使用老式 JSP,但我强烈建议不要这样做。而是将 login.jsp 重写为 login.xhtml

<!DOCTYPE html>
<html lang="en"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Login</title>
    </head>
    <body>
    <h:form>
        Username: <h:inputText value="#{userBean.username}" />
        <br />
        Password: <h:inputText value="#{userBean.password}" />
        <br />
        <h:commandButton value="Login" action="#{userBean.getNextPage}" />
    </h:form>
</body>


与问题无关,使用 JSF 2.0,您还可以摆脱整个faces-config.xml 正如您目前所拥有的。将以下 javax.faces.bean 注释添加到 User 类:

@ManagedBean(name="userBean")
@RequestScoped
public class User {
    // ...
}

使用新的隐式导航,结果将默认被视为视图的文件名。您已经做得很好,因此您根本不需要这些导航规则。

You're intermixing JSF 1.x and JSF 2.x approaches. Maybe you're reading from an outdated book/tutorial which is targeted on JSF 1.x while the IDE is autogenerating JSF 2.x stuff?

JSF 2.0 doesn't support JSP by default anymore. The legacy JSP has been succeeded by Facelets which offers far more superior templating capabilities. Although you can configure JSF 2.0 to use the vintage JSP again, I strongly recommend to not do so. Rather rewrite login.jsp to login.xhtml:

<!DOCTYPE html>
<html lang="en"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Login</title>
    </head>
    <body>
    <h:form>
        Username: <h:inputText value="#{userBean.username}" />
        <br />
        Password: <h:inputText value="#{userBean.password}" />
        <br />
        <h:commandButton value="Login" action="#{userBean.getNextPage}" />
    </h:form>
</body>


Unrelated to the problem, with JSF 2.0 you can also get rid of the whole faces-config.xml as you currently have. Add the following javax.faces.bean annotations to the User class:

@ManagedBean(name="userBean")
@RequestScoped
public class User {
    // ...
}

With the new implicit navigation, the outcome will be by default treated as filename of the view. You've already done it fine, so you don't need those navigation rules at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文