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

发布于 2024-07-04 05:40:03 字数 402 浏览 7 评论 0 原文

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

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

和在 index.jsp 中:

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

当然有更好的方法!

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:

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

and in index.jsp:

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

Surely there's a better way!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

筑梦 2024-07-11 05:40:06

我几乎确信OP是最好的解决方案(不确定最佳实践,但它工作完美,实际上是我的项目负责人和我更喜欢的解决方案。)

此外,我发现它可以与Spring安全性结合起来,如下所示:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

<sec:authorize access="isAnonymous()">
    <% response.sendRedirect("/myApp/login/login.action?error=false"); %>
</sec:authorize>
<sec:authorize access="isAuthenticated() and (hasRole('ADMIN') or hasRole('USER'))">
    <% response.sendRedirect("/myApp/principal/principal.action"); %>
</sec:authorize>
<sec:authorize access="isAuthenticated() and hasRole('USER')">
    <% response.sendRedirect("/myApp/user/userDetails.action"); %>
</sec:authorize>

通过这种方式,我们不仅可以控制作为登录表单的第一页,而且可以根据用户的角色控制用户登录后的流程。 奇迹般有效。

I am almost sure that the OP is the best solution(not sure about best practice, but it works perfectly, and actually is the solution my project leader and I prefer.)

Additionally, I find it can be combined with Spring security like this:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

<sec:authorize access="isAnonymous()">
    <% response.sendRedirect("/myApp/login/login.action?error=false"); %>
</sec:authorize>
<sec:authorize access="isAuthenticated() and (hasRole('ADMIN') or hasRole('USER'))">
    <% response.sendRedirect("/myApp/principal/principal.action"); %>
</sec:authorize>
<sec:authorize access="isAuthenticated() and hasRole('USER')">
    <% response.sendRedirect("/myApp/user/userDetails.action"); %>
</sec:authorize>

By this, not only we have control over the first page to be the login form, but we control the flow AFTER user is login in, depending on his role. Works like a charm.

荒人说梦 2024-07-11 05:40:06

这对我来说也很好:

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

当用户使用 Web 应用程序的根目录 (mywebapp/) 进入 Web 应用程序时,我无法获得要执行的默认操作。 struts 2.3.12 中有一个 bug,当您使用 root url 时,不会进入默认操作或使用欢迎页面。 这将是一个常见的现象。 一旦我改回struts 2.1.8,它就工作得很好。

This worked fine for me, too:

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

I was not able to get the default action to execute when the user enters the webapp using the root of the web app (mywebapp/). There is a bug in struts 2.3.12 that won't go to the default action or use the welcome page when you use the root url. This will be a common occurrence. Once I changed back to struts 2.1.8 it worked fine.

隔岸观火 2024-07-11 05:40:06

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

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

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

<welcome-file-list>
<welcome-file>/MyAction.action</welcome-file>
</welcome-file-list>
梦情居士 2024-07-11 05:40:06

我的配置如下。 它工作完美,也没有 URL 更改...

创建一个虚拟操作,如 struts2.xml 文件中的以下内容。 因此,每当我们访问像 http://localhost:8080/myapp 这样的应用程序时,它都会将其转发到虚拟操作,然后重定向到index.jsp/index.tiles...

<action name="">
    <result type="tiles">/index.tiles</result>
</action>

没有瓷砖

<action name="">
    <result>/index.jsp</result>
</action>

可能 我们在 web.xml 中配置一些动作index.action为index.action,并使用该动作转发所需的页面...

I have configured like following. it worked perfect and no URL change also...

Create a dummy action like following in struts2.xml file. so whenever we access application like http://localhost:8080/myapp, it will forward that to dummy action and then it redirects to index.jsp / index.tiles...

<action name="">
    <result type="tiles">/index.tiles</result>
</action>

w/o tiles

<action name="">
    <result>/index.jsp</result>
</action>

may be we configure some action index.action in web.xml as <welcome-file>index.action</welcome-file>, and use that action to forward required page...

隐诗 2024-07-11 05:40:06

下面的代码可以在struts.xml中使用来加载欢迎页面。

在加载欢迎页面之前执行一些操作。

<!-- welcome page configuration -begin -->
    <action name="" class="com.LoginAction">
        <result name="success">login.jsp</result>
    </action>
<!-- welcome page configuration -end -->

直接返回一些 JSP,而不执行 Action。

<!-- welcome page configuration -begin -->
    <action name="">
        <result name="success">login.jsp</result>
    </action>
<!-- welcome page configuration -end -->

web.xml 中不需要

Below code can be used in struts.xml to load welcome page.

Execute some Action before loading a welcome page.

<!-- welcome page configuration -begin -->
    <action name="" class="com.LoginAction">
        <result name="success">login.jsp</result>
    </action>
<!-- welcome page configuration -end -->

Return directly some JSP without execution of an Action.

<!-- welcome page configuration -begin -->
    <action name="">
        <result name="success">login.jsp</result>
    </action>
<!-- welcome page configuration -end -->

No <welcome-file-list> is not needed in web.xml

メ斷腸人バ 2024-07-11 05:40:06

上面有这个答案,但不清楚网络应用程序上下文
所以
我这样做:

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
    <servlet-name>TilesDispatchServlet</servlet-name>
    <servlet-class>org.apache.tiles.web.util.TilesDispatchServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>TilesDispatchServlet</servlet-name>
    <url-pattern>*.tiles</url-pattern>
</servlet-mapping>

index.jsp 中,我只是写:

<jsp:forward page="index.tiles" />

我有索引定义,名为 index ,它一起工作正常,不依赖于 webapp 上下文路径。

there are this answer above but it is not clear about web app context
so
i do this:

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
    <servlet-name>TilesDispatchServlet</servlet-name>
    <servlet-class>org.apache.tiles.web.util.TilesDispatchServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>TilesDispatchServlet</servlet-name>
    <url-pattern>*.tiles</url-pattern>
</servlet-mapping>

And in index.jsp i just write:

<jsp:forward page="index.tiles" />

And i have index definition, named index and it all togather work fine and not depends on webapp context path.

节枝 2024-07-11 05:40:06

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

它需要 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>

因此您不再需要非 WEB-INF 中的 redirect.jsp代码>目录!!

Here two blogs with same technique:

It require 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.jsp in non-WEB-INF directory!!

奶气 2024-07-11 05:40:06

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

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

〃安静 2024-07-11 05:40:06

我将创建一个过滤器并通过转发响应将所有请求退回 root。 创建 home.do 页面的黑客对我来说看起来很丑(你还需要记住一件事,并为支持你的代码的人进行调查)。

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).

戏舞 2024-07-11 05:40:06

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

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

并在 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);
        }
    }
}

这样 Filter 就会重定向到该操作。 您也不需要将任何 JSP 放置在 WEB-INF 之外。

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

<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

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.

雨落□心尘 2024-07-11 05:40:05

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

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

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.

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.

南冥有猫 2024-07-11 05:40:05

“当然还有更好的办法!”

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

该机制的目的是允许部署者指定一个有序的
容器的部分 URI 列表,用于在存在时附加到 URI
请求与 WAR 中未映射的目录条目相对应的 URI
一个 Web 组件。

您无法将 Struts 映射到“/”,因为 Struts 需要使用文件扩展名。 因此,您只能使用隐式映射的组件,例如 JSP 或静态文件。 所有其他解决方案都只是黑客。 因此,保留您的解决方案,它是完全可读和可维护的,不必费心进一步寻找。

"Surely there's a better way!"

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

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.

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.

动听の歌 2024-07-11 05:40:05

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

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>
  <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>
定格我的天空 2024-07-11 05:40:04

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

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

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

<s:action name="loadHomePage" namespace="/load" executeResult="true" />
笔落惊风雨 2024-07-11 05:40:03

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

因此,我特别将

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

index.jsp 中的 替换为 此

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文