开发者

What's the Struts 2 equivalent of ASP.NET's Request.Form (or FormCollection)?

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 textboxe开发者_StackOverflows? (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.


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.


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;
   }      
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜