struts2 convention插件无法正常工作

时间:2013-04-05 08:05:02

标签: java struts2 struts-validation action-mapping struts2-convention-plugin

我试图使用常规插件Struts2运行该应用程序。该应用程序很好,struts.xml配置如下:

<struts>

    <package name="struts2demo" extends="struts-default">
    <action name="hey" class="action.CountryAction" method="get">
       <result name="success">/index.jsp</result>
    </action>
    <action name="add" class="action.CountryAction" method="add">
       <result type="redirect" name="success">hey</result>
    </action>
    <!-- Add your actions here -->
    </package>

</struts>

现在我删除了struts.xml并添加了一些这样的注释:

@Namespace("/")
@ResultPath(value="/")
public class CountryAction extends ActionSupport implements ModelDriven<Country>{
    private List<Country> worldCountry;
    private Country country = new Country();



    public Country getCountry() {
            return country;
        }

    public void setCountry(Country country) {
            this.country = country;
        }

 //   HttpServletRequest request;
@Action(value="/hey",results={@Result(name="success",location="/index.jsp")})
    public String get() throws SQLException
    {
        CountryService cs = new CountryService();
        setWorldCountry(cs.getCountry());
      //  System.out.println(getWorldCountry());
        return SUCCESS;
    }

     public List<Country> getWorldCountry() {
        return worldCountry;
    }

    public void setWorldCountry(List<Country> worldCountry) {
        this.worldCountry = worldCountry;
    }

    @Override
    public Country getModel() {
        return country;
    }
}

但是当我尝试运行应用程序时,我收到以下错误:

消息:

There is no Action mapped for namespace [/] and action name [hey] associated with context path [/JustStruts2].

我的web.xml就是这样:

<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
         <param-name>struts.devMode</param-name>
         <param-value>true</param-value>
      </init-param>
    </filter>

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

如果我做错了,我们将不胜感激任何帮助 问候。

1 个答案:

答案 0 :(得分:3)

根据消息,Struts会通知您在您的操作配置中找不到[hey]。在struts.xml中,您没有使用斜杠定义它。在注释中执行相同操作。不要映射可以由容器本身处理的index.jsp,而不是由Struts2处理。默认情况下使用名称“success”,因此没有必要。

@Action(value="hey", results = { @Result(location="/page.jsp") })

请注意,@ResultPath不是必需的。