2013-01-14 53 views
0

在struts2動作中,注入用於設置動作類的屬性值,因此屬性通過表單提交中的表單字段進行更新。爲了發現提交的表單,我創建了一個調用isFormSubmitted()的方法,並在那裏檢查爲此動機創建的冗餘屬性。該屬性在隱藏字段中更新。但我覺得這種鍛鍊太髒了!我認爲必須有更好的方法來解決這個問題。如何在struts動作中提交表單以提交表單

我要做的就是:

<s:form name="form1"> 
    <s:hidden name="submit" value="10" /> 
    ...other fields go here 
</s:form> 
在動作類我有 getSubmitsetSubmit方法,按照下述方式

public boolean isFormSubmitted(){ 
    return (submit == 10); 
} 
+0

「發現哪個表單提交」 - 你的電話一個動作從不同的形式,並知道至少提交了一些不同的邏輯在行動? –

回答

3

你可以調用不同的操作方法,在你的行動,不僅是 「執行」 的方法。只需在請求中添加名爲「method:actionMethodName」的參數即可。下面是例子:

public class MyAction extends ActionSupport {  

    public String execute() {  
    // Base code 
    return SUCCESS; 
    } 

    public String one() {  
    // Code one 
    return SUCCESS; 
    } 

    public String two() {  
    // Code two 
    return SUCCESS; 
    }  

} 

而這裏的jsp:

<s:form action="MyAction"> 
    <input type="submit" value="Call execute"/> 
    <input type="submit" name="method:one" value="Call method one"/> 
    <input type="submit" name="method:two" value="Call method two"/> 
</s:> 

或者你也可以做這樣的:

<s:form action="MyAction" name="form0"> 
    <!-- call execute--> 
</s:> 

<s:form action="MyAction" name="form1"> 
    <!-- call method one--> 
    <input type="hidden" name="method:one"/> 
</s:> 

<s:form action="MyAction" name="form2"> 
    <!-- call method two--> 
    <input type="hidden" name="method:two"/> 
</s:> 
0

您應該使用<s:form>標籤action屬性提交到具體行動。

<s:form action="action1"> 
    ... 
</s:form> 

<s:form action="action2"> 
    ... 
</s:form> 

<s:form>標籤文檔:http://struts.apache.org/2.x/docs/form.html

更新

然後,只需使用加載和保存用戶單獨行動。

+0

但你沒有明白我的意思!我知道動作屬性,但想象一下editUser動作。當表單尚未提交時,我應該將用戶加載到數據庫,而如果表單已提交,我應該嘗試將用戶保存到數據庫中。我怎麼能理解我應該做什麼。檢查editUser表單域非常髒,特別是因爲後來我決定更改字段。這是一個例子。在一些頁面中,我有不同的形式調用注入相同的動作。我寧願像表格名稱那樣檢查。不是隱藏的領域! – Johnny

+0

@ user1892555:然後只需使用單獨的操作來加載和保存用戶。 –

+0

無用的數據庫加載和剩餘的問題!爲什麼?首先,我們應該去編輯用戶加載並顯示錶單,然後提交到saveUser。然後保存成功/錯誤的用戶應該返回到編輯用戶審查。 editUser總是嘗試加載用戶。可以有另一種情況。如果用戶直接進行saveUser操作,則不會下劃線,如果我已重定向到它或表單已提交。另一方面,我將不得不有三個班級一個行動。 1個擴展ActionSupport幷包含屬性和getter和setter以避免冗餘的父類。 2個孩子 – Johnny

0

你應該做這樣的提交表單。

<s:form name="form1"> 
<s:hidden name="submit" value="10" /> 
</s:form> 

創建一個Java腳本函數

function onclick() 
{ 
document.form1.submit(); 
} 

然後創建干將在行動& setter方法隱藏字段

和執行方法

public String execute() 
    { 
    setSubmit(10); 
    return SUCCESS; 
} 

更新:

<a href="your_action_name"></a> 

<s:a href="your_action"></a> 
+0

,如果用戶禁用JavaScript?! – Johnny

+0

好的,那麼你可以使用錨點標籤放置在你想點擊的地方,檢查更新的部分 –

+0

你的意思是我設置了 Johnny