struts2 conventions plugin not working properly

自闭症网瘾萝莉.ら 提交于 2019-12-06 06:16:06

问题


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:


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.



来源:https://stackoverflow.com/questions/15828845/struts2-conventions-plugin-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!