2013-02-14 75 views
-1

我期待我的代碼重新設置URL,並將其顯示如下(有/)如何使用struts2重寫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> 

請讓我知道是否有其他方法來實現它。

+0

我有同樣的問題:( – 2013-02-14 05:44:15

+1

更改後_你/重新啓動了網絡應用程序嗎?無論如何,除非將struts.enable.SlashesInActionNames設置爲true,否則不應該在操作名稱中使用斜槓。你做到了? – Quaternion 2013-02-14 06:19:19

+0

我不明白它是一個正確的URL,地址的URL格式是不正確的,還是你需要一個格式化URL的規範? – 2013-02-14 11:32:57

回答

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中使用的參數,你應該考慮在你的Struts 2的配置文件中使用

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

問候到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 Configuration

動作名稱用斜槓
如果你的操作名稱中都有 斜槓(例如, <action name="admin/home" class="tutorial.Admin"/>)you 需要在struts.xml文件中通過指定 <constant> name="struts.enable.SlashesInActionNames" value="true"/>通過 常量在您的動作名稱中明確指定斜槓。
請參閱JIRA Issue WW-1383進行討論,因爲將 設置爲true會產生副作用。

用點動作名稱和破折號
雖然動作的命名是相當 靈活,應該用點時要注意(如:create.user) 和/或破折號(如my-action)。雖然點符號此時不存在已知的 副作用,但短劃線符號會導致爲某些標記和主題生成的JavaScript生成的問題 。謹慎使用 ,並且始終嘗試使用camelcase動作名稱(例如 createUser)或下劃線(例如my_action)。

因此,Struts 2會將您的斜槓轉換爲下劃線。

+0

感謝它的工作原理,但有一個小問題如下http://stackoverflow.com/questions/14886855/how-to-change-my-code-to-implement-網址reqriting-正確 – 2013-02-15 01:05:28

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> 

你是不是允許你在這裏使用通配符apping爲方法=「{1}」,因爲在你的行動,你還沒有指定anything.And如果你真的想使用通配符然後在動作這樣

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

我想它是用來刪除不重新格式化URL的擴展。 – 2013-02-14 06:02:59

+0

ohh對不起,我編輯了我的答案,刪除您的downvote – 2013-02-14 06:08:16

0

默認情況下,struts不允許/在操作名稱中。你可以通過使用命名空間來實現你想要的,或者修改struts配置來允許破解行動。

1.To使用的命名空間,定義另一個包與命名空間「/檔案」:如果你想在同一命名空間下組的許多行動

<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.To讓破折號在行動上,你有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>