1

独自の ActionListener クラスを使用してコマンド ボタンを押したときに例外が発生した場合に例外をインターセプトできるように、JSFでカスタムActionListenerクラスを作成する方法はありますか

次のコードを使用してみました:

firstpage.jsp
<f:view>
  <html>
    <head>
      <title>firstpage</title>
    </head>
    <body>
      <h:form>    
        <h:commandButton id="button" value="submit"
                         actionListener="#{databasebean.fetchUserName}"/>
      </h:form>
    </body>
  </html>
</f:view>
Dao.java
package getexception;

import java.sql.*;
import java.util.ArrayList;
import javax.faces.event.ActionEvent;

public class Dao {

    String firstname;
    private Connection con=null;
    private Statement stmt = null;
    private ResultSet rst = null;
    private ArrayList ar;
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    String url = "jdbc:odbc:" + "new";
    public Dao() {

    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getFirstname() {
        return firstname;
    }
    public void fetchUserName(ActionEvent ae) throws Exception{
        Driver d= (Driver)Class.forName(driver).newInstance();
        con = DriverManager.getConnection(url,"","");
        stmt = con.createStatement();
        rst=stmt.executeQuery("select firstname from employee where firstname like '%e'");
        if(rst.next()) {
            this.setFirstname(rst.getString("firstname"));
        }
        rst.close();
        stmt.close();
        con.close();
    }}
NewActionListener.java
package getexception;

import com.sun.faces.application.ActionListenerImpl;
import javax.faces.application.Application;
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

public class NewActionListener extends ActionListenerImpl{

    public void processAction(ActionEvent event) {
    try {
        System.out.println("try block");
        super.processAction(event);

    } catch (Exception exception) {
        System.out.println("catch block");
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        NavigationHandler navigationHandler = application.getNavigationHandler();
        navigationHandler.handleNavigation(facesContext,null,"exceptionNavigation");
        facesContext.renderResponse();
        }
    }}
faces-config.xml
<faces-config xmlns="http://java.sun.com/JSF/Configuration">
  <managed-bean>
    <managed-bean-name>databasebean</managed-bean-name>
    <managed-bean-class>getexception.Dao</managed-bean-class>
    <managed-bean-scope>application</managed-bean-scope>
  </managed-bean>

  <navigation-rule>
    <navigation-case>
      <from-outcome>exceptionNavigation</from-outcome>
      <to-view-id>/error.jsp/<to-view-id>
    </navigation-case>
  </navigation-rule>

  <application>
    <action-listener>getexception.NewActionListener</action-listener>
  </application>

  <managed-bean>
    <managed-bean-name>backing_hellopage</managed-bean-name>
    <managed-bean-class>getexception.backing.Hellopage</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
  </managed-bean>
</faces-config>
error.jsp
  <html>
    <f:view>
      <head>
        <title>firstpage</title>
      </head>
      <body>
        <h:form>
          <h:outputText value="Exception succesfully navigated"/>
        </h:form>
      </body>
    </f:view>
  </html>

error.jspコードは SQL 例外を生成しましたが、ページが表示されませんでした。

例外が発生したときにエラー ページが表示されないように設定を誤ったのは何ですか?

4

4 に答える 4

3

Application.setActionListenerのjavadoc :

すべてのActionSource コンポーネントに登録されるデフォルトのActionListenerを設定します。

アプリケーションに設定されたActionListenerは、他のリスナーを呼び出す責任を負いません。actionListener属性またはf:actionListenerタグを介して設定されたものは呼び出されません。ナビゲーションケースの文字列を返すメソッドにバインドされているアクション属性の処理担当します( ActionSourceのget / setActionを参照)。

そのため、アプリケーションのリスナーは、他のリスナーからのイベントを呼び出さないため、それらをキャッチできません。

概要:

<h:commandButton action="#{bean.foo1}" actionListener="#{bean.foo2}">
    <f:actionListener type="type.Foo3" />
</h:commandButton>
  1. foo1:(メソッドpublicStringfoo1())はfaces- configActionListenerを使用します
  2. foo2:(method public void foo2(ActionEvent))はMethodExpressionActionListenerを使用します
  3. Foo3 :(クラスパブリッククラスFoo3はActionListenerを実装します)はコンポーネントに直接登録されます

余談ですが、このパターンを使用して、faces-config.xmlに登録されているActionListenerを作成することをお勧めします。

public class MyActionListener implements ActionListener {

    private final ActionListener delegate;

    public MyActionListener(ActionListener delegate) {
        this.delegate = delegate;
    }

    public void processAction(ActionEvent event)
            throws AbortProcessingException {
        delegate.processAction(event);
    }

}

JSFフレームワークがそれ自体を構成するとき、基礎となるActionListener実装をコンストラクターに渡します。これは、多くのJSFアーティファクト(工場など)に適用されます。

于 2009-01-21T12:27:58.110 に答える
1

行に不要なスラッシュ (/) があります。

<to-view-id>/error.jsp/<to-view-id>

する必要があります

<to-view-id>/error.jsp<to-view-id>
于 2010-08-19T22:22:12.413 に答える
0

私はあなたが必要だと信じています:

<from-view-id>/*</from-view-id>

ナビゲーションルールの後、ナビゲーションケースの前。

于 2010-09-07T15:36:08.193 に答える