code_junkie

Intercepting Exceptions with custom JSF event handlers

Nov 13th, 2011
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. <f:view>
  2. <html>
  3. <head>
  4. <title>firstpage</title>
  5. </head>
  6. <body>
  7. <h:form>
  8. <h:commandButton id="button" value="submit"
  9. actionListener="#{databasebean.fetchUserName}"/>
  10. </h:form>
  11. </body>
  12. </html>
  13. </f:view>
  14.  
  15. package getexception;
  16.  
  17. import java.sql.*;
  18. import java.util.ArrayList;
  19. import javax.faces.event.ActionEvent;
  20.  
  21. public class Dao {
  22.  
  23. String firstname;
  24. private Connection con=null;
  25. private Statement stmt = null;
  26. private ResultSet rst = null;
  27. private ArrayList ar;
  28. String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
  29. String url = "jdbc:odbc:" + "new";
  30. public Dao() {
  31.  
  32. }
  33.  
  34. public void setFirstname(String firstname) {
  35. this.firstname = firstname;
  36. }
  37.  
  38. public String getFirstname() {
  39. return firstname;
  40. }
  41. public void fetchUserName(ActionEvent ae) throws Exception{
  42. Driver d= (Driver)Class.forName(driver).newInstance();
  43. con = DriverManager.getConnection(url,"","");
  44. stmt = con.createStatement();
  45. rst=stmt.executeQuery("select firstname from employee where firstname like '%e'");
  46. if(rst.next()) {
  47. this.setFirstname(rst.getString("firstname"));
  48. }
  49. rst.close();
  50. stmt.close();
  51. con.close();
  52. }}
  53.  
  54. package getexception;
  55.  
  56. import com.sun.faces.application.ActionListenerImpl;
  57. import javax.faces.application.Application;
  58. import javax.faces.application.NavigationHandler;
  59. import javax.faces.context.FacesContext;
  60. import javax.faces.event.ActionEvent;
  61.  
  62. public class NewActionListener extends ActionListenerImpl{
  63.  
  64. public void processAction(ActionEvent event) {
  65. try {
  66. System.out.println("try block");
  67. super.processAction(event);
  68.  
  69. } catch (Exception exception) {
  70. System.out.println("catch block");
  71. FacesContext facesContext = FacesContext.getCurrentInstance();
  72. Application application = facesContext.getApplication();
  73. NavigationHandler navigationHandler = application.getNavigationHandler();
  74. navigationHandler.handleNavigation(facesContext,null,"exceptionNavigation");
  75. facesContext.renderResponse();
  76. }
  77. }}
  78.  
  79. <faces-config xmlns="http://java.sun.com/JSF/Configuration">
  80. <managed-bean>
  81. <managed-bean-name>databasebean</managed-bean-name>
  82. <managed-bean-class>getexception.Dao</managed-bean-class>
  83. <managed-bean-scope>application</managed-bean-scope>
  84. </managed-bean>
  85.  
  86. <navigation-rule>
  87. <navigation-case>
  88. <from-outcome>exceptionNavigation</from-outcome>
  89. <to-view-id>/error.jsp/<to-view-id>
  90. </navigation-case>
  91. </navigation-rule>
  92.  
  93. <application>
  94. <action-listener>getexception.NewActionListener</action-listener>
  95. </application>
  96.  
  97. <managed-bean>
  98. <managed-bean-name>backing_hellopage</managed-bean-name>
  99. <managed-bean-class>getexception.backing.Hellopage</managed-bean-class>
  100. <managed-bean-scope>request</managed-bean-scope>
  101. </managed-bean>
  102. </faces-config>
  103.  
  104. <html>
  105. <f:view>
  106. <head>
  107. <title>firstpage</title>
  108. </head>
  109. <body>
  110. <h:form>
  111. <h:outputText value="Exception succesfully navigated"/>
  112. </h:form>
  113. </body>
  114. </f:view>
  115. </html>
  116.  
  117. <h:commandButton action="#{bean.foo1}" actionListener="#{bean.foo2}">
  118. <f:actionListener type="type.Foo3" />
  119. </h:commandButton>
  120.  
  121. public class MyActionListener implements ActionListener {
  122.  
  123. private final ActionListener delegate;
  124.  
  125. public MyActionListener(ActionListener delegate) {
  126. this.delegate = delegate;
  127. }
  128.  
  129. public void processAction(ActionEvent event)
  130. throws AbortProcessingException {
  131. delegate.processAction(event);
  132. }
  133.  
  134. }
  135.  
  136. <to-view-id>/error.jsp/<to-view-id>
  137.  
  138. <to-view-id>/error.jsp<to-view-id>
Add Comment
Please, Sign In to add comment