我现在遇到一个问题:
在DispatchAction中我有一个方法saveBook(),它有个参数request,我要在该方法中调用request.setAttribute("msg","my msg")
然后forward到另外一个页面show.jsp
在show.jsp中,我用out.println(request.getAttribute("msg"))打印出我想要的信息my msg.
但是结果为null.
我想这个应该是个生命周期的问题.我用Action就不存在这个问题。请高手指教!
相关代码:
public class TestAction extends DispatchAction {
public ActionForward ok(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
TestForm testForm = (TestForm) form;
request.setAttribute("msg","my msg");
return mapping.findForward("ok");
}
}

解决方案 »

  1.   

    你能确定你调用了TestAction吗。DispatchAction很容易忘记在struts-config中的action配置parameter="method"
      

  2.   

    request.setAttribute("msg","my msg");
    ================
    下一行加上System.out.println("test");还有就是struts-config.xml文件确实配了吗
    还有有注释掉execute方法吧
      

  3.   

    使用DispatchAction要在struts-config中的action配置parameter="method"
      

  4.   

    "在show.jsp中,我用out.println(request.getAttribute("msg"))打印出我想要的信息my msg.
    但是结果为null."你在show.jsp中得到结果为null,请问你是通过URL直接访问show.jsp的?还是通过DispatchAction转发的?如果是经过DispatchAction转发的,那说明Struts转发环节没有问题,只是往容器赋值或取值是有错,你可以这样测试:
    在"request.setAttribute("msg","my msg");"后
    加这样的测试语句:request.getAttribute("msg);
    然后看命令行输出的值是否正确.
    在页面还可以用<prisent>判断是否存在request.getAttribute("msg").
      

  5.   

    首先确定页面里面没有错误(比如标签引入,标签里面的属性),我以前也出现过你这样的错,最后发现在页面里面少了个/引起的,struts有时候真的很奇怪