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>

该代码生成了一个 SQL 异常,但它没有向我显示error.jsp页面。

我有什么错误配置导致异常触发时错误页面不显示?

4

4 回答 4

3

Application.setActionListener的 javadoc :

设置要为所有ActionSource 组件注册的默认 ActionListener。

应用程序上设置的ActionListener不负责调用其他侦听器。它不会调用通过actionListener属性或f:actionListener标签设置的任何内容。负责处理action属性,该属性绑定到返回导航案例字符串的方法(请参阅ActionSource上的 get/setAction )。

因此,应用程序上的侦听器无法捕获来自其他侦听器的事件,因为它不调用它们。

概括:

<h:commandButton action="#{bean.foo1}" actionListener="#{bean.foo2}">
    <f:actionListener type="type.Foo3" />
</h:commandButton>
  1. foo1: (method public String foo1() ) 使用 faces-config ActionListener
  2. foo2: (method public void foo2(ActionEvent) ) 使用MethodExpressionActionListener
  3. Foo3:(class public class Foo3 implements 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

在这里你可以找到,如何实现一个动作监听器

在这里,如何在组件上注册一个侦听器(甚至是 commandButton 的示例)。

于 2009-01-21T06:09:33.960 回答
0

我相信你需要:

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

在导航规则之后和导航案例之前。

于 2010-09-07T15:36:08.193 回答