• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use struts to display initial data in JSP

 
Nuwan Priyadarshana
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to struts, in my application each JSP page display data when initially load the JSP page, so how can I use struts to do that?
 
rakshini nithya
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package com.example;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

public final class SubmitAction extends Action
{

public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
String param = mapping.getParameter();
if( param.equalsIgnoreCase("load") )
{
SubmitForm submitform = (SubmitForm) form;
submitform.setFirstName("Kamesh");
submitform.setLastName("loganathan");

AddressBean addressBean = submitform.getAddressBean();

addressBean.setStreet1("Taramani,");
addressBean.setStreet2("Tidel Park");
addressBean.setCity("Chennai");
addressBean.setState("
 
rakshini nithya
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package com.example;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

public final class SubmitAction extends Action
{

public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
String param = mapping.getParameter();
if( param.equalsIgnoreCase("load") )
{
SubmitForm submitform = (SubmitForm) form;
submitform.setFirstName("Kamesh");
submitform.setLastName("loganathan");

AddressBean addressBean = submitform.getAddressBean();

addressBean.setStreet1("Taramani,");
addressBean.setStreet2("Tidel Park");
addressBean.setCity("Chennai");
addressBean.setState("TN");

PhoneBean phoneBean1 = new PhoneBean();
phoneBean1.setAreaCode("044");
phoneBean1.setPhone("24753044");
phoneBean1.setMobile("9884487517");
PhoneBean phoneBean2 = new PhoneBean();
phoneBean2.setAreaCode("2044");
phoneBean2.setPhone("224753044");
phoneBean2.setMobile("29884487517");
List phoneList = new ArrayList();
phoneList.add(phoneBean1);
phoneList.add(phoneBean2);

addressBean.setPhoneBeanArray(phoneList);

submitform.setAddressBean(addressBean);
}
// Forward control to the specified success target
return (mapping.findForward("success"));
}
}
ans specify the parameter in struts config.xml also like this
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config (View Source for full doctype...)>
- <struts-config>
- <!-- ========== Form Bean Definitions =================
-->
- <form-beans type="org.apache.struts.action.ActionFormBean">
<form-bean name="submitForm" type="com.example.SubmitForm" />
</form-beans>
- <!-- ========== Global forwards ============
-->
- <global-forwards type="org.apache.struts.action.ActionForward">
<forward name="first" path="/submit.jsp" />
</global-forwards>
- <!-- ========== Action Mapping Definitions ============
-->
- <action-mappings type="org.apache.struts.action.ActionMapping">
- <action path="/start" type="com.example.SubmitAction" name="submitForm" attribute="FormBean" scope="session" parameter="load">
<forward name="success" path="/submit.jsp" />
<forward name="failure" path="/submit.jsp" />
</action>
- <action path="/submit" type="com.example.SubmitAction" name="submitForm" attribute="FormBean" input="/submit.jsp" scope="session" parameter="display">
<forward name="success" path="/success.jsp" />
<forward name="failure" path="/submit.jsp" />
</action>
</action-mappings>
</struts-config>
 
Nuwan Priyadarshana
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks rakshini
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic