如何在Struts 2中创建自定义URL?就像www.twitter.com/goodyzain [英] How to create custom URL in Struts 2? Like www.twitter.com/goodyzain
问题描述
我正在一个项目中,我想为每个用户提供唯一的URL.
I am working on a project where I want to provide unique URL for each user.
例如:
-
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
只需使用域名和用户名.
像Twitter一样:
Like Twitter does:
-
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具有属性 username
和返回 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
映射到您的操作.
这篇关于如何在Struts 2中创建自定义URL?就像www.twitter.com/goodyzain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!