如何在 Struts 2 中创建自定义 URL?喜欢 www.twitter.com/goodyzain [英] How to create custom URL in Struts 2? Like www.twitter.com/goodyzain

查看:14
本文介绍了如何在 Struts 2 中创建自定义 URL?喜欢 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

这里profiles是我的操作,我可以通过

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

只需使用域名用户名.

就像推特一样:

  • 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,和方法 execute 返回一个 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆