You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Gudla, Madhuri" <MG...@americanaships.com> on 2002/10/14 22:33:43 UTC

Struts customized exception handling

Below is my customised Exception handler code that extents the default 
Struts ExceptionHandler code. 
This is how i set the message key in ActionError ( struts framework will 
get the message for this messageKey from the Resourcebundle) 
error = new ActionError( messageKey ); 
My Requirement is: 
(1)When the Exception occurs, focus should go back to the page where the 
Exception occurred.(what should i have in my action mappings to accomplish 
this?) 
(2)A pop-up window needs to display the message associated with the 
messageKey 
      Right now I have sample code of the JSP, for the pop up window
displaying error messages.( page validation using struts validator).  The
code for jsp is below.    
    
(3)The userID of the person and date-time needs to appear in the pop-up 
window along with the message 
How do i accomplish this? What should my JSP code look like? 
How will the MessageTag in the JSP process the ActionError? 
Thanks for any ideas. 
package com.cpships.trident.framework.strutscustomization; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.apache.struts.action.ExceptionHandler; 
import org.apache.struts.action.ActionForm; 
import org.apache.struts.action.ActionError; 
import org.apache.struts.util.AppException; 
import org.apache.struts.action.ActionForward; 
import org.apache.struts.action.ActionMapping; 
import org.apache.struts.config.ExceptionConfig; 
import com.cpships.trident.framework.exception.TridentChainedException; 
public class TridentExceptionHandler extends ExceptionHandler { 

private TridentChainedException tCE = null; 

/** 
* Constructor for an external message 
* @param tce of type TridentChainedException 
* 
*/ 
public TridentExceptionHandler(TridentChainedException tce) { 
this.tCE = tce; 

} 

public ActionForward execute(Exception ex, 
ExceptionConfig config, 
ActionMapping mapping, 
ActionForm formInstance, 
HttpServletRequest request, 
HttpServletResponse response) 
throws ServletException { 
ActionForward forward = null; 
ActionError error = null; 
String property = null; 
/* Get the path for the forward either from the exception element 
* or from the input attribute. 
*/ 
String path = null; 
if (config.getPath() != null) { 
path = config.getPath(); 
}else{ 
path = mapping.getInput(); 
} 
// Construct the forward object 
forward = new ActionForward(path); 
/* Figure out what type of exception has been thrown. The Struts 
* AppException is not being used in this example. 
*/ 
if( tCE instanceof TridentChainedException) { 
// This is the specialized behavior 

Throwable t = tCE.getCause(); 
String messageKey = t.getClass().getName(); 
error = new ActionError( messageKey ); 

/* 
String messageKey = baseException.getMessageKey(); 
Object[] exArgs = baseException.getMessageArgs(); 
if ( exArgs != null && exArgs.length > 0 ){ 
// If there were args provided, use them in the ActionError 
error = new ActionError( messageKey, exArgs ); 
}else{ 
// Create an ActionError without any arguments 
error = new ActionError( messageKey ); 
} 
*/ 
} 
// Store the ActionError into the proper scope 
// The storeException method is defined in the parent class 
storeException(request, property, error, forward, config.getScope()); 
return forward; 
} 
} 

Code for the JSP with code for a pop up window , displaying page validation
error message:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" %>
<%@ taglib uri="struts-bean.tld"  prefix="bean" %>
<%@ taglib uri="struts-html.tld"  prefix="html" %>
<%@ taglib uri="struts-logic.tld" prefix="logic" %>
<%@ page import="org.apache.struts.action.ActionErrors" %>
<%@ page import="org.apache.struts.action.ActionError" %>
<%@ page import="org.apache.struts.action.Action" %>
<%@ page import="java.util.Iterator" %>

<html:html locale="true">

<head>
<script language="JavaScript">

	function showErrors(errors) {
		
		 alert(errors);
	}
		
		function handleError(error){
			var left = Math.floor( (screen.width - 360) / 2);
			var top = Math.floor( (screen.height - 450) / 2);
var
errW=window.open('','Error','dependent=yes,width=360,height=450,left='+left+
',top='+top+',titlebar=no,scrollbars=yes,resizable=1')
var Message="<html><head><title>ERROR</title></head><body
bgcolor='white'><"+"script language='JavaScript'>function keepFocused() {
self.focus();
setTimeout('keepFocused()',100);}keepFocused();<"+"/script><b>"+error+"</b><
center><input type='button' onclick='self.close()'
value='OK'></center></body></html>";
errW.document.write(Message)
}

	
</script>
   <title><bean:message key="index.title"/></title>
	<META name="GENERATOR" content="IBM WebSphere Studio">
</head>


<logic:messagesPresent>
	<body bgcolor="white" onload ='handleError("<bean:message
key='errors.header'/><ul><html:messages id='error'><li><bean:write
name='error'/></li></html:messages></ul>")'>
</logic:messagesPresent>

<logic:messagesNotPresent>
	<body bgcolor="white">
</logic:messagesNotPresent>

<h2>Create a book</h2>

<html:form action="createBook.do" method="GET"> 

    Title:<html:text property="title" /> <br/>
    Pages:<html:text property="pages" /> <br/>
    Date:<html:text property="date" /> <br/>
    <html:submit property="submit"/>
</html:form>

</body>
</html:html>       









--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>