新人提问:使用DispatchAction的问题
正在学习struts,按照视频中写的代码,在show.jsp页面的两个form中分别有一个按扭,一个叫print一个叫saveAsExcel,想实现点击不同的按扭在message.jsp页面上返回相应的方法名(方法名也叫pring,saveAsExcel)
问题是:
无论我点击哪个按扭,返回的都是print
而当我把show.jsp中的包含print按扭的form删除后,点击saveAsExcel按扭,就可以显示saveAsExcel了
再次添加上print按扭,又是不论点击哪个都显示saveAsExcel了,小弟新人,请大家多帮助!
------------------------------------------------
源代码如下:
show.jsp
<%@page contentType="text/html;charset=GBK"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@page isELIgnored="false"%>
<html>
<head>
<title>dispatch</title>
</head>
<body>
<h1 align="center"><font size="200" color="red">dispatch</font></h1>
<hr>
<form action="${pageContext.request.contextPath}/actions/dispatch.do" method="POST" >
<input type="hidden" name="method" value="print" />
<center>
<input type="submit" value="print" />
</center>
<form>
<form action="${pageContext.request.contextPath}/actions/dispatch.do" method="POST" >
<input type="hidden" name="method" value="saveAsExcel" />
<center>
<input type="submit" value="save As Excel" />
</center>
<form>
</body>
</html>
------------------------------------------------
message.jsp
<%@page contentType="text/html;charset=GBK"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@page isELIgnored="false"%>
<html>
<head>
<title>message</title>
</head>
<body>
<h1 align="center"><font size="200" color="red">message</font></h1>
<hr>
<center>
<font size="5" color="red">you click is ${operation}</font>
</center>
</body>
</html>
------------------------------------------------
StudentAction.java
package com.kettas.struts.actions.dispatch;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class StudentAction extends DispatchAction{
public ActionForward print(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception{
request.setAttribute("operation","print");
return mapping.findForward("next");
}
public ActionForward saveAsExcel(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception{
request.setAttribute("operation","saveAsExcel");
return mapping.findForward("next");
}
}
------------------------------------------------
struts-config.xml:
<action path="/actions/dispatch"
type="com.kettas.struts.actions.dispatch.StudentAction"
parameter="method"
scope="session">
<forward name="next" path="/actions/dispatch/message.jsp"/>
</action>