Pregunta

I'm getting confusion to converting the codebehind annotation to conventional struts.xml file.

How to identify action name in action class? Because if write method public String list{} - action its match with JSPs product-list.jsp will automatically identify the page and URL was product!list like that. What is going to be conventional plugin ?

current URL :

http://localhost:7001/example/product!search- jsp name product-search.jsp and ProductAction - action class.

Please tell me how to configure the struts.xml file which equivalent above config.

I have tried like below:

<package name="example" namespace="/" extends="struts-default">
  <action name="Search">
    <result>product-search.jsp</result>
  </action>         
</package>

Error :

org.apache.struts2.dispatcher.Dispatcher - Could not find action or result
There is no Action mapped for namespace / and action name part. - [unknown location]
¿Fue útil?

Solución

In Struts2 actions are mapped on methods. The above url you should map as

<package name="example" namespace="/" extends="struts-default">
   <action name="product" method="search"> <!-- case sensitive -->
     <result>product-search.jsp</result>
   </action>
</package>

or via annotations

@Namespace("/")
public class ProductAction extends ActionSupport {

  public String execute() {
    return SUCCESS;

  }

  @Action(value="product",
    results=@Result(location="/product-list.jsp")
  )
  public String search() {
    return SUCCESS;
  }
}

Notice, that the method execute is not mapped, so it will not execute. If you need that method execute you should create mapping to it. For this purpose you could place annotation on class or on method execute.

More examples you could find on the page Convention Plugin.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top