JR Galia
JR Galia

Reputation: 17269

Struts 2 Dynamic Url to be used in Search

How to treat URL string as parameter without having the "?" character after the action name?

Let's say for example we have usernames from database: jr, jack, j, etc...

What I want is to view their profile using domain.com/user/jr, domain.com/user/jack, domain.com/user/j, etc.

Is there a way for these?

From the example, jr, jack, j are dynamic and the key to be used in viewing their profile.

Upvotes: 1

Views: 687

Answers (2)

lschin
lschin

Reputation: 6811

Use Wildcard mappings.

An example for Action Annotations (Convention Plugin) :

struts.xml

<constant name="struts.enable.SlashesInActionNames" value="true" />
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false" />
<constant name="struts.patternMatcher" value="regex" />

@Action(value = "user/{userName}", results = {
        @Result(location = "user.jsp")
})
public class UserAction extends ActionSupport implements Preparable {

    private String userName; // with getter & setter

    public void prepare() {
        // getUserByUserName(userName)
    }
}

Upvotes: 1

Shawn D.
Shawn D.

Reputation: 8125

I think what you're looking for is the Struts 2 REST plug-in, since you're looking for REST-style URLs.

http://struts.apache.org/2.x/docs/rest-plugin.html

I don't remember if having the plug-in replaces the normal 'action' class mapping, but I think it might. That is, I think after having it present, all your actions have to be mapped by convention as specified in the info I linked.

Good luck.

Upvotes: 0

Related Questions