struts2 conventions plugin not working properly

2019-04-09 18:42发布

I was trying to run the application with convention plugin Struts2. The application was fine with struts.xml configured like this:

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

now I removed that struts.xml and added some annotations like this:

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

but when I am trying to run the application i am getting the following error:

Messages:

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

My web.xml is this:

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

Where I am doing wrong, any help will be appreciated.
Regards.

1条回答
萌系小妹纸
2楼-- · 2019-04-09 19:26

According to the message the Struts inform you [hey] not found in your action configuration. In the struts.xml you defined it without slash. Do the same in the annotation. Don't map index.jsp that could be handled by the container itself but not by Struts2. The name "success" is used by default, so it's not necessary.

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

Note that @ResultPath is not necessary.

查看更多
登录 后发表回答