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

后端 未结 15 1965
天涯浪人
天涯浪人 2020-11-29 20:15

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 welc

相关标签:
15条回答
  • 2020-11-29 20:52

    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" />
    
    0 讨论(0)
  • 2020-11-29 20:52

    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.

    0 讨论(0)
  • 2020-11-29 20:55

    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.

    0 讨论(0)
  • 2020-11-29 20:56

    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>
    
    0 讨论(0)
  • 2020-11-29 20:59

    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.

    0 讨论(0)
  • 2020-11-29 21:00

    Here two blogs with same technique:

    • http://technologicaloddity.com/2010/03/25/spring-welcome-file-without-redirect/
    • http://wiki.metawerx.net/wiki/HowToUseAServletAsYourMainWebPage

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

    0 讨论(0)
提交回复
热议问题