将链接值传递给bean

时间:2013-11-25 19:19:29

标签: java html jsp struts-1

我有一个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>

我想将此单个操作文件用于我网页中的所有链接。我该怎么做?

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

处理请求的方法。您可以尝试在parameter('method')方法中获取请求execute(),并根据其值将其委托给homeAdmin或signUp。

答案 2 :(得分: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");
}

如果它是正确的,那么你应该去另一种类型的Action,即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

答案 3 :(得分:1)

正如piyush在上面解释的那样,使用DispatchAction扩展你的动作类点击http://struts.apache.org/release/1.3.x/struts-extras/apidocs/org/apache/struts/actions/DispatchAction.html

配置文件中的配置应该是这样的

<action-mappings>
  <action input="/yourpage.jsp" parameter="method" name="yourform" 
    path="/yourAction" scope="session/request" type="yourpackage.youraction">
    <forward name="signup" path="/yourpage.jsp" />
</action>