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);
}
}

