如何在action中引用另一个action中的方法
推荐于2016-05-21 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517196
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
action中调用另一个action中的方法
建立三个jsp页面
forward1.jsp,forward2.jsp,forward3.jsp
forward2.jsp是最后跳转的页面
建立2个action
Forward1Action,Forward3Action
forward1:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>forward1.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<a href="forward1.do?method=doForward">forwarder to forward2.jsp</a>
</body>
</html:html>
forward2:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>forward2.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<bean:write name="from"/> <br>
<logic:present name = "from2">
<bean:write name="from2"/> <br>
</logic:present>
</body>
</html:html>
forward3:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>forward3.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<a href="forward3.do?method=doForward">forwarde to forward3.do</a>
</body>
</html:html>
}
package com.test.struts.action;
import java.util.Properties;
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;
import com.test.struts.form.Forward1Form;
public class Forward1Action extends DispatchAction {
public ActionForward doForward(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
Forward1Form forward1Form = (Forward1Form) form;
String s = "form forward1.jsp";
request.setAttribute("from",s);
return mapping.findForward("ok");
}
}
package com.test.struts.action;
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 Forward3Action extends DispatchAction {
public ActionForward doForward(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
Forward1Action f1 = new Forward1Action();
//调用另一个action的方法
ActionForward a = f1.doForward(mapping,form,request,response);
String s = "from forward3.jsp";
request.setAttribute("from2",s);
return a;
}
}
配置文件:
<xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="forward1Form"
input="/forward1.jsp"
path="/forward1"
scope="request"
type="com.test.struts.action.Forward1Action" parameter="method">
<forward name="ok" path="/forward2.jsp" />
</action>
<action
input="/forward3.jsp"
path="/forward3"
type="com.test.struts.action.Forward3Action" parameter="method">
<!--此处配置成要调用的action方法返回的页面,即f1.doForward(mapping,form,request,response)返回的页面-->
<forward name="ok" path="/forward2.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.test.struts.ApplicationResources" />
</struts-config>
建立三个jsp页面
forward1.jsp,forward2.jsp,forward3.jsp
forward2.jsp是最后跳转的页面
建立2个action
Forward1Action,Forward3Action
forward1:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>forward1.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<a href="forward1.do?method=doForward">forwarder to forward2.jsp</a>
</body>
</html:html>
forward2:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>forward2.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<bean:write name="from"/> <br>
<logic:present name = "from2">
<bean:write name="from2"/> <br>
</logic:present>
</body>
</html:html>
forward3:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>forward3.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<a href="forward3.do?method=doForward">forwarde to forward3.do</a>
</body>
</html:html>
}
package com.test.struts.action;
import java.util.Properties;
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;
import com.test.struts.form.Forward1Form;
public class Forward1Action extends DispatchAction {
public ActionForward doForward(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
Forward1Form forward1Form = (Forward1Form) form;
String s = "form forward1.jsp";
request.setAttribute("from",s);
return mapping.findForward("ok");
}
}
package com.test.struts.action;
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 Forward3Action extends DispatchAction {
public ActionForward doForward(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
Forward1Action f1 = new Forward1Action();
//调用另一个action的方法
ActionForward a = f1.doForward(mapping,form,request,response);
String s = "from forward3.jsp";
request.setAttribute("from2",s);
return a;
}
}
配置文件:
<xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="forward1Form"
input="/forward1.jsp"
path="/forward1"
scope="request"
type="com.test.struts.action.Forward1Action" parameter="method">
<forward name="ok" path="/forward2.jsp" />
</action>
<action
input="/forward3.jsp"
path="/forward3"
type="com.test.struts.action.Forward3Action" parameter="method">
<!--此处配置成要调用的action方法返回的页面,即f1.doForward(mapping,form,request,response)返回的页面-->
<forward name="ok" path="/forward2.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.test.struts.ApplicationResources" />
</struts-config>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询