2

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>

Roman C
  • 49,761
  • 33
  • 66
  • 176
9er
  • 1,604
  • 3
  • 20
  • 37
  • This can be achieved by working on web.xml and / or struts.xml. Show both your files to get a better help – Andrea Ligios Apr 02 '14 at 14:31
  • just added them, I was wondering if it might have something to do with the tag? – 9er Apr 02 '14 at 14:35
  • 1
    Are you using annotations? – PbxMan Apr 02 '14 at 14:41
  • Try to add a convention plugin it will handle such situation. – Roman C Apr 02 '14 at 14:45
  • yes i am using struts conventions already – 9er Apr 02 '14 at 14:45
  • @Results({ @Result(name = "success", location="index.jsp") }) @Action("index") – 9er Apr 02 '14 at 14:45
  • that the annotations i used for my index action – 9er Apr 02 '14 at 14:46
  • Did you try to remove welcome-file list? – Roman C Apr 02 '14 at 14:47
  • I did. It didn't work but gave me a different error NoTargetForURIException: No target servlet configured for uri: /project/ – 9er Apr 02 '14 at 14:50
  • Which one (a different error)? Did you use `@ResultPath` or where your jsps are? – Roman C Apr 02 '14 at 14:52
  • I did not use @ResultPath and the error is listed in my previous comment. I didn't give it any other welcome file location such as where my jsps are i simply removed it to see what would happen. – 9er Apr 02 '14 at 14:54
  • I shouldn't need to use '@ResultPath' though because I change the locaiton of my jsps in the struts.xml file (i.e ) – 9er Apr 02 '14 at 14:57
  • What happened? Your struts filter isn't mapped to a folder location and uses an action extension for actions. What would be better to use a default action or index action if map to the folder location. The convention plugin should handle it. Why don't you map a struts filter to `/*`? – Roman C Apr 02 '14 at 14:59
  • Nothing happened. I got the NoTargetForURIException. What do you mean by my struts filter isn't mapped to a folder location? – 9er Apr 02 '14 at 15:06
  • Oh I see what you mean! Thanks. i used a url-pattern of /*. I will post an answer for others. – 9er Apr 02 '14 at 15:10
  • @9er Check if it works first. – Roman C Apr 02 '14 at 15:11
  • 1
    it does indeed work :) Thanks @RomanC – 9er Apr 02 '14 at 15:37

3 Answers3

2

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.

9er
  • 1,604
  • 3
  • 20
  • 37
2

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.

Mukus
  • 4,870
  • 2
  • 43
  • 56
1

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>
sanek
  • 86
  • 8