0

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

2 Answers2

1

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

Jaiwo99
  • 9,687
  • 3
  • 36
  • 53
  • i don't understand your question, like i said, if you have `/persons/jack/black`, `param1` should be set to `jack`, `param2` should be set to `black` – Jaiwo99 Sep 03 '12 at 15:03
  • hi, see your edit `Method 2`, if you do not wanna use convention plugin, how could you possible `import org.apache.struts2.convention.annotation.Namespace;`, you cannot address {0} and {1} with your both params, how could struts2 possible do this.. – Jaiwo99 Sep 05 '12 at 13:10
  • can i rollback my answer and give you some new idea? with your edit i cannot express it clearly.. – Jaiwo99 Sep 05 '12 at 13:15
  • sry, please try again with `{2} and {3}` – Jaiwo99 Sep 05 '12 at 13:47
  • can you please add your action `pacakge` info, like `` – Jaiwo99 Sep 05 '12 at 13:50
  • so are you calling `/persons/jack/black` or `/person/jack/black` – Jaiwo99 Sep 05 '12 at 13:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16304/discussion-between-jaiwo99-and-threat) – Jaiwo99 Sep 05 '12 at 14:02
0

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...

Umesh Awasthi
  • 23,407
  • 37
  • 132
  • 204
Pugal
  • 21
  • 4