Search code examples
javaurlstruts2home-directorywelcome-file

Struts 2 Default Page


I have a a web application using struts two that can access a action by literally typing it into the URL

localhost/project/index.action

but it won't redirect to this default action without explicitly typing it in.

localhost/project/

gives the error

Error 404: SRVE0190E: File not found: {0}

I am assuming the file isn't found because I don't have something configured correctly.

Can anyone point me in the right direction as for getting this behavior with Struts 2?

Per request here is my struts.xml and web.xml

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Doc Parser</display-name>

<filter>
    <filter-name>struts-prepare</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
</filter>
<filter>
    <filter-name>struts-execute</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>
</filter>
<filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>struts-prepare</filter-name>
    <url-pattern>*.action</url-pattern>
    <url-pattern>/struts/*</url-pattern>
    <url-pattern>/</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>*.action</url-pattern>
    <url-pattern>/struts/*</url-pattern>
    <url-pattern>/</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>struts-execute</filter-name>
    <url-pattern>*.action</url-pattern>
    <url-pattern>/struts/*</url-pattern>
    <url-pattern>/</url-pattern>
</filter-mapping>

<listener>
    <listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class>
</listener>

<servlet>
    <servlet-name>sitemesh-freemarker</servlet-name>
    <servlet-class>org.apache.struts2.sitemesh.FreemarkerDecoratorServlet</servlet-class>
    <init-param>
        <param-name>default_encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet>
    <servlet-name>sitemesh-velocity</servlet-name>
    <servlet-class>org.apache.struts2.sitemesh.VelocityDecoratorServlet</servlet-class>
    <init-param>
        <param-name>default_encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>


<servlet-mapping>
    <servlet-name>sitemesh-freemarker</servlet-name>
    <url-pattern>*.ftl</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>sitemesh-velocity</servlet-name>
    <url-pattern>*.vm</url-pattern>
</servlet-mapping>

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

Strust.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">

<constant name="struts.freemarker.templatesCache" value="true" />

<constant name="site.framework.roles"
    value="
    free => http://security.site.com/service/mwstutorial/getItems,
    loggedIn => http://security.site.com/service/mwstutorial/setItems" />

<package name="blank" extends="default">

</package>


Solution

  • I have a slightly different approach. And it does not matter if it is Struts 2 or even java.

    Just add an index.html in the root of your web application that does a redirect.

    <meta http-equiv="refresh" content="0; url="http://localhost/home.action" />
    

    Alternately, you can use javascript.

    <script type="text/javascript">
                window.location.href = "http://localhost/home.action"
            </script>
    

    That way you don't need to worry about welcome files as well as struts.xml configuration. Plus you can use this approach in other applications too.