4

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Aadam
  • 1,521
  • 9
  • 30
  • 60

1 Answers1

4

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • 1
    Thanks for the reply. I changed it to @Action(value="hey", results = { @Result(location="/success.jsp") }) and removed @ResultPath. Still the same problem. Any other solution please, not able to find on net also. :( – Aadam Apr 05 '13 at 09:37
  • did you try hey.action? did you rename index.jsp? There's also convention plugin on the classpath. – Roman C Apr 05 '13 at 09:42
  • hey.action also not working. I did not rename index.jsp. Ya conventionplugin is on class path I am using netbeans so i added it in libraries along with other jars. Is it required that i have to use POM.xml to describe the dependencies? I think netbeans will do this already wright? – Aadam Apr 05 '13 at 10:26
  • jars should be also in the WEB-INF/lib of deployed app. – Roman C Apr 05 '13 at 10:39
  • 1
    Thanks a lot, it is solved, i was missing asm-commons.jar. Thanks – Aadam Apr 05 '13 at 10:48