Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Struts - Submit action, form fields are dynamic

Status
Not open for further replies.

sxjustin

Programmer
Jul 12, 2006
2
0
0
US
Hello,

I have a jsp form with a field SSN1 and a corresponding input field for SSN. I would like to provide a button "Add" which adds one more set of fields to the form (SSN2 text field and it's corresponding input field). If they click on "Add" again, another set will be added(SSN3, it's value field) and so on.

After the user enters required no.of SSNs, the form needs to be submitted by clicking another button "Submit". The question is, here we don't know the form size or fields inadvance. Only after the user clicks on submit, we know how many fields (SSNs with values) are there in the form.

Is there a way that we can handle this sitution in Struts ?

I really appreciate your help.

Thanks,
Justin
 
Hi

In two words : use array. In more words :

[highlight lime]struts-config.xml[/highlight]
Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "[URL unfurl="true"]http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">[/URL]

<struts-config>

  <form-beans>
    <form-bean type="stranger.ArrayFormBean" name="ArrayFormBean"/>
  </form-beans>

  <action-mappings>
    <action path="/stranger/array" type="stranger.ArrayAction" name="ArrayFormBean" scope="request" input="/stranger/arrayform.jsp">
      <forward name="success" path="/stranger/arrayresult.jsp" />
    </action>
  </action-mappings>

</struts-config>
[highlight lime]ArrayAction.java[/highlight]
Code:
package stranger;

import java.util.ArrayList;
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.ActionMapping;
import org.apache.struts.action.ActionForward;

public class ArrayAction extends Action
{
  public ActionForward execute(ActionMapping mapping, ActionForm  form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    ArrayFormBean fb=(ArrayFormBean) form;
    ArrayList list;
    int count;
    
    count=fb.getName().length;
    list=new ArrayList();
    for (int i=0;i<count;i++) list.add(fb.getName()[i]);

    request.setAttribute("count",new Integer(count));
    request.setAttribute("list",list);
      
    return mapping.findForward("success");
  }
}
[highlight lime]ArrayFormBean.java[/highlight]
Code:
package stranger;

import javax.servlet.http.HttpServletRequest;

public class ArrayFormBean extends org.apache.struts.action.ActionForm
{
  private String name[];
   
  public String[] getName() { return name; }
  public void setName(String string[]) { name = string; }
}
[highlight lime]stranger/arrayform.jsp[/highlight]
Code:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="[URL unfurl="true"]http://java.sun.com/jsp/jstl/core"[/URL] prefix="c"%> 
<html>
<body>

<form action="array.do">

<c:set var="count" value="3"/>
<c:if test="${param.count>0}"><c:set var="count" value="${param.count}"/></c:if>
<c:forEach var="i" begin="1" end="${count}">
${i}) <input type="text" name="name[]"><br>
</c:forEach>

<input type="submit" value="Ok">

</form>

</body>
</html>
[highlight lime]stranger/arrayresult.jsp[/highlight]
Code:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="[URL unfurl="true"]http://java.sun.com/jsp/jstl/core"[/URL] prefix="c"%> 
<html>
<body>

You submitted ${count} names :

<ul>
<c:forEach items="${list}" var="one">
<li>${one}</li>
</c:forEach>
</ul>

</body>
</html>
Note, that I did not implemented JavaScript. Simply request arrayform.jsp by passing the count in query string like this :
[navy][ignore][/ignore][/navy]

Feherke.
 

Hi Feherke

Thank you so much for taking time to answer this.

I will try this.

> Justin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top