JSP 如何在Struts 2中创建自定义URL?如www.twitter.com/goodyzain

93ze6v8z  于 2023-05-11  发布在  Go
关注(0)|答案(2)|浏览(219)

我在一个项目,我想为每个用户提供独特的URL工作。
例如:

  • www.SocialNetwork.com/jhon
  • www.SocialNetwork.com/jasmine

到目前为止,我可以做到这一点:

  • www.SocialNetwork.com/profiles/jasmine

这里profiles是我的操作,我可以通过以下方式获取用户名

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

但我想实现这样的东西:

  • www.SocialNetwork.com/jasmine

只需使用 * 域名 * 和 * 用户名 *。
就像Twitter一样:

  • www.twitter.com/username

如何做到这一点?

e0uiprwp

e0uiprwp1#

如果你想在通配符Map中使用命名模式,那么你应该在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结果的方法execute。然后,您可以将根命名空间"/"中配置的操作Map到您的包。

<action name="{username}" class="com.example.actions.ViewProfileAction">
  <result>/WEB-INF/jsp/profile.jsp</result>
</action>

您可以使用OGNL在JSP中获取名称

<s:property value="username"/>

还请注意,您应该部署到根上下文,以使
your.domain.com/usernameMap到您的操作。

s4n0splo

s4n0splo2#

试试这个可能会有用。使用FreemarkerUSE $

<action name="profiles/${username}" class="com.example.actions.ViewProfileAction">
    <result name="input">/WEB-INF/jsp/profile.jsp</result>
</action>

可能会有用

相关问题