Question

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>

Was it helpful?

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.

OTHER TIPS

I found that the filter mappings of

    <url-pattern>*.action</url-pattern>
    <url-pattern>/struts/*</url-pattern>
    <url-pattern>/</url-pattern>

were preventing the redirect to my welcome file.

the pattern of

    <url-pattern>/*</url-pattern>

fixed the problem.

For action as defaut page this works for me

<welcome-file-list>     
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/index.jsp</url-pattern>
</filter-mapping>

index.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:action name="welcome21" namespace="/" executeResult="true" />

struts.xml (jsp result not tiles)

<package name="default" namespace="/" extends="struts-default">
        <result-types>
            <result-type name="tiles"
                class="org.apache.struts2.views.tiles.TilesResult" />
        </result-types>


        <action name="welcome21" class="org.zzz.action.CommonAction" method="welcome">
            <result>/jsp/welcome2.jsp</result>          
        </action>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top