如何使用Struts2创建自定义URL?如www.twitter.com/goodyzain [英] How to create custom URLs with Struts2?Like www.twitter.com/goodyzain
问题描述
我正在开展一个项目,我希望为每个用户提供唯一的URL。例如,
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,
到目前为止,我能够做到这一点: http:// www.SocialNetwork.com/profiles/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>
但我希望实现这样的目标, http:// www。 SocialNetwork.com/jasmine
只需域名然后用户名。
but I want to achieve something Like this, http://www.SocialNetwork.com/jasmine
Just Domain name then username.
像twitter一样:
Like twitter does:
www.twitter.com/username
www.twitter.com/username
如何实现这一目标?
推荐答案
如果要在通配符映射中使用命名模式,则应在 struts中配置以下内容。 xml
:
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"/>
现在假设 com.example.actions.ViewProfileAction
bean有一个属性用户名
,方法执行
返回 SUCCESS
结果。然后,您可以映射配置到您的包的根命名空间/
中的操作。
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>
您可以使用OGNL获取JSP中的名称
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
映射到您的操作。
这篇关于如何使用Struts2创建自定义URL?如www.twitter.com/goodyzain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!