<html:errors> in JSP

This is strange, I am working with the DAOS, business object, and struts.

The purpose of this is type a string and click submit, it should be adding a field in test table in mySQL.

The problem is when I click on it, and it'll forward to the success page. I checked the database, it didn't add. I am not getting any error messages in JSP. So, it's really hard for me to debug whatever it is. I made exceptions and properties. I placed the <html:errors/> in the submit page. It stills forward success page, and I didn't see any error messages in success page. I had a small example of getting SQL exceptions in JSP. It was really helpful for me to debug this. So I'm not sure how I can get exception messages from struts. Examples of those will be nice.

A big picture of this is fuzzy in my mind.

-I'm not sure how will the ActionError sends a message to the properties. Then the <html:errors> messages will show up. Here's the files that I'm working on.

AddAction - a struts Action

publicclass AddActionextends Action{

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

privatefinalstatic String SUCCESS ="success";

protectedstaticfinal String FAILURE ="failure";

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception{

ActionErrors errors =new ActionErrors();

try{

ClientBO clientBO =new ClientBO();

clientBO.addSheet((SheetsForm)form);

return mapping.findForward(SUCCESS);

}

catch (Throwable e){

e.printStackTrace();

ActionMessage error =new ActionMessage(e.getMessage());

errors.add(ActionErrors.GLOBAL_MESSAGE, error);

}

saveMessages(request,errors);

returnnew ActionForward(mapping.getInput());

}

}

This is a submit page.

<%@page contentType="text/html"%>

<%@page pageEncoding="UTF-8"%>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html:errors/>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<SCRIPT language="JavaScript" src="/js/ajax.js"></SCRIPT>

<title></title>

</head>

<body onload="focusIn();">

<html:form action="addsheet" enctype="multipart/form-data" method="post">

<table border="1">

<tr>

<td>Sheet:</td>

<td><html:text property="name" size="15"/></td>

</tr>

<tr>

<td></td>

<td><html:submit property="submit" value="Submit"/></td>

</tr>

</tbody>

</table>

</html:form>

</body>

</html>

[4546 byte] By [Meepa] at [2007-11-27 11:08:32]
# 1

put <html:errors/> just before <html:form> tag. NOT WITHIN HEAD

<body>

<html:errors/>

<html:form>

...................

</html:form>

</body>

skp71a at 2007-7-29 13:29:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

If it is forwarding to the success page, then obviously the code is succeeding and going to the forward "SUCCESS".

If it is not saving anything in the database, and you expect it to, I would take a look at the clientBO.addSheet() method and what it is doing there.

Can you step through/debug it with an IDE (Eclipse/Netbeans)?

Maybe some logging/System.out.println statements to the console would help determine what is happening? Even just which path it takes through your code - or even if your code runs at all.

evnafetsa at 2007-7-29 13:29:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

I didn't know that it's possible to have system.out.println in netbean console. I'll check on it. I'm still getting success page. Thanks, I'll back to you if I find something.

Meepa at 2007-7-29 13:29:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

I tried adding system.out.println in ClientBO.java, it states it's an unreachable statement. I'm trying to get debugging stuff in jsp, so I can debug stuff, and then when it'll be in production, I want to remove it, somehow to avoid the users to see it. any solutions for that? I tried looking at logj4 and ActionErrors /Exception chaining but I'm not sure if it is a right solution.

Here's the code -

package com.refsheets.bo;

import com.refsheets.daos.TomcatClientDAO;

import com.refsheets.daos.TomcatDAOFactory;

import com.refsheets.exceptions.ClientException;

import com.refsheets.struts.*;

import java.sql.Connection;

import java.sql.SQLException;

public class ClientBO {

private Connection con;

/** Creates a new instance of ClientBO */

public ClientBO() {

con = TomcatDAOFactory.createConnection();

}

public void addSheet(SheetsForm sheetsform) throws ClientException {

Connection con = null;

try {

TomcatClientDAO tomcatclientdao = new TomcatClientDAO(con);

tomcatclientdao.createTest(sheetsform);

con.commit();

} catch (Exception e) {

try {

if (con != null) {

con.rollback();

throw new ClientException(e.getMessage());

}

} catch (SQLException sqle) {

e.printStackTrace();

throw new RuntimeException("error.unexpected");

}

} finally {

try {

if (con != null) {

con.close();

}

} catch (SQLException sqle) {

sqle.printStackTrace();

throw new RuntimeException("error.unexpected");

}

}

}

}

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

Hi, are you sure your Data Access code is commiting the data to the database? For instance, I know with a lot of data access libraries (jdbc, hibernate, etc...) you have to either explicitly commit using a provided function or set it up to auto-commit.

Just a thought.

CraigTataryna at 2007-7-29 13:29:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

I'm using JDNI tomcat, I typed the code in JSP and it worked fine. But it looks like something's wrong in DAOS, or Struts. My main purpose to get the errors in the JSP so I can debug. Any suggestions?

Meepa at 2007-7-29 13:29:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

The question is bothering me for while, how can I pass the exception (any exceptions in java) to struts then pass the errors in JSP. That's what I'm looking for, I'm reading struts, and JSP books to fit the puzzle together.

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