gpt4 book ai didi

java - 将链接值传递给 bean

转载 作者:太空狗 更新时间:2023-10-29 15:56:31 25 4
gpt4 key购买 nike

我有一个带有如下链接的 JSP 文件:

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

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

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

最佳答案

您需要添加parameter="method"。这标识了包含方法名称的请求参数。

如果你有下一个 Action 类:

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 .

关于java - 将链接值传递给 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20201384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com