user1191027
user1191027

Reputation:

struts2 Pass URL to Action

I read the struts manual on wildcard mappings and decided to test some of the examples for myself. I have an action that points to:

<action name="**" method="getPerson" class="PersonActionBean">
      <result>/person/view.jsp</result>
</action>

This allows me to go anywhere past /person and see the view.jsp as far as I can understand it. So what I'm trying to do now is go to /person/jack/black then I want the getPerson method inside PersonActionBean class to get the URL fields jack and black and do a search in my DB by name and surname then populate an object that will be used on view.jsp

My concern isn't around the search functionality but around retrieving the fields in the URL from the method getPerson. How would I retrieve jack and black from the URL and use it in my getPerson method?

I'm using struts 2.1.8.1

Upvotes: 0

Views: 2757

Answers (2)

Jaiwo99
Jaiwo99

Reputation: 10017

Method 1 - With struts2-convention plugin

struts.xml

<constant name="struts.patternMatcher" value="namedVariable"/>

PersonAction.java

    import org.apache.struts2.convention.annotation.Namespace;
    ...
    @Namespace{"/persons/{param1}/{param2}");
    public class PersonActionBean exends ActionSupport {
        private String param1;
        private String param2;
        // getter and setter
    }

If you call persons/jack/black, the params should be set to param1 = jack, param2 = black

Method 2 - Without struts2-convention plugin

PersonAction.java

public class PersonActionBean exends ActionSupport {
    private String param1;
    private String param2;
    // getter and setter
}

person.xml

<package name="person" namespace="/person" extends="website">
    <action name="*/*" method="getPerson" class="PersonActionBean">
            <param name="param1">{1}</param>
            <param name="param2">{2}</param>
            <result>/person/view.jsp</result>
    </action>   
</package>

struts.xml

<package name="website" namespace="/" extends="struts-default, json-default">
     ...
    <constant name="struts.enable.SlashesInActionNames" value="true"/>
    <constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
     ...
</package>

References

Check out Advanced Wildcard

Upvotes: 1

Pugal
Pugal

Reputation: 21

Use <s:Property> tag

Why not you go for property tag. better u had send those names via property tag.

<s:url action="PersonActionBean" var="urlPersonActionBean" >
    <s:param name="name1">no</s:param>
    <s:param name="name2">no</s:param>
</s:url>

<a href="<s:property value="#urlPersonActionBean"/>"  target="content">

i am sure this will help you...

Upvotes: 0

Related Questions