2014-06-11 17 views
0

我用我的JSP頁面上的支柱,佈局以顯示與單選按鈕表提交按鈕。它有一個提交按鈕,用於在struts操作類中調用execute方法。這工作正常。支柱佈局多采用reqCode

我需要相同的形式添加另一個提交按鈕既可以執行另一種方法或以某種方式簡單地實現了第二個按鈕被點擊的動作類的方法相同。我明白這可以使用reqCode完成。但我不知道如何。

我的JSP代碼:

<layout:form action="/taskSelected.do" reqCode="execute" > 
    <layout:submit value="Show Task Status" styleClass="btn btn-default"/> 
    <layout:submit value="troubleshoot" styleClass="btn btn-default" reqCode="troubleshoot"/> 
    <layout:collection name="userTasks" title="" styleClass="FORM" selectProperty="uniqueID" selectType="radio"> 
    <layout:collectionItem title="Task Name" property="name"/> 
    <layout:collectionItem title="UID" property="uniqueID" /> 
    <layout:collectionItem title="Request Time" property="add_info" /> 
    <layout:collectionItem title="FTP URL" property="ftp_url"/> 
    <layout:collectionItem title="Status" property="status" /> 
    </layout:collection> 

我的struts-config.xml

<action name="taskBean" path="/taskSelected" scope="session" type="com.myapp.struts.taskSelectedAction" validate="false"> 
     <forward name="success" path="/ShowTaskStatus.jsp"/> 
     <forward name="troubleshootPage" path="/ShowTroubleshoot.jsp"/> 
     <forward name="failure" path="/ViewTasks.jsp"/> 
    </action> 

我的Action類第一種方法:

public ActionForward troubleshoot(ActionMapping mapping, ActionForm form, 
     HttpServletRequest request, HttpServletResponse response) 
     throws Exception { 

    System.out.println("Method 1= " + request.getSession().getAttribute("troubleshoot")); 

    if(request.getSession().getAttribute("USER_KEY")==null){ 
     return mapping.findForward("NotLoggedIn"); 
    } 
    System.out.println("Reached in Troubleshoot"); 
    return mapping.findForward("troubleshootPage"); 

} 

第二種方法

public ActionForward execute(ActionMapping mapping, ActionForm form, 
     HttpServletRequest request, HttpServletResponse response) 
     throws Exception { 
    System.out.println("Method 2= " + request.getSession().getAttribute("troubleshoot")); 
    if(request.getSession().getAttribute("USER_KEY")==null){ 
     return mapping.findForward("NotLoggedIn"); 
    } 

這應該是非常簡單的,但我只是無法弄清楚什麼是錯的。

回答

0

使用兩個按鈕,而不是在你的JSP頁面(我也嘗試了兩種的提交在一個點上,但對我沒有工作)。

<input type="button" value="Method1" onclick="location.href='taskSelected.do?method=method1';"/> 
<input type="button" value="Method2" onclick="location.href='taskSelected.do?method=method2';"/> 

在你的struts-config.xml文件中添加參數=方法

<action name="taskBean" 
     path="/taskSelected" 
     scope="session" 
     type="com.myapp.struts.taskSelectedAction" validate="false" 
     parameter=method> 

你只需要一個動作類。

public class taskSelectedAction extends org.apache.struts.actions.DispatchAction{ 

    public ActionForward method1(ActionMapping mapping, ActionForm form, 
    HttpServletRequest request, HttpServletResponse response) 
    throws Exception{ 
     //your code goes here  
    } 

    public ActionForward method2(ActionMapping mapping, ActionForm form, 
    HttpServletRequest request, HttpServletResponse response) 
    throws Exception{ 
     //your code goes here 
    } 
} 
+0

感謝回答。通過這種方法,至少我可以得到兩個不同的按鈕來調用兩個單獨的方法。但是DataGrid中的數據呢?我有一個佈局集合(Datagrid),我的目標是確定網格用戶選擇了哪一行。根據他選擇的行和他按下的按鈕,我需要啓動單獨的任務。通過這種方法,select屬性(在這種情況下是uniqueID)返回null。 – mumshad

+0

您將需要創建一個FormBean併爲其中的表單屬性創建getter/setter方法。 – Maverick