java - #{facesContext} EL 表达式在运行时未解析

标签 java jsp jsf el jsf-1.2

我正在研究 JSF 1.x 关于 JavaServer Faces in Action 中所述的示例。

我的“Faces Context”的 EL 表达式在检索图像时未在运行时在命令按钮中执行。

我的项目结构如下:

Structure

我在这个例子中没有使用任何 Java 代码, 而且它只包含一个登录页面。

login.jsp如下:

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>


<f:view>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="stylesheet.css" />

    <script type="text/javascript">
        function set_image(button, image){
            button.src = img;
        }
    </script>

    <title><h:outputText value="ProjectTrack" /></title>
</head>

<body>

    <h:form>
        <h:panelGrid columns="2" border="0" cellpadding="3" cellspacing="3">

            <h:graphicImage url="/images/logo.png"
                    alt="Welcome to ProjectTrack" title="Welcome to ProjectTrack"
                    width="149" height="160" />


            <h:panelGrid columns="3" border="0" cellpadding="5" cellspacing="3" headerClass="login-heading">

                <f:facet name="header">
                    <h:outputText value="ProjectTrack" />
                </f:facet>

                <h:outputLabel for="userNameInput" >
                    <h:outputText value="Enter your user name: " />
                </h:outputLabel>

                <h:inputText id="userNameInput" size="20" maxlength="30" required="true">
                    <f:validateLength minimum="5" maximum="30"/>
                </h:inputText>

                <h:message for="userNameInput" />

                <h:outputLabel for="passwordInput">
                    <h:outputText value="Password"/>
                </h:outputLabel>

                <h:inputSecret id="passwordInput" size="20" maxlength="20" required="true">
                    <f:validateLength minimum="5" maximum="15" />
                </h:inputSecret>

                <h:message for="passwordInput" />

                <h:panelGroup>
                    <h:commandButton  action="success" 
                                image="#{facesContext.externalContext.requestContextPath}/images/submit.gif" 
                                onmouseover="set_image(this, '#{facesContext.externalContext.requestContextPath}/images/submit_over.gif');"
                                onmouseout="set_image(this, '#{facesContext.externalContext.requestContextPath}/images/submit.gif');" 
                            />
                </h:panelGroup>

            </h:panelGrid>

        </h:panelGrid>
    </h:form>
</body>
</f:view>
    </html>

我的部署描述符web.xml如下:

<web-app version="2.5"
      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-app_2_5.xsd">

    <display-name>Project Track</display-name>
    <description>Sample Project</description>

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

    <welcome-file-list>
        <welcome-file>faces/login.jsp</welcome-file>
    </welcome-file-list>


</web-app>

Faces-config.xml如下:

<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
     "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
     "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">

<faces-config>

    <application>
        <message-bundle>ptrackResources</message-bundle>
    </application>

    <navigation-rule>
        <from-view-id>/login.jsp</from-view-id>
        <navigation-case>
            <from-outcome>success</from-outcome>
            <to-view-id>/inbox.jsp</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-outcome>failure</from-outcome>
            <to-view-id>/login.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>

</faces-config>

我的Maven依赖如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.achutha.labs</groupId>
  <artifactId>03JsfExample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>03JsfExample</name>
  <description>Project Track</description>

  <dependencies>

        <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>1.2_14</version>
        </dependency>
        <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>1.2_14</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
        </dependency>

        <!-- EL -->
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>el-impl</artifactId>
            <version>2.2</version>
        </dependency>

    </dependencies>


    <build>
        <finalName>JavaServerFaces</finalName>

        <plugins>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>tomcat-maven-plugin</artifactId>
                <configuration>
                    <url>http://localhost:8090/manager/text</url>
                    <server>TomcatServer</server>
                    <path>/balaji</path>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>



</project>

应用程序运行成功。但是提交按钮的图像没有显示,它是通过运行时表达式检索的。

浏览器显示:

Browser Display

我已经配置了我的 JBoss 工具,从中我可以看到评估表达式并在部署前显示提交按钮的预览:

eclipse 屏幕截图:

Eclipse Screen Shots

中使用的 EL 表达式未在运行时求值。

<h:commandButton  action="success" 
                                image="#{facesContext.externalContext.requestContextPath}/images/submit.gif" 
                                onmouseover="set_image(this, '#{facesContext.externalContext.requestContextPath}/images/submit_over.gif');"
                                onmouseout="set_image(this, '#{facesContext.externalContext.requestContextPath}/images/submit.gif');" 
                            />

我必须使用 JSF 1.x,无法将其升级到 JSF 2。 请给我建议解决方案,并帮助我了解出了什么问题。

我的问题类似于此链接中所述的问题: FacesContext not resolved at run-time

编辑:

我刚刚在login.jsp中添加了一条Debug语句,如下:

<h:outputText value=" Debug test for EL exp : #{facesContext.externalContext.requestContextPath}/images/submit.gif" />

调试结果:

Debug Result

表达式在运行时被评估, 但是我感觉结果一定不能包含/balaji/images/submit.gif, 它应该只包含 /images/submit.gif

最佳答案

隐式 EL 变量 #{facesContext}仅在 JSF2 中可用。速记 #{request}正如评论中所建议的那样,仅在 Facelets 中可用。

您在 JSP 上使用 JSF1。你应该使用 ${pageContext.request.contextPath}相反。


与具体问题无关<h:commandButton image>已经是上下文相关的。你不需要在那里指定它。它仅对您的 JS 鼠标悬停/移出功能是强制性的。

关于java - #{facesContext} EL 表达式在运行时未解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15002141/

相关文章:

java - 在 OpenGL ES(在 Android 上)中创建复杂 3D 绘图的方法?

java - 使用 WebLogic 解析 XML 时出现 ClassCastException

java - 通过 servlet 在 jsp 文件中显示购物车中的商品

java - jsp的form标签中modelattribute的使用

java - 如何在android中设置 "onItemSelected()"函数的延迟?

java - @Async, future ,AsyncResult; "awaiting"完成异步函数有哪些方法

java - 从 Java 获取数据到 JSP

java - 服务层 EJB 空指针异常

java - 使用 JBoss 71 的替代 JSF 实现

java - 如何在Web应用程序中重新加载资源包?