Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use wildcard method invocation in Struts2 with conventions plugin using only annotations?

I know how to use wildcard method invocation within struts.xml, but is it possible to do this with annotations? If so how?

like image 977
Quaternion Avatar asked May 17 '11 22:05

Quaternion


1 Answers

You probably have it resolved by now, but for those looking for an answer, yes it is possible.

For wildcard mapping, refer to: http://struts.apache.org/2.3.1.2/docs/wildcard-mappings.html

To read params from the url, annotate your method with this:

private String id;

@Action(value = "edit/{id}",
        results={@Result(name = "success", location = "/WEB-INF/views/devices/edit.ftl")}
)        
public String edit() {  
    // do something
    return SUCCESS;
}

public void setId(String id) {
    this.id = id;
}

public String getId() {
    return id;
}
  • You will need a getter/setter for the id parameter.
  • You need to specify the results since struts2 won't know what to use for success for the url: ...edit/123, hence the need to use @Result to point to your file. Kinda defits the purpose of the convention plugin there.

In the case I want to redirect to a specific url, use this annotation:

@Action(value = "index",
        results={@Result(name = "success", location = "/devices/edit/${entityId}", type = "redirect")}
    )

You would need a getter/setter for the entityId (String in my case).

You can also have advanced wilcard mapping, namespace wildcard mapping ...

Don't forget to change the struts.xml and add the following constants.

<!-- Used for advanced wilcard mapping -->
<constant name="struts.enable.SlashesInActionNames" value="true"/>
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<constant name="struts.patternMatcher" value="regex" />
like image 75
ontk Avatar answered Oct 05 '22 00:10

ontk