Forwarding to error.jsp

The Action class should forward to the error.jsp after getting an error message. I purposely turned off the mySQL to get errors but it looks like it's not working for some reason. I looked at every code, it looks fine and should be working.

I think it might be related to ActionMessages since I'm looking at other work and it has that syntaxes. I changed them to ActionMessage from ActionError. Or, the exception chaining? I'm fairly new to this, but it looks straightforward to me.

Here's the info -

struts file - I already placed the<forward name="error" path="/error.jsp"/>

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"

"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>

<form-beans>

<form-bean name="sheetsForm" type="com.refsheets.struts.SheetsForm"/>

<form-bean name="tempsheets" type="com.refsheets.struts.Tempsheets"/>

</form-beans>

<global-exceptions>

</global-exceptions>

<global-forwards>

<forward name="error" path="/error.jsp"/>

</global-forwards>

<action-mappings>

<action input="index.jsp" name="sheetsForm" path="/search" scope="request" type="com.refsheets.struts.SearchAction">

<forward name="success" path="/result.jsp"/>

</action>

<action input="/addsheet.jsp" name="sheetsForm" path="/addsheet" scope="request" type="com.refsheets.struts.AddAction">

<forward name="success" path="/addresult.jsp"/>

</action>

</action-mappings>

<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>

<message-resources parameter="resources.application"/>

<!-- ========================= Tiles plugin ===============================-->

<!--

This plugin initialize Tiles definition factory. This later can takes some

parameters explained here after. The plugin first read parameters from

web.xml, thenoverload them with parameters defined here. All parameters

are optional.

The plugin should be declared in each struts-config file.

- definitions-config: (optional)

Specify configuration file names. There can be several comma

separated file names (default: ? )

- moduleAware: (optional - struts1.1)

Specifyif the Tiles definition factory is module aware. Iftrue

(default), there will be one factoryfor each Struts module.

If false, there will be one common factoryfor all module. Inthis

later case, it is still needed to declare one plugin per module.

The factory will be initialized with parameters found in the first

initialized plugin (generally the one associated with thedefault

module).

true : One factory per module. (default)

false : one single shared factoryfor all modules

- definitions-parser-validate: (optional)

Specifyif xml parser should validate the Tiles configuration file.

true : validate. DTD should be specified in file header (default)

false : no validation

Paths found in Tiles definitions are relative to the main context.

-->

<plug-in className="org.apache.struts.tiles.TilesPlugin" >

<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />

<set-property property="moduleAware" value="true" />

</plug-in>

<!-- ========================= Validator plugin ================================= -->

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">

<set-property

property="pathnames"

value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>

</plug-in>

</struts-config>

The action class - this has two choices to forward - one to addresult.jsp for "success" linked to struts. Other one is "error" linked to the global forward.

package com.refsheets.struts;

import com.refsheets.exceptions.DAOException;

import javax.servlet.http.*;

import com.refsheets.daos.*;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMessage;

publicclass AddActionextends Action{

/* forward name="success" path="" */

privatefinalstatic String SUCCESS ="success";

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception{

ActionErrors errors =new ActionErrors();

DAOFactory TomcatFactory =

DAOFactory.getDAOFactory(DAOFactory.TOMCAT);

ClientDAO clientdao = TomcatFactory.getClientDAO();

try{

SheetsForm sf = (SheetsForm) form;

String name = sf.getName();

clientdao.createTest(name);

}

catch (DAOException e){

// Message for the user:

String message ="Sheet cannot be created.";

errors.add("label",new ActionMessage("error.createfailed"));

saveMessages(request, errors);

// Save the chained exceptions:

request.setAttribute("MYEXCEPTION",new DAOException(message, e));

// Return to the error page

return (mapping.findForward("error"));

}

return (mapping.findForward(SUCCESS));

}

}

DAOException - chaining exceptions

package com.refsheets.exceptions;

publicclass DAOExceptionextends Exception{

public DAOException(){

super();

}

public DAOException(String arg0){

super(arg0);

}

public DAOException(Throwable arg0){

super(arg0);

}

public DAOException(String arg0, Throwable arg1){

super(arg0, arg1);

}

}

[9417 byte] By [Meepa] at [2007-11-27 11:20:27]
# 1

What exactly is the problem you are trying to solve?

I am presuming it "doesn't work"

What currently happens with this setup?

Does it go to the success page?

Does it go to the standard error page?

What (if any) exceptions are being thrown by your DAO layer?

Is your exception handling block being invoked?

evnafetsa at 2007-7-29 14:42:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

What exactly is the problem you are trying to solve?

I am presuming it "doesn't work"

What currently happens with this setup?

It forwards to addresult.jsp which is success.

Does it go to the success page?

Yes.

Does it go to the standard error page?

It should be, it should get SQLException error via DAOException.

What (if any) exceptions are being thrown by your DAO layer?

Is your exception handling block being invoked?

SQLException, and NamingExpection.

I think it's session that I didn't add to struts. I'm reading the struts, and I spotted the scope of struts are Request, Session, Application and Page. The book doesn't really explain how to encapsule the session. I looked at the example in website, it has session. The logic in this isn't cleared in my mind, I'm trying to figure out why session is going to get JDNI to get the MySQL db then do a sql operation.

Meepa at 2007-7-29 14:42:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Ok, so it is going to the success page.

To me that would indicate that no DAOException is being thrown/caught.

Put some logging statements in and see if you can trace the flow of control.

Obviously you expect an error to be thrown. Where do you expect that error to be thrown from? Does it actually throw that error?

It is possible you are getting a runtime exception (Null pointer?) somewhere that causes it to fail?

evnafetsa at 2007-7-29 14:42:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

I see. I didn't turn MySQL DB on so I expected SQLException return some connection error.

For the logging statements, I'm working on that, it is confusing since lot of options for logging the statements - logj4, commons, and system.out.println.

How do you trace the flow of control? I'm working on logj4 now, I placed the logj4.properties under WEB-INF/classes. I am not sure about the implementation of this logj4.properties to get the debugging stuff. From what I know is to put appender to the root logger. Last time, I did, it returned all debugging via tomcat, and classes so it was hard to read. I'm trying to get debug on the packages only and tomcat will be only warn/or fatal. Any suggestions? I'm looking for the examples of this.

I think it should throw under this method. I ran this code, and didn't see any errors in tomcat console. I'm not sure if e.printStackTrace(); will work. I think I prefer all exceptions should be logged in console, rather than going to JSP unless it's about validating stuff. I was trying to get non-users-errors in JSP, it looks like it's lot of work to get together then separate them for production.

public static Connection createConnection() {

try {

Context initCtx = new InitialContext();

Context envCtx;

envCtx = (Context) initCtx.lookup("java:comp/env");

ds = (DataSource) envCtx.lookup("jdbc/refsheets");

con = ds.getConnection();

}

catch (Exception e) {

e.printStackTrace();

}

return con;

}

Meepa at 2007-7-29 14:42:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

I found the error through logj4 statements. It took me some time to understand how logj4 work.

It's ps.executeUpdate(); I overlooked this example. Thank you.

Meepa at 2007-7-29 14:42:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...