繁体   English   中英

JSF-第一步-Bean不起作用

[英]JSF - First steps - Bean doesnt work

我正在迈入JSF框架的第一步。 我这样做这些jsp / bean:

index.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
   <%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
   <%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Home</title>
    </head>
    <body>
        <f:view>
            <h:form>
                <h:outputText value="Inserisci il tuo nome" />
                <h:inputText value="#{utente.nome}" />
                <h:commandButton value="Cliccami" action="avanti" />
            </h:form>
        </f:view>
     </body>
</html>

pagina1.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login Page</title>
    </head>
    <body>
        <f:view>
            <h:form id="LoginApplication">
                <h:panelGrid id="lpg" columns="2">
                    <h:outputText value="Benvenuto nella tua prima pagina JSP " />
                    <h:outputText value="#{utente.nome}" />
                </h:panelGrid>
            </h:form>
        </f:view>
     </body>
</html>

user.java

package myPack;

public class user{
    private String nome;
    public user(){}

    public String getNome(){
        return nome;
    }
    public void setNome(String nome) {
        this.nome=nome;
    }
}

faces.config.xml

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

<!DOCTYPE faces-config PUBLIC
  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
  "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<!-- =========== FULL CONFIGURATION FILE ================================== -->

<faces-config>
    <navigation-rule>
        <from-view-id>/index.jsp</from-view-id>
        <navigation-case>
            <from-outcome>avanti</from-outcome>
            <to-view-id>/pagina1.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>

    <managed-bean>
        <managed-bean-name>utente</managed-bean-name>
        <managed-bean-class>myPack.user</managed-bean-class>

        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
</faces-config>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <context-param>
        <param-name>javax.faces.CONFIG_FILES</param-name>
        <param-value>/WEB-INF/faces-config.xml</param-value>
    </context-param>

    <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>/faces/*</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>faces/index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

我有这些问题:1-在输入文本上,单击按钮之前,我看到#{utente.nome}打印出来了(为什么?此刻我什么都没有了)。 2-当我将其发送到服务器时,什么也没有发生,并且将打印字符串“#{utente.nome}”。 为什么?

如果未将web.xml至少正确地配置为Servlet 2.4,则会发生这种情况。 EL表达式(那些#{}东西)将不会被求值。 由于您使用的是老式JSF 1.1,所以我敢打赌,您也同时使用了老式的servlet容器。 确保它支持Servlet 2.4,并确保web.xml至少声明为:

<web-app 
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

或者,如果您使用的是与Servlet 2.5兼容的容器,请确保按以下方式声明web.xml

<web-app 
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
    version="2.5">

(并考虑将JSF升级到1.2或更好的2.0)

您还需要确保您没有任何servlet-api.jar容器特定的库,例如servlet-api.jarj2ee.jarjavaee.jarjsp-api.jar等等。等等。在webapp的/WEB-INF/lib徘徊/WEB-INF/lib文件夹,甚至更糟,位于JRE/lib/ext文件夹中。 /WEB-INF/lib文件夹应仅包含JSF库(以及其他特定于Webapp本身的库)。 JRE/lib/ext文件夹应保持不变。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM