2010-07-17 66 views
0

我使用Javascript動態地將文本框添加到我的jsp頁面上的窗體。當表單提交給一個動作時,我的動作如何獲得這些文本框的值? (我使用的是Struts 2,順便說一句)。在ASP.NET中,我能夠在Form.Request/FormCollection中找到它們。是否有一個Struts 2相當於?太感謝了。什麼是ASP.NET的Request.Form(或FormCollection)的Struts 2等價物?

回答

0

在Struts2中,您可以在表單中創建bean來執行提交值。爲了創建輸入文本框,請使用<s>標籤。例如:

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

這裏loginBean是當bean傳遞給jsp頁面的時候。 Bean由變量的變量聲明和getter-setters組成。

然後在表單提交到的後端Java中,可以訪問同一個bean。 在Java中聲明getter-setter,然後可以訪問bean的屬性。

public LoginBean getLoginBean() { 
       return loginBean; 
     } 

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

公共字符串的authenticate(){ 字符串username = loginBean.getUserName();

我建議您查看開源Struts項目的源代碼。

+0

對不起,如果我沒有強調這一點,但我想知道如何獲得一個Javascript生成的文本框的價值。 – Chris 2010-07-18 00:06:12

0

這聽起來像你正試圖填充動態列表。要做到這一點,你只需要使用[N]指數語法在你的Action類的屬性名的末尾:

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類:

public class YourAction extends Action { 

    public List<String> yourCollection; 

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

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