The struts2 agreement plugin is not working properly

I tried to run the application with the Struts2 agreement plugin. The application was fine with struts.xml configured as follows:

 <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 deleted this 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 try to run the application, I get the following error:

Messages:

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

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

If I am mistaken, any help would be appreciated. Best wishes.

+4
source share
1 answer

According to the message, Struts informs you that [hey] not found in your action configuration. In struts.xml you defined it without a slash. Do the same in the annotation. Do not map index.jsp , which can be handled by the container itself, but not Struts2. The name success is used by default, so this is optional.

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

Please note that @ResultPath not required.

+3
source

All Articles