-->

Struts2的约定插件不能正常工作(struts2 conventions plugin not

2019-08-19 17:32发布

我试图运行与惯例插件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>

我哪里做错了,任何帮助将不胜感激。
问候。

Answer 1:

据消息Struts的通知你[hey]不在你的动作配置中找到。 在struts.xml ,你定义它没有斜线。 不要在注释中的相同。 不要映射index.jsp ,可以通过容器本身而不是由Struts2的处理。 命名为“成功”默认情况下使用,所以它是没有必要的。

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

需要注意的是@ResultPath是没有必要的。



文章来源: struts2 conventions plugin not working properly