This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not sure why I am not able to insert my data

 
howard franklin
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to struts and I am trying to put a sample application together.

I can not figure out why I an not able to insert data using the following action. Everything works except that. What happens is after I click save I get a blank screen.

Below I have included code for the following files.

AddAddressAction.java
Address.java
AddressData.java
AddressForm.java
addAddress.jsp
struts-config.xml

address table sql


table
 
Anurag Blore
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do following and check:
1. In class AddAddressAction, Print exception in catch block in method execute.
2. In class AddressData , add a catch block in your method before finally.

Above will give you where the exception is. Also check the forward mapping again as on success it will go to next page but on failure is that handled?

Thank you
 
jatan bhavsar
Ranch Hand
Posts: 299
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Howard,

Have you checked what happens once click on submit button.

is it reaching to the action class? Because sometimes it may happen that request dosent reach to the action class due to configuration issues
.Please check this and let us know.also print stack trace if you are getting execptions.

Regards
Jatan
 
Eswar Nec
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hide all your db related code,
Check : After click save button then your application goes to result page or blank page.

If you got blank page, then check your configuration.
Then you will do write further implementation code.

Here i give you one similar sample. I follow this technique


LoginAction.java



1. First you don't write any logic, Check your all configuration is correct .
That means, Either the success page is displayed or not, when click the save button
2. After that write your full logic.
 
howard franklin
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone. Remove everything from the action and found out that my code never made it there. I change the code to the following.

I am new trying to understand. thanks

package input;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import login.LoginBean;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;

public class AddAddressAction extends Action {


public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

// Default target to success
String target = new String("success");

if ( isCancelled(request) ) {

// Cancel pressed back to employee list
return (mapping.findForward("success"));
}


if (request.getParameter("add") != null)
{

/* start */


try {

Address address = new Address();
AddressForm addressform = (AddressForm) form;

address.setfName(addressform.getfName());
address.setLname(addressform.getLname());
address.setStreet(addressform.getStreet());
address.setCity(addressform.getCity());
address.setState(addressform.getState());
address.setZip(addressform.getZip());
// address.setType(addressform.getType());
// address.setUsername(addressform.getUsername());


AddressData.addAddress(address, getDataSource(request, "address"));

//= new LoginBean(getDataSource(request, "address"));
}
catch ( Exception e ) {

System.err.println("Setting target to error");
target = new String("error");
ActionErrors errors = new ActionErrors();

errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("errors.database.error", e.getMessage()));

// Report any errors
if ( !errors.isEmpty() ) {

saveErrors(request, errors);
}
}



/* end */

target = new String("add");
return (mapping.findForward("add"));




}

// Forward to the appropriate View
target = new String("missed");
return (mapping.findForward("missed"));
}
}
 
jatan bhavsar
Ranch Hand
Posts: 299
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Howard,

If still there is a problem you need to look into the configuration of action.

Regards
jatan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic