langhua9527 2009-12-01 12:53 采纳率: 0%
浏览 230
已采纳

网页主页的问题

比如说我现在的网页:http://127.0.0.1:8080
我有一个项目放到TOMCAT的ROOT目录下
然后我想输出http://127.0.0.1:8080跳转到xxxx.action!xxx=yyyy这个页面
我在WEB.XML里面配置了

index.jsp

index.jps里面:
response.sendRedirect("index/indexWebAction!indexFrame.action");

这样当我输入http://127.0.0.1:8080
就会转到http://127.0.0.1:8080/index/indexWebAction!indexFrame.action这个页面(IE地址栏上面也是这个地址)
但是我想这个地址还是http://127.0.0.1:8080,而且显示index/indexWebAction!indexFrame.action这个里面的内容

[b]问题补充:[/b]
我想用第二种解决,但是好像不行啊。。。
request.getRequestDispatcher("index/indexWebAction!indexFrame.action").forward(request,response);

http://127.0.0.1:9000/index/indexWebAction!indexFrame.action
这样就行的。。
上面那样就不行。。。
[b]问题补充:[/b]
request.getRequestDispatcher("http://127.0.0.1:9000/index/indexWebAction!indexFrame.action ").forward(request,response);

这个我试了的。。。

  • 写回答

8条回答 默认 最新

  • Ryankay 2009-12-03 20:44
    关注

    我看了下我以前的代码,以前用的是
    [code="java"]RequestDispatcher rd = getServletContext().getRequestDispatcher("/seconddurl.do?m=index");
    rd.forward(request,response);[/code]
    ServletContext的getRequestDispatcher()其实和ServletRequest的getRequestDispatcher()有点区别的。
    [code="java"]
    getRequestDispatcher
    public RequestDispatcher getRequestDispatcher(java.lang.String path)
    Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path. A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.
    The pathname must begin with a "/" and is interpreted as relative to the current context root. Use getContext to obtain a RequestDispatcher for resources in foreign contexts. This method returns null if the ServletContext cannot return a RequestDispatcher.

    Parameters:
    path - a String specifying the pathname to the resource
    Returns:
    a RequestDispatcher object that acts as a wrapper for the resource at the specified path
    See Also:
    RequestDispatcher, getContext(java.lang.String)
    [/code]

    ServletRequest的相关API
    [code="java"]
    getRequestDispatcher
    public RequestDispatcher getRequestDispatcher(java.lang.String path)
    Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path. A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.
    The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root. This method returns null if the servlet container cannot return a RequestDispatcher.

    The difference between this method and ServletContext.getRequestDispatcher(java.lang.String) is that this method can take a relative path.

    Parameters:
    path - a String specifying the pathname to the resource
    Returns:
    a RequestDispatcher object that acts as a wrapper for the resource at the specified path
    See Also:
    RequestDispatcher, ServletContext.getRequestDispatcher(java.lang.String)

    [/code]
    [color=red]
    The difference between this method and ServletContext.getRequestDispatcher(java.lang.String) is that this method can take a relative path.[/color]

    所以request.getRequestDispatcher("index/indexWebAction!indexFrame.action").forward(request,response);

    request.getRequestDispatcher("/index/indexWebAction!indexFrame.action").forward(request,response);

    效果是一样的,因为request.getRequestDispatcher()这方法可以处理相对路径自动加上工程根路径,昨天我测试了下,确实是的,但是forward却失败了。

    这点很吃惊,后来比较了下,以前项目用的是struts1,现在用的struts2。

    查看web.xml发现:struts2有个
    [code="xml"]

    struts
    org.apache.struts2.dispatcher.FilterDispatcher

    [/code]

    呵呵,着就难怪了。因为第一个filter执行后,没有进入struts2,所以没进action.

    在servlet规范2.4以后,配置filter时有一个dispatcher的元素,修改配置如下
    [code="java"]

    publichome
    /*


    struts-cleanup
    *.do
    FORWARD
    REQUEST

    [/code]

    提示:在不配置的情况下,默认是REQUEST,就是说只有REQUEST才会被STRUTS2的FILTER拦截,而FORWARD过来的请求是会被忽略的,这样我们的FORWARD当然不成功了!

    关于这个属性你可以访问
    [url]http://www.360doc.com/content/070903/11/39865_715414.html[/url]

    改成上面的配置后,我再一测试,果然,FORWARD成功!原来是STRUTS2的问题,呵呵,估计你直接FORWARD一个HTML或者JSP肯定是可以的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿