How to create custom URL in Struts 2? Like www.twitter.com/goodyzain
Asked Answered
C

2

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?

Cortico answered 21/5, 2014 at 10:16 Comment(11)
So why do you put profiles/ in front of the action name if you don't want it to be there?Tiffa
i dont need profiles to be in URL...for users it should be simple...like Domainname/username...simple one..Cortico
So remove it from action name.Tiffa
U mean to tell... 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...Cortico
@goodyzian How do you distinguish between user names and and other actions?Galer
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 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.??Cortico
Why not if you have only one action in the root namespace that gets the username as a parameter in url?Galer
like this...<package name="default" extends="struts-default,json-default,jasperreports-default" namespace="profiles"> Or can u tell me How to do this...??Cortico
I made a complete sample for the above. If any one wants please refer hereAndaman
@Rajesh its mine...:p :p :pCortico
@Cortico I accept. But I made that for the new users to understand completely and easily all discussions made here.Andaman
G
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.

Galer answered 21/5, 2014 at 16:3 Comment(2)
I'm getting null action mapping when I follow the same. am I doing anything wrong ?Vagina
Sorry its working for me, I missed entry in the web.xml :(Vagina
F
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

Futhark answered 21/5, 2014 at 11:11 Comment(10)
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 urlCortico
So don't use profiles. use only action name="${username}". it will work for you.Futhark
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 URLCortico
i voted your question up. its a nice question. i will work on it.Futhark
How to call this action from the login form? I tried like <s:form> <s:textfield name="username"/> <s:submit/> But it is calling just .action like localhost:8080/CustomURL/.action </s:form>Andaman
@Cortico 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.Futhark
@Rajesh here we are using in the Above question <s:form action="{username}"> and struts.xml <action name="{username}">. i think by this you get your answer.Futhark
@Futhark 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. Please suggest.Andaman
@Futhark 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 :(Cortico
its okay. i am happy that you got your answer.Futhark

© 2022 - 2024 — McMap. All rights reserved.