2013-11-25 106 views
5

我有一個帶有鏈接的JSP文件象下面這樣:通鏈接價值豆

<a href="links.do?method=homeAdmin"> 
<a href="links.do?method=signUp"> 

而且我有一個動作類LinkAction.java

public class LinkAction extends org.apache.struts.action.Action { 

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

     return mapping.findForward("signUp"); 
    } 
    public ActionForward homeAdmin(ActionMapping mapping, ActionForm form, 
      HttpServletRequest request, HttpServletResponse response) 
      throws Exception { 

     return mapping.findForward("homeAdmin"); 
    } 
} 

的問題是,這是行不通的。但是當我將其更改爲這樣的情況時,它僅適用於signUp操作。

public class LinkAction extends org.apache.struts.action.Action { 

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

     return mapping.findForward("signUp"); 
    } 

} 

我也試圖改變它

public class LinkAction extends DispatchAction { ... } 

這裏是我的struts-config.xml

<action input="/index.jsp" 
     name="signUp" 
     path="/links" 
     scope="session" 
     type="Actions.LinkAction"> 
    <forward name="signUp" path="/signUp.jsp"/> 
    <forward name="homeAdmin" path="/homeAdmin.jsp"/> 
</action> 

我想利用這個單一操作文件在我的網頁所有鏈接。我該怎麼做?

+0

你可以在你的'struts-config.xml'中共享動作定義嗎? –

+0

我認爲你只需要在'struts-config.xml'中添加一些東西,然後使用一個擴展類'org.apache.struts.actions.DispatchAction'的動作。 –

回答

2

您需要添加parameter="method"。這標識了包含方法名稱的請求參數。

如果你有下一步的動作類:

package actions; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.apache.struts.action.ActionForm; 
import org.apache.struts.action.ActionForward; 
import org.apache.struts.action.ActionMapping; 
import org.apache.struts.actions.DispatchAction; 

public class LinkAction extends DispatchAction { 

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

     return mapping.findForward("signUp"); 
    } 

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

     return mapping.findForward("homeAdmin"); 
    } 
} 

您需要在struts-config.xml文件如下配置:

<form-beans> 
    <form-bean name="SignUpForm" type="org.apache.struts.action.DynaActionForm"> 
     <form-property name="fname" type="java.lang.String" /> 
     <form-property name="lname" type="java.lang.String" /> 
     <form-property name="usr" type="java.lang.String" /> 
     <form-property name="pwd" type="java.lang.String" /> 
    </form-bean> 
</form-beans> 
<action-mappings> 
    <action path="/links" 
      type="actions.LinkAction" 
      name="SignUpForm" 
      input="/index.jsp" 
      parameter="method" 
      scope="session"> 
     <forward name="signUp" path="/signUp.jsp"/> 
     <forward name="homeAdmin" path="/homeAdmin.jsp"/> 
    </action> 
</action-mappings> 

org.apache.struts.actions.DispatchAction見。

+0

我可以在struts config中的同一個動作中添加多個輸入嗎?因爲我使用相同的LinkAction.java文件鏈接多個頁面。 – Maverick

+0

是的,在struts config中添加parameter =「method」。 – Maverick

+0

'input'屬性用於驗證錯誤,並且當您爲輸入頁面返回_forward_的mapping.getInputForward();'時。通過簡單的鏈接,你可以使用'html:link'標籤。 –

1
Struts1 always calls execute(ActionMapping mapping, ActionForm form, 
     HttpServletRequest request, HttpServletResponse response) 

處理請求的方法。您可以嘗試在​​方法中獲取請求parameter('method'),並根據其值將其代理到homeAdmin或signUp。

1

據我什麼,我得到了你的問題,

1)有沒有什麼辦法,通過一個動作類,可以處理多個任務,如:你的兩個方法可以工作。

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

return mapping.findForward("signUp"); 
} 

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

return mapping.findForward("homeAdmin"); 
} 

,如果它是正確的,那麼你應該去其他類型的行動即DispatchAction的,因爲它提供了編寫多種方法的能力。

的詳細信息:

public class LanguageSelectAction extends DispatchAction{ 

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

    request.getSession().setAttribute(
      Globals.LOCALE_KEY, Locale.SIMPLIFIED_CHINESE); 

    return mapping.findForward("provide"); 
} 

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

    request.getSession().setAttribute(
      Globals.LOCALE_KEY, Locale.ENGLISH); 

    return mapping.findForward("success"); 
} 

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

    request.getSession().setAttribute(
      Globals.LOCALE_KEY, Locale.GERMAN); 

    return mapping.findForward("success"); 
} 

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

    request.getSession().setAttribute(
      Globals.LOCALE_KEY, Locale.FRANCE); 

    return mapping.findForward("success"); 
} 

}

你可以得到詳細的here

+0

返回mapping.findForward()在每個方法中返回相同的字符串。我想根據我調用的方法轉發不同的字符串。 – Maverick

+0

在web.xml中@Ankit,所有準備好你映射你的動作,現在如果你看到,那麼它就是說你正在返回「提供」 「成功」字符串的地方。 < forward name =「provide」path =「/ requiredpage.jsp」/> Piyush