Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

How to use struts to display initial data in JSP

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks rakshini
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic