nasty pasty
nasty pasty

Reputation: 7158

'adding' a url to an existing URL to make them more readable with Struts2/Java EE

I want to change my ugly url which looks like:

/search.action?category=1

to a nice readable URL which looks like

/my-first-category

I'm using Struts 2, Java EE on Tomcat.

I've tried URL configs in struts like this:

    <action name="my-first-category">
        <result type="redirect">search.action?category=1</result>
    </action>

Which does a redirect, and once the user visits the page they see the ugly URL in the address bar, not the nice readable one.

The same thing happens with this mapping:

    <action name="12345">
       <result type="redirectAction">
            <param name="actionName">search</param>
            <param name="category">1</param>
        </result>
    </action>

Do you know how to achieve what I need to be able to do?

Upvotes: 1

Views: 331

Answers (4)

Umesh Awasthi
Umesh Awasthi

Reputation: 23587

One thing you can use is NamedVariablePatternMatcher in struts2. a discussion about its features can be found here

NamedVariablePatternMatcher in Struts2

else you can use URL-Rewriting for your URLS by which they can be converted as a user friendly and readable format. tuckey URL is one of the most widely used URL filter for the same

URl-Rewriting

Upvotes: 2

Steven Benitez
Steven Benitez

Reputation: 11055

You're better off creating clean, restful URLs from the start, rather than trying to mask the "ugly" ones later. To expand on the NamedVariablePatternMatcher approach that umesh mentioned, here's an example that shows how to set that up:

struts.xml

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

  <package ...>
    <action name="categories/{category}" ...>
      ...
    </action>
  </package>
</struts>

Example Action

public class SearchAction extends ActionSupport {
  private Integer category;

  public void setCategory(Integer category) {
    this.category = category;
  }
}

This would yield URLs like:

/categories/1
/categories/2
/categories/3

Upvotes: 2

Russell Shingleton
Russell Shingleton

Reputation: 3196

I would suggest checking out the Struts 2 Rest Plugin architecture.

Apache Struts 2 Documentation REST Plugin

I've begun moving a lot of my new projects to REST style as it supports a more friendly URL project model.

You might also be interested in the following post:

Struts 2: parameters between Actions

This demonstrates how to pass parameters in your action via the struts.xml. You can set parameters from your action class or set static parameters by hard coding them in the xml file.

Upvotes: 0

Ahmed Nematallah
Ahmed Nematallah

Reputation: 112

try to use frames they are very common and used in *.tk and in *.co.cc domains

wish this helps

Upvotes: -2

Related Questions