Expressive with experience,Correction and goal is always in mind

Very recently I started blogging wanted to share my own experience to make my self better and better

Wednesday, February 2, 2011

Struts wild card search in URL

I have been completely offline with my blogs as travelling keep me busy between Pune and Mumbai..Of course I can't miss out time with my family on weekend for sure.

Recently One of my friend requested to how to perform wild card search in struts2 when search parameter is included in URL.say for example user can search on http://www.retailstore.com/catalog/shirts so now for application should search on search parameters.

So here is the easy solution :


Include following lines in struts.xml :

  <"struts.enable.SlashesInActionNames" value="true"/>
 <"struts.mapper.alwaysSelectFullNamespace" value="false"/>



Now include an action in struts.xml :

<package name="catalog" extends="struts-default" namespace="/catalog"/>
    <action name="/catalog/*" class="com.project.view.action.CatalogAction"/>
        <param name="search">{1}>
<result>/searchResult.jspresult/>
    </action>   
</package>

Now make new Action named CatalogAction :
public class CatalogAction {
   private String search ;
  public void execute(){
     //this.getSearch() will return search param from URL
  }
  //inclde getter / setter method for search
}
That is it u r done ..no need to extend any Action classes from struts extends="struts-default" does the work for u.
I have been working on spring mvc a lot but struts-2 looks promising for sure....easing out developer's life:-)

1 comment:

Raghu said...

Good one. Like it