-1

我希望我的代码重新格式化 URL 并将它们显示如下(使用 / )

 http://www.example.com/myProject/Profile/view.action
 http://www.example.com/myProject/Profile/edit.action

但它显示它们如下(带 _ )

http://www.example.com/myProject/Profile_view.action
http://www.example.com/myProject/Profile_edit.action

为此,我将 struts.xml 文件中的“_”更改为“/”,但它不起作用

<action name="Profile/*" method="{1}" class="com.controller.Profile">
      <result name="view" tiles="viewProfile">viewProfile</result>
       <result name="edit" tiles="editProfile">editProfile</result>
  </action>

我使用以下代码调用它

 <a href="Profile/view.action" >Profile</a>

请让我知道是否有任何其他方法可以实现它。

4

3 回答 3

1

你为什么把你的行为称为

<a href="Profile/view.action" >Profile</a>

但是您可以使用

<a href="view.action" >Profile</a>

并在您的 xml 中进行了更改

<action name="view.action" method="{1}" class="com.controller.Profile">
      <result name="view" tiles="viewProfile">viewProfile</result>
       <result name="edit" tiles="editProfile">editProfile</result>
</action>

你不能在你的映射中使用通配符因为在你的操作中你没有指定任何东西。如果你真的想使用通配符,那么在你的操作中指定一个方法名称,就像这样

<a href="YOURMETHODNAMEview.action" >Profile</a>

在你的 xml

 <action name="*view.action" method="{1}" class="com.controller.Profile">
          <result name="view" tiles="viewProfile">viewProfile</result>
           <result name="edit" tiles="editProfile">editProfile</result>
    </action>
于 2013-02-14T05:56:01.070 回答
1

如果你真的想用斜线分隔你的动作,你应该使用NAMESPACE,试试这个:

<package name="profile" extends="struts-default" namespace="/Profile">
    <action name="*" method="{1}" class="com.controller.Profile">
      <result name="view" tiles="viewProfile">viewProfile</result>
      <result name="edit" tiles="editProfile">editProfile</result>
    </action>
</package>

如果您尝试在 url 上使用参数,则应考虑使用

<constant name="struts.enable.SlashesInActionNames" value="true"/>

在您的 Struts 2 配置文件中。

关于Wildcart Mapping文档,您也可以这样做:

    <action name="**" method="{1}" class="com.controller.Profile">
      <result name="view" tiles="viewProfile">viewProfile</result>
      <result name="edit" tiles="editProfile">editProfile</result>
    </action>

** matches zero or more characters including the slash ('/') character.您还可以在 Wildcart Mapping文档中找到它。

您应该首先考虑您真正想要什么,然后进行配置和实现。

在你的情况下,Struts 2 认为你的动作有斜线,关于动作配置

带有斜杠
的动作名称 如果您的动作名称中包含斜杠(例如, <action name="admin/home" class="tutorial.Admin"/>),您需要通过 struts.xml 文件中的常量指定 ,特别允许在动作名称中使用斜杠 <constant> name="struts.enable.SlashesInActionNames" value="true"/>
请参阅 JIRA 问题 WW-1383 进行讨论,因为将此属性设置为 true 会产生副作用。

带有点和破折号的动作名称 尽管动作命名非常灵活,但在使用点(例如)和/或破折号(例如)
时应该注意。虽然点表示法目前没有已知的副作用,但破折号表示法会导致为某些标签和主题生成的 JavaScript 出现问题。请谨慎使用,并始终尝试使用驼峰式操作名称(例如。 )或下划线(例如。)。create.usermy-actioncreateUsermy_action

所以 Struts 2 会将你的斜杠转换为下划线。

于 2013-02-14T08:50:33.523 回答
0

默认情况下,struts 不允许 / 在动作名称中。您可以通过使用命名空间或修改 struts 配置以允许破折号起作用来实现您想要的。

1.要使用命名空间,使用命名空间“/Profile”定义另一个包:

<package name="profile" extends="struts-default" namespace="/Profile">
<action name="*" method="{1}" class="com.controller.Profile">
  <result name="view" tiles="viewProfile">viewProfile</result>
  <result name="edit" tiles="editProfile">editProfile</result>
</action>
......
</package>

如果您想在同一个命名空间下对许多操作进行分组,则此方法非常有用。

2.要允许破折号起作用,您有 2 个选项,在 struts.xml 中定义常量,如下所示:

<constant name="struts.enable.SlashesInActionNames" value="true"/>
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>

或者在 struts.properties 中定义它:

struts.enable.SlashesInActionNames=true
struts.mapper.alwaysSelectFullNamespace=false

之后,您可以在 struts.xml 中使用它:

<action name="Profile/*" method="{1}" class="com.controller.Profile">
  <result name="view" tiles="viewProfile">viewProfile</result>
   <result name="edit" tiles="editProfile">editProfile</result>
</action>
于 2015-03-18T03:19:21.567 回答