0

JavaServer Faces in Action に記載されている例で、JSF 1.x に取り組んでいます。

コマンド ボタンで呼び出された JavaScript が機能していません。onmouseover/out イベントの両方がメソッドを実行していません。

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

構造

この例では 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="/images/submit.gif" 
                                onmouseover="set_image(this, '/images/submit_over.gif'); alert('button: ' + this );"
                                onmouseout="set_image(this, '/images/submit.gif'); alert('button: ' + this ); " 
                            />
                </h:panelGroup>

            </h:panelGrid>

        </h:panelGrid>

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

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

ブラウザ表示:

ここに画像の説明を入力

JavaScript は、宣言されたイベントを呼び出していません。

<h:commandButton  action="success" 
                                    image="/images/submit.gif" 
                                    onmouseover="set_image(this, '/images/submit_over.gif'); alert('button: ' + this );"
                                    onmouseout="set_image(this, '/images/submit.gif'); alert('button: ' + this ); " 
                                />

JSF 1.x を使用する必要があり、JSF 2 にアップグレードできません。解決策を提案し、何が問題なのかを教えてください。

以前:

質問#{facesContext} 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');" 
                            />

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

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

デバッグ結果:

デバッグ結果

式は実行時に評価されます (ただし、送信イメージは表示されませんでした)。これにより、前の問題が解消されます。したがって、ELステートメントを削除し、プロジェクト名を追加せずに相対パスに置き換えました。

4

1 に答える 1

0

私は間違いが何であるかを理解しました。

私の JavaScript コードが間違っていました。

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

img を image に変更しました:

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

その後、イベントは正常に機能し始めました。しかし、2 番目の画像 'submit_over.gif' は表示用に取得されませんでした。

次に、イベントに EL 式を追加しました。

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

これですべての問題が解決しました。

コメントと提案をありがとう。あなたの提案はうまくいきました。

于 2013-02-22T01:53:30.523 に答える