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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:30:14  来源:igfitidea点击:

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

javajspstruts2struts

提问by mrowe

I have a struts-based webapp, and I would like the default "welcome" page to be an action. The only solutions I have found to this seem to be variations on making the welcome page a JSP that contains a redirect to the action. For example, in web.xml:

我有一个基于 struts 的 web 应用程序,我希望默认的“欢迎”页面是一个操作。我发现的唯一解决方案似乎是使欢迎页面成为包含指向操作的重定向的 JSP 的变体。例如,在web.xml

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

and in index.jsp:

并在index.jsp

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

Surely there's a better way!

肯定有更好的方法!

回答by bpapa

It appears that a popular solution will not work in all containers... http://www.theserverside.com/discussions/thread.tss?thread_id=30190

似乎流行的解决方案不适用于所有容器...... http://www.theserverside.com/discussions/thread.tss?thread_id=30190

回答by Georgy Bolyuba

I would create a filter and bounce all requests to root back with forward responce. Hacks with creating home.do page looks ugly to me (One more thing to remember for you and investigate for someone who will support your code).

我会创建一个过滤器,并通过前向响应将所有请求退回到 root。在我看来,创建 home.do 页面的技巧很难看(还有一件事要记住,并调查支持您代码的人)。

回答by Damien B

"Surely there's a better way!"

“当然有更好的办法!”

There isn't. Servlet specifications (Java Servlet Specification 2.4, "SRV.9.10 Welcome Files" for instance) state:

没有。Servlet 规范(例如 Java Servlet 规范 2.4,“SRV.9.10 欢迎文件”)声明:

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.

这种机制的目的是允许部署者为容器指定部分 URI 的有序列表,当存在对 URI 的请求时,该 URI 对应于未映射到 Web 组件的 WAR 中的目录条目.

You can't map Struts on '/', because Struts kind of require to work with a file extension. So you're left to use an implicitely mapped component, such as a JSP or a static file. All the other solutions are just hacks. So keep your solution, it's perfectly readable and maintainable, don't bother looking further.

您不能在“/”上映射 Struts,因为 Struts 需要使用文件扩展名。因此,您只能使用隐式映射的组件,例如 JSP 或静态文件。所有其他解决方案都只是黑客。所以保持你的解决方案,它是完全可读和可维护的,不要再费心寻找了。

回答by Martin McNulty

Personally, I'd keep the same setup you have now, but change the redirect for a forward. That avoids sending a header back to the client and having them make another request.

就我个人而言,我会保留您现在的相同设置,但将重定向更改为转发。这避免了将标头发送回客户端并让他们发出另一个请求。

So, in particular, I'd replace the

所以,特别是,我会替换

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

in index.jsp with

在 index.jsp 中

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

The other effect of this change is that the user won't see the URL in the address bar change from "http://server/myproject" to "http://server/myproject/index.jsp", as the forward happens internally on the server.

此更改的另一个影响是用户不会看到地址栏中的 URL 从“ http://server/myproject”更改为“ http://server/myproject/index.jsp”,因为转发发生在服务器内部。

回答by Craig Wohlfeil

As of the 2.4 version of the Servlet specification you are allowed to have a servlet in the welcome file list. Note that this may not be a URL (such as /myproject/MyAction.action). It must be a named servlet and you cannot pass a query string to the servlet. Your controller servlet would need to have a default action.

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

回答by Nischal

Something that I do is to put an empty file of the same name as your struts action and trick the container to call the struts action.

我所做的就是放置一个与您的 struts 动作同名的空文件,并诱使容器调用 struts 动作。

Ex. If your struts action is welcome.do, create an empty file named welcome.do. That should trick the container to call the Struts action.

前任。如果您的 struts 操作是welcome.do,请创建一个名为welcome.do 的空文件。这应该会诱使容器调用 Struts 操作。

回答by Srikanth

This is a pretty old thread but the topic discussed, i think, is still relevant. I use a struts tag - s:action to achieve this. I created an index.jsp in which i wrote this...

这是一个相当古老的线程,但我认为所讨论的主题仍然相关。我使用 struts 标签 - s:action 来实现这一点。我创建了一个 index.jsp,我在其中写了这个...

<s:action name="loadHomePage" namespace="/load" executeResult="true" />

回答by Navathej

This works as well reducing the need of a new servlet or jsp

这也可以减少对新 servlet 或 jsp 的需求

<welcome-file-list>
<welcome-file>/MyAction.action</welcome-file>
</welcome-file-list>

回答by gavenkoa

Here two blogs with same technique:

这里有两个使用相同技术的博客:

It require Servlet API >= v2.4:

它需要 Servlet API >= v2.4:

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
    <url-pattern>/index.htm</url-pattern>    <<==  *1*
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.htm</welcome-file>   <<== *2*
</welcome-file-list>

so you no longer need redirect.jspin non-WEB-INFdirectory!!

所以你不再需要redirect.jsp在非WEB-INF目录中!!

回答by John Solomon

Just add a filter above Strut's filter in web.xmllike this:

只需在 Strut 的过滤器上方添加一个过滤器,web.xml如下所示:

<filter>
    <filter-name>customfilter</filter-name>
    <filter-class>com.example.CustomFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>customfilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

And add the following code in doFilter method of that CustomFilter class

并在该 CustomFilter 类的 doFilter 方法中添加以下代码

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
        FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest)servletRequest;
    HttpServletResponse httpResponse = (HttpServletResponse)servletResponse;
    if (! httpResponse.isCommitted()) {
        if ((httpRequest.getContextPath() + "/").equals(httpRequest.getRequestURI())) {
            httpResponse.sendRedirect(httpRequest.getContextPath() + "/MyAction");
        }
        else {
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }
}

So that Filter will redirect to the action. You dont need any JSP to be placed outside WEB-INF as well.

这样过滤器将重定向到操作。您也不需要将任何 JSP 放置在 WEB-INF 之外。