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

15

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!

Hypoplasia answered 2/9, 2008 at 12:40 Comment(0)
F
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.

Foushee answered 4/9, 2008 at 14:31 Comment(4)
This also will work if your app deployed on not-ROOT path. Redirect above will work if you strip the first "/".Sevenfold
forward doesn't work for me while sendRedirect does work. Got '404 Not Found'.Elswick
Apparently this won't work if you don't specify the full URL (including the domain etc), but @Hypoplasia 's approach worked for us!Charlatanism
Same, also got 404 with <jsp:forward>.Monecious
C
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" />
Convolve answered 30/1, 2011 at 12:53 Comment(2)
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.Entablement
This is a struts 2 only way.Noteworthy
E
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>
Educate answered 25/11, 2008 at 21:1 Comment(2)
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.Educate
Worked for me; thanks for the tip! Previously I had used the following: <%@ taglib prefix="struts" uri="http://struts.apache.org/tags-logic" %><struts:redirect forward="login.action" />Estevan
Z
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.

Zamir answered 3/9, 2008 at 21:13 Comment(3)
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.Estevan
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".Zamir
Anyway, maybe that was a WebWork question and not a Struts question after all :-)Zamir
N
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.

Nitrosamine answered 21/11, 2009 at 13:37 Comment(0)
R
1

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

Rusch answered 2/9, 2008 at 14:6 Comment(0)
S
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).

Sevenfold answered 2/9, 2008 at 21:39 Comment(0)
C
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!!

Casque answered 21/3, 2013 at 15:19 Comment(0)
D
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.

Desulphurize answered 10/8, 2013 at 14:20 Comment(0)
P
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

Phantasy answered 28/4, 2016 at 15:47 Comment(0)
W
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.

Waste answered 9/4, 2013 at 15:1 Comment(0)
R
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...

Runnel answered 19/5, 2013 at 19:38 Comment(0)
M
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.

Mcadams answered 20/11, 2015 at 11:39 Comment(0)
E
-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>
Emblazonment answered 11/2, 2013 at 12:1 Comment(1)
This doesn't work. Probably because the <welcome-file-list> 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.Monecious
H
-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.

Handpick answered 19/4, 2013 at 20:44 Comment(1)
This doesn't work. Probably because the <welcome-file-list> 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.Monecious

© 2022 - 2024 — McMap. All rights reserved.