How to create custom URLs with Struts2?Like www.twitter.com/goodyzain

跟風遠走 提交于 2019-11-26 11:15:30

问题


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

http://www.SocialNetwork.com/jhon , http://www.SocialNetwork.com/jasmine,

So far I\'m able to achieve this: http://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, http://www.SocialNetwork.com/jasmine Just Domain name then username.

Like twitter does:

www.twitter.com/username

How to achieve this?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/23780282/how-to-create-custom-urls-with-struts2like-www-twitter-com-goodyzain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!