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

前端 未结 2 918
感情败类
感情败类 2020-11-30 08:13

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/jas         


        
相关标签:
2条回答
  • 2020-11-30 08:53

    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

    0 讨论(0)
  • 2020-11-30 08:56

    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.

    0 讨论(0)
提交回复
热议问题