views:

71

answers:

2

I'm dynamically adding textboxes to a form on my jsp page using Javascript. When that form is submitted to an action, how does my action get the values of those textboxes? (I'm using Struts 2, btw.) In ASP.NET, I was able to find them in Form.Request/FormCollection. Is there a Struts 2 equivalent? Thanks a million.

A: 

In Struts2, you create beans in the form to do submit values. In order to create the input text-box, use the <s> tag. For example :

<s:textfield name="loginBean.userName" label="UserName" required="true" />

Here loginBean is the bean passed to the jsp page when. Bean consists of variable declarations and getters-setters for the variable.

Then in the back-end Java where the form is submitted to, you can access the same bean. Declare getter-setter in Java and then you can access the properties of the bean.

 public LoginBean getLoginBean() {
                return loginBean;
        }

        public void setLoginBean(LoginBean loginBean) {
                this.loginBean = loginBean;
        }

public String authenticate() { String username = loginBean.getUserName();

I would recommend looking at source codes of open-source Struts projects.

Hrishi
Sorry if I didn't emphasize this, but I would like to know specifically how to get the value of a Javascript-generated textbox.
Chris
A: 

It sounds like you're trying to populate a dynamic list. To do that, you just have to use the [n] index syntax at the end of your Action class property name:

HTML:

<input type="text" name="yourCollection[0]" value="first value" />
<input type="text" name="yourCollection[1]" value="second value" />
<input type="text" name="yourCollection[2]" value="third value" />

Action Class:

public class YourAction extends Action {

   public List<String> yourCollection;

   public List<String> getYourCollection(){
       return yourCollection;
   }  

   public void setYourCollection(List<String> aCollection){
       this.yourCollection = aCollection;
   }      
}
Pat