Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

java - How to create custom URL in Struts 2? Like www.twitter.com/goodyzain

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...