新人提问:使用DispatchAction的问题

guanglin2320 2009-04-03 06:10:48
正在学习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>
...全文
104 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
guanglin2320 2009-04-04
  • 打赏
  • 举报
回复
感谢大家,8楼9楼的正确
dadi5566 2009-04-04
  • 打赏
  • 举报
回复
<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>
yangfeitarena 2009-04-04
  • 打赏
  • 举报
回复
你出现笔误了吧:<form> 应该</form>
guolimin1118 2009-04-04
  • 打赏
  • 举报
回复
有可能是IE有缓存的问题~~~~~~~~~
重启ie看下
在后台直接写上request.getParamter("method");
在后台 输出下看到底是那个值;
gwikimo 2009-04-04
  • 打赏
  • 举报
回复
return mapping.findForward("next"); 两次跳转都是同一个页面所以看不到效果,你看看是不是因为这样。
wzju64676266 2009-04-03
  • 打赏
  • 举报
回复
加上表单名称看一下
guanglin2320 2009-04-03
  • 打赏
  • 举报
回复
回4楼,已经试过很多次了
tomcat从5.5换到6.0
结果都是一样
luobaolin2009 2009-04-03
  • 打赏
  • 举报
回复
没问题啊,重新部署然后启动一下
guanglin2320 2009-04-03
  • 打赏
  • 举报
回复
请问楼上的朋友,可以详细地说一下么?
运行原理我现在还不是很清楚,我不太清楚如何取到method的值
用request.getAttribute()吗?
商科程序员 2009-04-03
  • 打赏
  • 举报
回复
仔细看了一下,没发现问题。
你可以重载 execute 方法试试,看看你的 method 值是多少。
在里面输出一下,然后调用 super.execute()。

最好DEBUG
商科程序员 2009-04-03
  • 打赏
  • 举报
回复
仔细看了一下,没发现问题。
你可以重载 execute 方法试试,看看你的 method 值是多少。
在里面输出一下,然后调用 super.execute()。

最好DEBUG

81,077

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧