40

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!

Roman C
  • 49,761
  • 33
  • 66
  • 176
mrowe
  • 841
  • 1
  • 7
  • 6

15 Answers15

23

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.

Martin McNulty
  • 2,601
  • 3
  • 22
  • 26
17

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" />
Srikanth
  • 2,014
  • 19
  • 22
  • The best answer as you don't hard code the `struts.action.extension` in your jsp. The `struts.action.extension` may be changed to something other than `action` and then you need to change the `request.redirect` url in this jsp too. – Alireza Fattahi Feb 24 '16 at 16:34
  • 1
    This is a struts 2 only way. – VeenarM May 28 '16 at 05:35
11

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>
Craig Wohlfeil
  • 627
  • 9
  • 9
  • 1
    This solution is framework agnostic. You can use it with anything, you just have to figure out how to specify a default action with your chosen framework. – Craig Wohlfeil Mar 15 '12 at 00:29
  • 1
    Worked for me; thanks for the tip! Previously I had used the following: `<%@ taglib prefix="struts" uri="http://struts.apache.org/tags-logic" %>` – guymac Sep 19 '14 at 01:40
6

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.

Nischal
  • 958
  • 2
  • 10
  • 14
6

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

Damien B
  • 1,992
  • 15
  • 19
  • This is incorrect. You can map particular extensions to the default (static content) servlet and anything/everything else regardless of extension to the Struts action servlet. – guymac Sep 19 '14 at 01:38
  • If I remember correctly (that was 6 years ago after all), in Struts 1 you are required to have your action URLs having a specific extension, because this is how the mapping is performed (URL -> strip extension -> resolve module -> Action). Of course that doesn't apply to WebWork aka "Struts 2". – Damien B Sep 21 '14 at 18:46
  • Anyway, maybe that was a WebWork question and not a Struts question after all :-) – Damien B Sep 21 '14 at 18:47
1

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

gavenkoa
  • 45,285
  • 19
  • 251
  • 303
1

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.

Community
  • 1
  • 1
msangel
  • 9,895
  • 3
  • 50
  • 69
1

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

Ravi MCA
  • 2,491
  • 4
  • 20
  • 30
1

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

bpapa
  • 21,409
  • 25
  • 99
  • 147
1

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

Georgy Bolyuba
  • 8,355
  • 7
  • 29
  • 38
0

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.

zb226
  • 9,586
  • 6
  • 49
  • 79
John Solomon
  • 119
  • 1
  • 3
  • 17
0

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

siva
  • 71
  • 5
0

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.

WesternGun
  • 11,303
  • 6
  • 88
  • 157
-1

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>
Navathej
  • 7
  • 1
  • 1
    This doesn't work. Probably because the `` looks for a _physical_ file hence "welcome FILE list". That's why putting an empty file with the same name as the action name in the app root (WebContent) folder works. – k_rollo Jan 04 '15 at 15:35
-1

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.

Lund Wolfe
  • 319
  • 3
  • 5
  • 1
    This doesn't work. Probably because the `` looks for a _physical_ file hence "welcome FILE list". That's why putting an empty file with the same name as the action name in the app root (WebContent) folder works. – k_rollo Jan 04 '15 at 15:34