2013-02-21 21 views
3

我正在JSF 1.x上工作 關於JavaServer Faces in Action中所述的示例。#{facesContext}在運行時未解析EL表達式

在檢索圖像時,我的EL表達式'Faces Context'未在運行時在命令按鈕中執行。

我的項目結構如下:

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主要內容如下:

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

調試結果:

Debug Result

表達在運行時, 得到評價,但我覺得結果不能包含/balaji/images/submit.gif, 它應該只包含/images/submit.gif

+0

您是否用'#{request.contextPath}'來嘗試? – 2013-02-21 12:28:14

+0

您使用哪個URL訪問該頁面? – 2013-02-21 12:48:32

+0

http:// localhost:8090/balaji 我正在使用Tomcat 7服務器。 – bali208 2013-02-21 13:04:31

回答

3

隱含EL變量#{facesContext}僅在JSF2可用。評論中建議的簡寫#{request}僅適用於Facelets。

您在JSP上使用JSF1。您應該使用${pageContext.request.contextPath}來代替。


無關到具體問題,<h:commandButton image>已經是上下文相關。你不需要在那裏指定它。它只對您的JS鼠標懸停/彈出功能是必需的。

+0

@baluc, 感謝您的評論。 我不敢說舊的EL工作正常, 請檢查編輯的問題。 但我正面臨一個新問題。 我刪除了所有行中的EL短語, 我收到了提交按鈕。 但是,它沒有執行JavaScript,圖像也沒有改變。 我還爲onmouseover,onmouseout事件添加了一個alrt函數。 它甚至不執行警報。 請幫我解決它。 – bali208 2013-02-21 13:55:35

+0

@baluc ... 多數民衆贊成那是對的,你在我發表評論之前就想出了問題。 如何解決這個問題? – bali208 2013-02-21 14:00:12

1

您已經設置了包含/faces/*url-pattern,您必須使用此模式訪問您的JSF頁面或更改爲其他模式。其它圖案的 示例:

<servlet-mapping> 
    <servlet-name>Faces Servlet</servlet-name> 
    <url-pattern>*.face</url-pattern> 
    </servlet-mapping> 
    <context-param> 
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name> 
    <param-value>.xhtml</param-value> 
    </context-param> 
    <security-constraint> 
    <display-name>Disallow direct access to the XHTML Documents</display-name> 
    <web-resource-collection> 
     <web-resource-name>XHTML</web-resource-name> 
     <url-pattern>*.xhtml</url-pattern> 
    </web-resource-collection> 
    <auth-constraint /> 
    </security-constraint> 
+1

如果未調用'FacesServlet',則OP根本沒有看到輸出標籤和輸入文本。 – BalusC 2013-02-21 13:37:03

+0

這是有道理的,我從來沒有使用JSF 1與JSP – 2013-02-21 13:46:26

+0

@Christophe Roussy, 表示工作正常。普萊茲看看我編輯的問題上添加的調試。 現在的問題是在JavaScript中。 請幫我解決與執行我的JS的onmouseover和onmouseout事件有關的新問題。 – bali208 2013-02-21 13:58:34