如何将欢迎页面设置为struts动作?

时间:2020-03-05 18:46:24  来源:igfitidea点击:

我有一个基于struts的webapp,我希望将默认的"欢迎"页面作为一个动作。我发现的唯一解决方案似乎是使欢迎页面成为包含对操作的重定向的JSP的变体。例如,在web.xml中:

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

和在index.jsp中:

<% 
  response.sendRedirect("/myproject/MyAction.action");
%>

当然,有更好的方法!

解决方案

回答

似乎流行的解决方案无法在所有容器中使用... http://www.theserverside.com/discussions/thread.tss?thread_id=30190

回答

我将创建一个过滤器,并退回所有请求,以使用前向响应将其重新植根。创建home.do页面的黑客对我来说很难看(还有一件要记住的事,并调查支持我们代码的人)。

回答

"当然有更好的方法!"

没有。 Servlet规范(例如Java Servlet规范2.4," SRV.9.10 Welcome Files")状态:

The purpose of this mechanism is to allow the deployer to specify an ordered
  list of partial URIs for the container to use for appending to URIs when there is a
  request for a URI that corresponds to a directory entry in the WAR not mapped to
  a Web component.

我们不能将Struts映射到'/',因为Struts需要使用文件扩展名。因此,我们可以使用隐式映射的组件,例如JSP或者静态文件。所有其他解决方案都只是黑客。因此,请保持解决方案的可读性和可维护性,不要再看了。

回答

就个人而言,我将保持与现在相同的设置,但是将转发更改为转发。这样可以避免将标头发送回客户端,并使它们发出另一个请求。

因此,特别是,我将替换

<% 
  response.sendRedirect("/myproject/MyAction.action");
%>

在index.jsp中

<jsp:forward page="/MyAction.action" />

此更改的另一个效果是,随着转发的进行,用户不会看到地址栏中的URL从" http:// server / myproject"更改为" http://server/myproject/index.jsp"在服务器内部。

回答

从Servlet规范的2.4版本开始,允许在欢迎文件列表中包含Servlet。请注意,这可能不是URL(例如/myproject/MyAction.action)。它必须是一个命名的servlet,并且我们不能将查询字符串传递给servlet。控制器Servlet将需要执行默认操作。

<servlet>
  <servlet-name>MyController</servlet-name>
  <servlet-class>com.example.MyControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>MyController</servlet-name>
  <url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
  <welcome-file>MyController</welcome-file>
</welcome-file-list>

回答

我要做的是放置一个与struts动作同名的空文件,并欺骗容器调用struts动作。

前任。如果struts动作是welcome.do,请创建一个名为welcome.do的空文件。那应该欺骗容器来调用Struts动作。