0

I've searched for this all over SO, but didn't find a similar situation with the same symptoms. I'm using Sruts2 and trying to invoke an action before rendering the main page of my application (index.jsp). I truly believe that this action is not being called, because I have a System.out.println() (for debugging purposes) in the beginning of the execute() method of the action that is not being printed. Indeed index.jsp is being presented (because it is the default page), but the part that is related to the action is not being run. In conclusion, I think that the problem may reside in the struts.xml file. Below are both the struts.xml and the action file:

Struts.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!-- The core configuration file for the framework is the default (struts.xml) file
and should reside on the classpath of the webapp (generally /WEB-INF/classes). -->

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

  <!-- devMode equals debug information and reload everything for every request -->
  <constant name="struts.devMode" value="true" />
  <constant name="struts.ui.theme" value="simple" />

  <package name="faultinjector" extends="struts-default">

    <default-action-ref name="loadexperiments" />

    <action name="loadexperiments" class="faultinjector.action.LoadExperimentsAction" method="execute">
      <result name="success">/index.jsp</result>
    </action>

  </package>

</struts>

LoadExperimentsAction.java:

public class LoadExperimentsAction extends ActionSupport
{
    private static final long serialVersionUID = 4L;

    private ExperimentService service;
    private List <Experiment> experiments;

    @Override
    public String execute()
    {
        System.out.println("Hello!");

        return SUCCESS;
    }

    public ExperimentService getService()
    {
        return service;
    }

    public void setService(ExperimentService service)
    {
        this.service = service;
    }

    public List<Experiment> getExperiments()
    {
        return experiments;
    }

    public void setExperiments(List<Experiment> experiments)
    {
        this.experiments = experiments;
    }
 }
Roman C
  • 49,761
  • 33
  • 66
  • 176
João Fernandes
  • 558
  • 3
  • 11
  • 29
  • Are you going to `index.jsp` or `loadexperiments`. It is likely that your webserver is simply rendering index.jsp without calling Struts at all. You need to change your webserver config. – Boris the Spider Jan 25 '15 at 14:08
  • Is Struts2 filter configured to for the request you made? You might have misconfiguration or absence some important files required to function properly. You have also not include any helpful information in the post, so it's impossible to help. – Roman C Jan 25 '15 at 14:27
  • 1
    possible duplicate of [How can I set the welcome page to a struts action?](http://stackoverflow.com/questions/39399/how-can-i-set-the-welcome-page-to-a-struts-action) – Aleksandr M Jan 26 '15 at 08:58
  • 1
    Ok, by webserserver config you mean web.xml? If so, this is the content of it: ` struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* ` I also tried appending ` index.jsp ` to it, with no success. – João Fernandes Jan 26 '15 at 11:11
  • 1
    Ok, I've followed @AleksandrM 's suggestion, and did as the OP in the suggested question. It is now working (at least I know I can access the defined action). What I made was to create a new index.jsp file only with `<% response.sendRedirect("/FaultInjector/LoadExperiments.action"); %>` on it, changed in struts.xml `/user_main.jsp` and finally put the original index.jsp content in user_main.jsp. I guess there is a more elegant way to achieve this, if someone has a hint please let me know. Thanks for all the efforts. – João Fernandes Jan 26 '15 at 11:34

1 Answers1

1

I finally managed to achieve what I was looking for, without the need of redirecting pages. Basically the solution is to keep my original configuration, but changing the JSP page name from "index.jsp" to another one (in my case, it is now called "user_main".jsp). This way, it oddly works, I guess it has some hidden default configuration parameter when using the default name "index.jsp". Content of my configuration files:

web.xml:

<web-app id="faultinjector" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

struts.xml:

<struts>
  <constant name="struts.devMode" value="true" />
  <constant name="struts.ui.theme" value="simple" />

  <package name="faultinjector" extends="struts-default">

  <default-action-ref name="loadexperiments" />

  <action name="loadexperiments" class="faultinjector.action.LoadExperimentsAction" method="execute">
      <result name="success">/user_main.jsp</result>
    </action>
  </package>
</struts>
João Fernandes
  • 558
  • 3
  • 11
  • 29