3

私はJSF1.xに取り組んでいます。JavaServerFacesinActionで述べられている例についてです。

「FacesContext」のEL式が、実行時に画像を取得する際のコマンドボタンで実行されていません。

私のプロジェクト構造は次のとおりです。

構造

この例ではJavaコードを使用していません。また、ログインページは1つしか含まれていません。

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>

アプリケーションは正常に実行されました。ただし、実行時式を介して取得された[送信用の画像]ボタンは表示されません。

ブラウザの表示:

ブラウザ表示

JBossツールを構成しました。このツールから、式を評価し、デプロイ前に送信ボタンを表示するプレビューを確認できます。

Eclipseのスクリーンショット:

Eclipseのスクリーンショット

で使用される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が解決されない

編集:

login.jsp次のようなDebugステートメントを追加しました。

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

デバッグ結果:

デバッグ結果

式は実行時に評価されますが、結果にはが含まれていてはならず、含まれ/balaji/images/submit.gifている必要があるのは。だけであると思います/images/submit.gif

4

2 に答える 2

3

暗黙のEL変数#{facesContext}は、JSF2でのみ使用できます。コメントで提案されている省略形#{request}は、Faceletsでのみ使用できます。

JSPでJSF1を使用しています。${pageContext.request.contextPath}代わりに使用する必要があります。


具体的な問題とは関係なく、<h:commandButton image>はすでにコンテキストに関連しています。そこに指定する必要はありません。これは、JSのマウスオーバー/出力機能にのみ必須です。

于 2013-02-21T13:39:42.893 に答える
1

url-pattern含むを設定しました/faces/*。このパターンを使用して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>
于 2013-02-21T13:20:05.143 に答える