Struts and Dynamic Fields of a Form


Web tier: servlets, JSP, Web frameworks: Struts and Dynamic Fields of a Form

  1. Struts and Dynamic Fields of a Form (5 messages)

    Hi all,
    There's a form in a struts based web-application in which some fields will be made according to some varable parameters. I mean number of those text type fields may vary from 2 to 100.
    Any idea for building ActionForm class such that fields of that formBean be set automatically (obviously after the form submitted)?

    Majid Makki

    Threaded Messages (5)

  2. Have the attribute type as String[] in form class and use that attribute in the jsp, when you submit all the posted values will be assigned in your array attribute.

    Hope this helps,
  3. And what about relation between name of the text field in JSP and name of the String[] in the form class. I mean how they will be mapped?
    I think an example can make it clear for me.

    Thanks again
    Majid Makki
  4. Basically if you have a same variable name and multiple values, http will send all values as name & value pairs. Struts will present those as String[] in your form class.

    For Example:

    <input type="text" name="varName" value="aaa" >
    <input type="text" name="varName" value="bbb" >
    <input type="text" name="varName" value="ccc" >

    In http,these values will be sent as varName=aaa&varName=bbb&varName=ccc. In your form class if you have a variable it will be populated.

    class MyForm extends ..... {

    private String varName[];

    public String[] getVarName() {}
    public void setVarName(String[] varName) {
       this.varName = varName;

    Here the getVarName() will return all values of the variable varName.

    Hope this helps,
  5. DynaActionForms[ Go to top ]

    You could use DynaActionForms
  6. Re: Dynamic Forms[ Go to top ]

    I'm sorry in bothering you, but I'm new to Struts and I'm having this problem for many days without found any working solution (for me). I have Struts 1.1 (sigh, I must use this version) and I'm trying to get some multiple/dynamic file from a form (file's number depends on a field specified by users) as posted. When I reach the page all goes well, I can put the number of the file I want to upload, I can select the file but... when I press the submit botton I get an exception. Here is my code. The jsp page: 0){ for(int i=0; i<numSubreport; i++){ String propertyName = "file"; //String propertyName = "file["+i+"]"; %> : The bean for the form has this getter setter method (plus other for get/set other params) public int getNumFile() { return numFile; } public void setNumFile(int numFile) { this.numFile = numFile; file = new FormFile[this.numFile]; for(int i=0; i<this.numFile; i++){ file[i]=null; } } public FormFile[] getFile() { return file; } public void setFile(FormFile[] formFile) { file=formFile; } The Exception I get: javax.servlet.ServletException: BeanUtils.populate at org.apache.struts.util.RequestUtils.populate( at org.apache.struts.action.RequestProcessor.processPopulate( at org.apache.struts.action.RequestProcessor.process( ... I found a similar problem also in ASF's bugzilla. It was marked as a non-bug (I have no doubt in this). If can help the id is 13075. Thanks in advance, Lisa