正在学习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.javapackage 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>

解决方案 »

  1.   

    仔细看了一下,没发现问题。
    你可以重载 execute 方法试试,看看你的 method 值是多少。
    在里面输出一下,然后调用 super.execute()。最好DEBUG
      

  2.   

    仔细看了一下,没发现问题。
    你可以重载 execute 方法试试,看看你的 method 值是多少。
    在里面输出一下,然后调用 super.execute()。最好DEBUG
      

  3.   

    请问楼上的朋友,可以详细地说一下么?
    运行原理我现在还不是很清楚,我不太清楚如何取到method的值
    用request.getAttribute()吗?
      

  4.   

    回4楼,已经试过很多次了
    tomcat从5.5换到6.0
    结果都是一样
      

  5.   

    return mapping.findForward("next"); 两次跳转都是同一个页面所以看不到效果,你看看是不是因为这样。
      

  6.   

    有可能是IE有缓存的问题~~~~~~~~~
    重启ie看下
    在后台直接写上request.getParamter("method");
    在后台 输出下看到底是那个值;
      

  7.   

    你出现笔误了吧:<form> 应该</form> 
      

  8.   

    <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>