[ACCEPTED]-How can I set the welcome page to a struts action?-struts

Accepted answer
Score: 23

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

So, in particular, I'd replace the 5

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

in index.jsp with

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

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

Score: 17

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

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

As of the 2.4 version of the Servlet specification 6 you are allowed to have a servlet in the 5 welcome file list. Note that this may not 4 be a URL (such as /myproject/MyAction.action). It 3 must be a named servlet and you cannot pass 2 a query string to the servlet. Your controller 1 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>
Score: 6

"Surely there's a better way!"

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

The 13 purpose of this mechanism is to allow the 12 deployer to specify an ordered list of 11 partial URIs for the container to use for 10 appending to URIs when there is a request 9 for a URI that corresponds to a directory 8 entry in the WAR not mapped to a Web component.

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

Score: 6

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

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

Score: 1

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

Score: 1

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

Score: 1

Here two blogs with same technique:

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

Score: 1

there are this answer above but it is not clear about web app context so i 4 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 3 index definition, named index and it all togather 2 work fine and not depends on webapp context 1 path.

Score: 0

Just add a filter above Strut's filter in 5 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 4 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 3 that Filter will redirect to the action. You 2 dont need any JSP to be placed outside WEB-INF 1 as well.

Score: 0

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

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

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

w/o 3 tiles

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

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

Score: 0

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

Additionally, I 5 find it can be combined with Spring security 4 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 3 over the first page to be the login form, but 2 we control the flow AFTER user is login in, depending 1 on his role. Works like a charm.

Score: 0

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

Execute some Action before 3 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 2 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 1 not needed in web.xml

More Related questions