Struts2:在呈现index.jsp之前无法调用操作

时间:2015-01-25 13:53:45

标签: struts2

我已经搜索过这个,但是没有找到类似情况的相同症状。我在使用Sruts2并尝试在呈现应用程序的主页面之前调用一个动作(index.jsp)。我真的相信这个动作没有被调用,因为我在没有打印的动作的System.out.println()方法的开头有execute()(用于调试目的)。实际上,正在呈现index.jsp(因为它是默认页面),但是与该操作相关的部分未被运行。总之,我认为问题可能存在于struts.xml文件中。以下是struts.xml和操作文件:

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;
    }
 }

1 个答案:

答案 0 :(得分:1)

我终于设法实现了我想要的东西,而不需要重定向页面。基本上,解决方案是保留我的原始配置,但是更改JSP页面名称来自&#34; index.jsp&#34;到另一个(在我的情况下,它现在被称为&#34; user_main&#34; .jsp)。这样,奇怪的是,我猜它在使用默认名称&#34; index.jsp&#34;时有一些隐藏的默认配置参数。我的配置文件的内容:

<强>的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>