Chrysalis Error HandlingChrysalis supports error handling at several different levels:
Client-side validation is autogenerated, and is discussed in more detail in the chapter on Views. The other handling mechanisms are discussed below. Basic HandlingA controller method is allowed to throw any exception: public void controllerMethod() throws Exception { // Do operations that may throw exceptions ... } Alternately, you can use the controller's
public void controllerMethod() { try { // Do operations that may throw exceptions ... } catch (Exception ex) { handleError(ex); } } Any exception thrown by the controller method (including runtime
exceptions) will be caught by the Chrysalis system. Normal
processing will terminate, and the error will be forwarded to an
error page. The exception object is stored in the
The third option is simplest and preferred option. Specifying Error PagesYou can define the error page for a controller method at various levels of specificity.
Chrysalis uses the most specific error page defined. The
transfer between the controller and the error page will be a
forwarding operation
[ The error page should always be specified with the
The REFERER_PAGE Error PageThere is one special value that can be assigned to an error page. <!-- In the controller chrysalis.xml --> <config> <class name="CatalogController"> <method name="editItem" parameters="item" errorpage="REFERER_PAGE" /> </class> <!-- Other configuration data ... --> </config> If the error page is designated the "REFERER_PAGE", Chrysalis redirects error messages back the original page. It includes all the original request parameters used to invoke the original page, so that the page will render correctly. Unlike normal error handling, the redirection to the
REFERER_PAGE is a client-side redirect
[ If an error is redirected back to a form rendered using the Chrysalis custom tags, the tags will indicate errors as follows.
<jutil:printError/> Errors and the Controller LifecycleMore advanced initialization, error handling and release logic
for a controller can be managed using the controller
Controller LifecycleSimple controller invocations work only with the controller
method (and initializer for complex method parameters, if
necessary). More advanced applications may need a more
sophisticated lifecycles. The
The import java.sql.*; import org.chwf.servlet.Controller; public class JDBC_Controller extends Controller { private static final String URL = "[JDBC-URL]"; private static final String USER = "[USER ID]"; private static final String PASSWORD = "[PASSWORD]"; Connection con = null; public void start() throws SQLException { con = DriverManager.getConnection(URL, USER, PASSWORD); } // Controller methods ... public void stop() throws SQLException { if (con != null) { con.close(); con = null; } } } The Because of the way the public void start() throws SQLException { con = DriverManager.getConnection(URL, USER, PASSWORD); con.setAutoCommit(false); } // Controller methods ... public void stop() throws SQLException { if (con != null) { try { if (this.hasError()) { con.rollback(); } else { con.commit(); } } finally { con.close(); con = null; } } } These examples use JDBC, but the logic works just as well for other kinds of transactional resources, including JTA, JDO, JMS and so forth. Note that the Of course, the best way to handle this is to organize your
controller logic into classes based on common You can also do more sophisticated resource management using resource factories. Chrysalis SecurityChrysalis supports security at two levels:
Controller SecurityBy default, Chrysalis controllers do not require the user to be logged into the system. If you wish to constrain access to particular controller methods, you can do so by defining a "role" attribute for that method: <config> <class name="CatalogController"> <method name="editItem" parameters="item" role="editor" /> </class> <!-- Other configuration data ... --> </config> If a user tries to invoke a secured controller method without (a) being logged in and (b) belonging to the specified role, Chrysalis throws an exception. Chrysalis has a special "AUTHENTICATED" role, of which all users are considered a member. If you want a controller method to be accessible to anyone who is logged in, assign it the "AUTHENTICATED" role. The default Chrysalis installation uses the J2EE standard web security mechanisms to authenticate users. You must specify the authentication mechanism and set basic security constraints in the web.xml deployment descriptor as per the Servlet standard. If you wish to use another, custom security mechanism, you can
define a custom Page SecurityView pages can also be secured in Chrysalis, in the page configuration chrysalis.xml file. <config> <page name="showAccounts"> <security role="account-manager" encryption="ALWAYS" login="/login.jsp" /> </page> <page name="login"> <security encryption="ALWAYS" /> </page> <!-- Other configuration data ... --> </config> In addition to roles, a page can also be assigned a login page. Chrysalis will redirect to the login page if the user is not authenticated. If the user is authenticated and belongs to the role, Chrysalis allows access to the page. If the user is authenticated, but does not belong to the role (or if there is no login page), Chrysalis generates an error message. Finally, Chrysalis can specify encryption requirements for view pages:
The final option may seem strange, but is useful for commercial sites where a few pages need to be secure (making a purchase) but the rest should not be (general catalog browsing). The login page itself should (a) not require any roles and (b) ALWAYS have encryption. You can relax the second restriction if you do not care about userid and passwords being intercepted, but if that is the case, you might want to reconsider having security at all. Page security is primarily useful for applications that have custom security. Applications that use J2EE standard security can use the security configuration in the web.xml to achieve the same effects. Chrysalis PluginsChrysalis supports a number of plugins to custom its behavior.
These plugins must be subclasses of the abstract plugin classes
defined in the <plugin> <user class="Custom-Class-Name" /> <logger class="Custom-Class-Name" debug="true" /> <optionLister class="Custom-Class-Name" /> <scripter class="Custom-Class-Name" /> </plugin> If no custom plugins are defined, Chrysalis uses the default
plugins from the |