2014-09-04 56 views
2

嗨,我是初學者到struts2。如果我像後 https://stackoverflow.com/questions/25658104的URL我想獲得價值25658104在Struts2中清潔url-s

它必須不帶任何參數來完成。還不如https://stackoverflow.com/questions?qid=25658104

有什麼辦法基於之間傳遞的價值來獲得內容/ myvalue的/

如果我發佈https://stackoverflow.com/questions/myvalue1/ - >重定向到一個頁面,對應myvalue1(從DB)的負載值

如果我發佈https://stackoverflow.com/questions/myvalue2/ - >重定向到一個頁面,對應於(從DB)

我發現它在myvalue2負荷值,因此基於之間傳遞的價值觀網站導航所以導航。如何我可以使用它在S truts2

我的問題是類似URL rewriting with PHP ,但我想這樣做在Struts2

編輯

struts.xml的

<struts> 
<constant name="struts.enable.DynamicMethodInvocation" value="false" /> 
<constant name="struts.devMode" value="true" /> 
<constant name="struts.custom.i18n.resources" value="ApplicationResources" /> 
<constant name="struts.multipart.maxSize" value="104857600" /> 


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

<bean type="com.opensymphony.xwork2.util.PatternMatcher" name="username" class="com.opensymphony.xwork2.util.NamedVariablePatternMatcher"/> 

<package name="default" extends="struts-default" namespace="/"> 

    <result-types> 
     <result-type name="json" default="false" class="org.test.struts.controller.JSONResult" /> 
    </result-types> 

    <action name="login" class="org.test.struts.controller.LoginAction" method="execute"> 
     <result name="success">Welcome.jsp</result> 
     <result name="error">Login.jsp</result> 
    </action> 

    <action name="profiles/{username}" class="org.test.struts.controller.ViewProfileAction"> 
     <result name="input">profile.jsp</result> 
    </action> 




    <action name="getJson" class="org.test.struts.controller.JsonTestAction"> 
     <result type="stream"> 
     <param name="contentType">text/html</param> 
     <param name="inputName">inputStream</param> 
    </result> 
    </action> 

    <action name="getJsonObj" class="org.test.struts.controller.JsonTest"> 
     <result type="stream"> 
     <param name="contentType">text/html</param> 
     <param name="inputName">inputStream</param> 
    </result> 
    </action> 

    <action name="getJsonObject" class="org.test.struts.controller.JsonObjectRet"> 
     <result name="success" type="json"> 
       <param name="target">startswith</param> 
       <param name="targetObjectClass">java.lang.String</param> 
     </result> 
    </action> 


    <action name="upload"> 
     <result>/UploadFile.jsp</result> 
    </action> 
    <action name="UploadFile" class="org.test.struts.fileupload.controller.UploadFileAction"> 
     <param name="filesPath">myfiles</param> 
     <result name="success">/UploadFileSuccess.jsp</result> 
     <result name="input">/UploadFile.jsp</result> 

     <interceptor-ref name="defaultStack"> 
      <param name="fileUpload.maximumSize">10485760</param> 
      <param name="fileUpload.allowedTypes">text/plain,image/jpeg</param> 
     </interceptor-ref> 

    </action> 


</package> 
</struts> 

Action類

import com.opensymphony.xwork2.ActionSupport; 

public class ViewProfileAction extends ActionSupport{ 
    private String username; 

    public String getUsername() { 
    return username; 
} 


public String execute() { 
    // look up the appropriate user by username and 
    // expose the user to the JSP with a getUser() method. 
     System.out.println(username); 
     return INPUT; 
    } 


    public void setUsername(String username) { 
    this.username = username; 
    } 
} 
+0

你真的想** **重寫URL-S還是你只是想要搜索引擎優化友好的網址? – 2014-09-04 07:22:01

+1

如果你只需要乾淨的網址,看看:http://stackoverflow.com/q/19495332/1654265和http://stackoverflow.com/a/22068583/1654265 – 2014-09-04 08:16:13

+0

謝謝@AndreaLigios你的第二個鏈接恰好給我看我引用了http://www.struts2.info/blog/better-urls-with-struts2,但它沒有設置變量的值。action class中的值爲** null ** – theRoot 2014-09-04 09:22:23

回答

1

更改此

<constant name="struts.patternMatcher" value="username"/> 

<bean type="com.opensymphony.xwork2.util.PatternMatcher" name="username" class="com.opensymphony.xwork2.util.NamedVariablePatternMatcher"/> 

於此,在使用正則表達式patternMatcher:

<constant name="struts.patternMatcher" value="regex"/> 

當然,並添加一個結果爲比input另一個你的行動!像

<action name="profiles/{username}" class="org.test.struts.controller.ViewProfileAction"> 
    <result name="success">profile.jsp</result> 
    <result name="input">profile.jsp</result> 
</action> 

而且從行動而不是INPUT返回SUCCESS

INPUT是當輸入錯誤occour通過工作流攔截器返回的自動化結果,而動作不甚至達到了(讀detailed answer它)

+0

它顯示我錯誤'無法加載bean com.opensymphony.xwork2.util.PatternMatcher(正則表達式) - [未知位置]'......... Becaus of this我包括PatternMatcher bean – theRoot 2014-09-04 09:57:55

+0

你需要Struts版本高於2.1.9 – 2014-09-04 09:58:52

+1

如果你使用的是較低版本,我強烈建議你升級立即到2.3.16或更高,由於危險的安全問題折磨舊版本 – 2014-09-04 09:59:25