【问题标题】:Redirect index.jsp to a Struts 2 Action将 index.jsp 重定向到 Struts 2 Action
【发布时间】:2014-07-16 10:13:21
【问题描述】:

相关: How can I set the welcome page to a struts action?

我正在尝试将我的欢迎页面 index.jsp 重定向到一个操作 (Struts2)。 相关帖子中的任何选项都不适合我:

type Status report
message /malelo/indexRedir
description The requested resource (/malelo/indexRedir) is not available.

我遵循这个 FLOW 结构:

http://localhost:8580/malelo/ SEND TO WELCOME PAGE
     --->  (root)index.jsp SEND USER TO (by redirect, I need forward)
          --->  (indexRedir) IndexAction.java SEND USER TO
               ---> (WEB-INF/content) index-login.jsp

这行得通,但不太优雅(index.jsp):

<% 
  response.sendRedirect("indexRedir");
%>

我正在尝试这样做(index.jsp):

<jsp:forward page="indexRedir" />

我只使用 Annotation,请不要配置 XML 文件。

@Action (value = "indexRedir", results = 
    { @Result (location = "index-login.jsp", name = "ok") } ) 

编辑

澄清一下,这是通过 response.sendRedirect 发送到浏览器的 http 标头:

HTTP/1.1 302 Found
Date: Tue, 27 May 2014 16:15:12 GMT
Location: http://cmabreu.com.br/basico/
Content-Length: 0
Connection: close
Content-Type: text/plain

此位置 (http://cmabreu.com.br/basico/) 指向 index.jsp,其中:

<% 
  response.sendRedirect("indexRedir");
%>

并显示 indexRedir 动作指向的 JSP 页面的内容。谷歌网站管理员工具和一些机器人不关注这样的页面。

在此处查看更多信息:http://en.wikipedia.org/wiki/HTTP_302

【问题讨论】:

  • any 操作是否有效?另请注意,转发与重定向不同;如果你想 forward (我不会,因为你的 URL 不会改变),那么你需要配置你的服务器以通过拦截器处理转发,例如,对于 Tomcat,@987654324 @.
  • 是的。我的系统运行良好,有 50 个动作。我知道转发不是相同的重定向。我想改变只是因为我知道这一点。我的照片让人们感到困惑,对不起。不是文件夹,是流程。
  • 请不要在不告诉我为什么和不理解问题的情况下投反对票!请不要删除答案!
  • 欢迎来到 S2 标签用户之一的世界;我为他的行为道歉。您可以在转发之外访问indexRedir 操作吗?
  • 是的!在欢迎页面 index.jsp 中使用 response.sendRedirect("indexRedir") 可以正常工作。顺便说一句,您在其他帖子中的解决方案(您在上面给我的链接)有效。请把它放在一个答案中。现在我可以从 index.jsp 中&lt;jsp:forward page="indexRedir" /&gt;

标签: java struts2 annotations struts2-convention-plugin


【解决方案1】:

为了转发到一个动作,您必须配置过滤器以在转发上运行;通常他们不会,尽管这取决于容器。比如在Tomcat下:

<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  <dispatcher>REQUEST</dispatcher> 
  <dispatcher>FORWARD</dispatcher>
  <dispatcher>INCLUDE</dispatcher> <!-- If you want includes as well -->
</filter-mapping>

就我个人而言,我并不觉得重定向特别不雅,而且非常标准。

另一个好处是您可以将人们转移到基于操作的应用程序中;转发您的 URL 不会改变。一般来说,您会希望禁止基于 JSP 的 URL。

【讨论】:

  • 工作就像一个魅力。谢谢。
  • 我认为只在欢迎页面做不太好。谷歌网站管理员工具理解Moved Temporarily,我不喜欢这样。有些机器人也可能有问题。
猜你喜欢
  • 1970-01-01
  • 2014-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多