Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to store the data entered by user in a form into a database using struts

 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I developed an application that contains multiple forms. After entering the data, Again am just displaying the data which was entered by user as output, but am not storing in any database. If i want to store the data into database, where the jdbc code should be written in my application? Here am posting my application.. Please check it..

Thanks in Advance,
Lalitha

DetailsForm.java
------------
public class DetailsForm extends ActionForm{

// properties
String name;
int age;
String city;
String phone;

//setters and getters
}
FirstAction.java
---------

public class FirstAction extends Action{

public ActionForward execute(4parameters)

{

String forward = "firstpage";
if(request.getParameter("store") != null){
forward = "secondpage";
}

return mapping.findForward(forward);
}

}

SecondAction.java
-----------
public class SecondAction extends Action{

public ActionForward execute(4parameters)
{

String forward = "secondpage";
if(request.getParameter("submit") != null){
forward = "lastpage";
}
return mapping.findForward(forward);
}
}

LastAction.java
--------
public class LastAction extends Action{

public ActionForward execute(4parameters)
{

return mapping.findForward("lastpage");

}

}



firstjsp.jsp
---------
// code to create a form that contains name and age fields n submit button

<html:form action="/firstpage" method="post">

<html:text property="name" />

<html:password property="age" />

<html:submit property="store"/> <html:reset/>

secondjsp.jsp
-------

<html:form action="/secondpage" method="post">

<html:text property="city" />

<html:password property="phone" />

<html:hidden property="name" />

<html:hidden property="age" />

<html:submit property="submit"/>


lastjsp.jsp
---------

// just displaying the user entered data

Name = <bean:write name="detailsForm" property="name" />
Age = <bean:write name="detailsForm" property="age" />
City = <bean:write name="detailsForm" property="city" />
Phone = <bean:write name="detailsForm" property="phone" />


struts-config.xml
---------------
<action-mappings>
<action input="/firstjsp.jsp" path="/firstpage" name="detailsForm" type="example.actionclasses.FirstAction" scope="request">
<forward name="firstpage" path="/firstjsp.jsp" />
<forward name="secondpage" path="/secondjsp.jsp" />
</action>
<action input="/secondjsp.jsp" path="/secondpage" name="detailsForm" type="example.actionclasses.SecondAction" scope="request">
<forward name="secondpage" path="/secondjsp.jsp" />
<forward name="lastpage" path="/lastjsp.jsp" />
</action>
<action input="/lastjsp.jsp" path="/lastpage" name="detailsForm" type="example.actionclasses.LastAction" scope="request">
<forward name="lastpage" path="/lastjsp.jsp" />
</action>
</action-mappings>




 
Joe Ess
Bartender
Posts: 9280
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Struts Documentation details the best practices on How To Access A Database.
 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Actually i know the code to be written to access a database.. but my problem is, in my application i tried by writing the code in LastAction.java class, but it is not storing the details in db..
 
Joe Ess
Bartender
Posts: 9280
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lalitha Kotha wrote: but my problem is,..


How was I to know that from your original post? Have a look at our FAQ, HowToAskQuestionsOnJavaRanch. The better question you ask, the more help we can be.
Are you sure that LastAction is ever called?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic