Why is my struts annotation redirection not working?

124 Views Asked by At

I would like to redirect to fail.jsp when i type my root url (localhost:8081/). Here is my java class Root :

@Namespaces(value={@Namespace("/User"), @Namespace("/")})
@Result(location="/fail.jsp")
@Actions(value={@Action(""), @Action("home")})

public class Root {

    public String execute() throws Exception {
        return "success";
    }
}

Currently, it redirects me to index.jsp. Most online tutorials mention the web.xml but some people tells me it has nothing to do with web.xml. So i'm confused. May someone help me understand what i'm missing ?

Here is the tree of my app :

enter image description here

2

There are 2 best solutions below

4
On

Found out i either need an .xml setup or a proper Spring & Struts integration to get rid of all xml.

Here is the tutorial for the 2nd option

0
On

You have index.jsp in the web root folder. If you requested to show this folder, and web server found that there's index.jsp in that folder it redirects to index.jsp. This code is from Tomcat's web.xml:

  <!-- ==================== Default Welcome File List ===================== -->
  <!-- When a request URI refers to a directory, the default servlet looks  -->
  <!-- for a "welcome file" within that directory and, if present, to the   -->
  <!-- corresponding resource URI for display.                              -->
  <!-- If no welcome files are present, the default servlet either serves a -->
  <!-- directory listing (see default servlet configuration on how to       -->
  <!-- customize) or returns a 404 status, depending on the value of the    -->
  <!-- listings setting.                                                    -->
  <!--                                                                      -->
  <!-- If you define welcome files in your own application's web.xml        -->
  <!-- deployment descriptor, that list *replaces* the list configured      -->
  <!-- here, so be sure to include any of the default values that you wish  -->
  <!-- to use within your application.                                      -->

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

If you remove index.jsp from the root folder, your code might work if it has correct mapping to the action. You might also find this question Can Struts 2 First Page come through the engine instead of listing it in and answer useful to you.