6

I am working on a project where I want to provide unique URL for each user.

For example:

  • www.SocialNetwork.com/jhon
  • www.SocialNetwork.com/jasmine

So far I'm able to achieve this:

  • www.SocialNetwork.com/profiles/jasmine

here profiles is my action where I can get the user name by

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


<action name="profiles/{username}" class="com.example.actions.ViewProfileAction">
  <result name="input">/WEB-INF/jsp/profile.jsp</result>
</action> 

but I want to achieve something like this:

  • www.SocialNetwork.com/jasmine

Just use domain name and username.

Like Twitter does:

  • www.twitter.com/username

How to achieve this?

Roman C
  • 49,761
  • 33
  • 66
  • 176
goodyzain
  • 683
  • 2
  • 8
  • 21
  • So why do you put `profiles/` in front of the action name if you don't want it to be there? – Aleksandr M May 21 '14 at 10:23
  • 1
    i dont need profiles to be in URL...for users it should be simple...like Domainname/username...simple one.. – goodyzain May 21 '14 at 10:24
  • So remove it from action name. – Aleksandr M May 21 '14 at 10:26
  • U mean to tell... http://www.SocialNetwork/jasmine but the problem is jasmine is a username...i dont have any action class so that i can process the name and redirect to his profile page... – goodyzain May 21 '14 at 10:30
  • @goodyzian How do you distinguish between user names and and other actions? – Roman C May 21 '14 at 10:56
  • Yeah...that was other case...i will assign them a unique Id...like the username not going to be just Jhon...i will give something like this...Jhon+unique id...like http://www.SocialNetwork/jasmine$8383992Juuf, Roman C sir i am just finding a way where i can provide Unique urls to every user...:)in simple fashion like domainname/username..is this achievable.?? – goodyzain May 21 '14 at 11:00
  • Why not if you have only one action in the root namespace that gets the username as a parameter in url? – Roman C May 21 '14 at 11:10
  • 2
    like this... Or can u tell me How to do this...?? – goodyzain May 21 '14 at 11:13
  • I made a complete sample for the above. If any one wants please refer [here](http://tech.learnerandtutor.com/custom-url-in-struts2-for-each-user/) – Rajeshkumar May 23 '14 at 09:39
  • @Rajesh its mine...:p :p :p – goodyzain May 23 '14 at 10:07
  • @goodyzain I accept. But I made that for the new users to understand completely and easily all discussions made here. – Rajeshkumar May 23 '14 at 10:09

2 Answers2

3

If you want to use named patterns in wildcard mapping then you should configure following in the struts.xml:

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

now assume com.example.actions.ViewProfileAction bean has a property username, and method execute that returns a SUCCESS result. Then you can map the action in the root namespace "/" configured to your package.

<action name="{username}" class="com.example.actions.ViewProfileAction">
  <result>/WEB-INF/jsp/profile.jsp</result>
</action>

you can get the name in the JSP using OGNL

<s:property value="username"/>

Also note that you should deploy to the root context to have

your.domain.com/username mapped to your action.

Roman C
  • 49,761
  • 33
  • 66
  • 176
1

Try this out. It may work. Use Freemarker USE $.

<action name="profiles/${username}" class="com.example.actions.ViewProfileAction">
    <result name="input">/WEB-INF/jsp/profile.jsp</result>
</action> 

It may work

Paresh3489227
  • 845
  • 11
  • 22
  • it will work fine...i want the URL to look like this... SocialNetwork/jasmine Not Like SocialNetwork/profiles/jasmine i dont want to call profiles action...in url – goodyzain May 21 '14 at 11:16
  • So don't use profiles. use only action name="${username}". it will work for you. – Paresh3489227 May 21 '14 at 11:18
  • SocialNetwork is Domain Name,Jasmine is Username...Profiles Is actions name...I dont want user to enter action name which is profiles..i just want them to enter them is Domainname/username NOTE:no action name is called in URL – goodyzain May 21 '14 at 11:24
  • i voted your question up. its a nice question. i will work on it. – Paresh3489227 May 21 '14 at 12:13
  • How to call this action from the login form? I tried like But it is calling just .action like localhost:8080/CustomURL/.action – Rajeshkumar May 22 '14 at 10:00
  • @goodyzain that i was telling you yesterday. that remove profiles and use ognl like Roman C sir said. you can see my comments also. and set the action file means java file in class attribute of action tag. jsp remains same as it is. – Paresh3489227 May 22 '14 at 10:43
  • @Rajesh here we are using in the Above question and struts.xml . i think by this you get your answer. – Paresh3489227 May 22 '14 at 10:47
  • @Paresh3489227 Thanks. As you mentioned I added but it is not showing the correct url it shows name username in URL. I posted a separate question for that [here](http://stackoverflow.com/questions/23819733/how-to-call-custome-url-action-from-form-action). Please suggest. – Rajeshkumar May 23 '14 at 01:52
  • @Paresh3489227 Yeh..Paresh You told me that in comments...But i was in confused state..before thinking of Ticking Your Answer RomonC sir told He will Work on this and Posted the Full answer...so i voted your Answer :( – goodyzain May 23 '14 at 05:09
  • its okay. i am happy that you got your answer. – Paresh3489227 May 23 '14 at 06:01