2016-09-08 37 views
0

私はウェブサイトのインデックスを持っています..... http://localhost/myContext/
RESTインデックス............... ...
、多くのRESTページhttp://localhost/myContext/restController/ ... http://localhost/myContext/restController/*/*相対URIの<spring:url />で絶対パスを解決する方法

RESTコントローラは、URLに応じて動的にコンテンツをロードし、相対パスを使用しています。

これらのページに直接ブラウザでアクセスすると、すべてのページが正常に動作し、自由にナビゲートできます。


は今、私はAJAXを使用して/myContext/インデックスに直接restController/*/*ページを統合したいです。内容はDIVにロードされます。 ブラウザのURLは決して変更されません

私のブラウザは、Webサイトのインデックスを打つ/myContext/
次にJSPを指し示すAJAX負荷/restController/、... JSPインサイド


:に基づいて直接<spring:url value="something">戻っ全体ではなく、絶対パスのsomethingコントローラのservletPath /myContext/restController/something

<spring:url>は相対パスを返すため、ブラウザは/myContext/something-404をヒットしようとします

これは以前に<form:form>を使用したフォームでこれを行いました。アクションは、コントローラのservletPathに対して相対的に作成されます。

<spring:url>(またはその他)の絶対パスを取得する最もクリーンな方法は、相対URIからですか?


私は${pageContext.request.requestURI}を使用してみましたが、それは私がナビゲートする最後のダッシュの存在かどうかに依存しているため、ファイル名は動作しませんフィルタリング、JSPファイル名(/myContext/restController/something/ctrl.jsp)が含まれています。

また、<form:form />を含めて試してみました.Javascriptを使用してアクションを回復しました。それは非常に醜いですが、それは動作します...良い解決策tho。


<spring:url>からコード:

private String createUrl() throws JspException { 
     HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); 
     HttpServletResponse response = (HttpServletResponse) pageContext.getResponse(); 
     StringBuilder url = new StringBuilder(); 
     if (this.type == UrlType.CONTEXT_RELATIVE) { 
      // add application context to url 
      if (this.context == null) { 
       url.append(request.getContextPath()); 
      } 
      else { 
       if (this.context.endsWith("/")) { 
        url.append(this.context.substring(0, this.context.length() - 1)); 
       } 
       else { 
        url.append(this.context); 
       } 
      } 
     } 
     if (this.type != UrlType.RELATIVE && this.type != UrlType.ABSOLUTE && !this.value.startsWith("/")) { 
      url.append("/"); 
     } 
     url.append(replaceUriTemplateParams(this.value, this.params, this.templateParams)); 
     url.append(createQueryString(this.params, this.templateParams, (url.indexOf("?") == -1))); 

     String urlStr = url.toString(); 
     if (this.type != UrlType.ABSOLUTE) { 
      // Add the session identifier if needed 
      // (Do not embed the session identifier in a remote link!) 
      urlStr = response.encodeURL(urlStr); 
     } 

     // HTML and/or JavaScript escape, if demanded. 
     urlStr = htmlEscape(urlStr); 
     urlStr = this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(urlStr) : urlStr; 

     return urlStr; 
    } 
+0

あなたがspring urlを使用することを強制されていない場合は、elを使用して ベースルールは絶対値になります –

答えて

0

<spring:url>が埋め込まれたJSPファイルに正しく相対パスを処理していないようです。
この変換を行うために私自身のタグを作成しなければなりませんでした。

TAGコード:

<%@tag trimDirectiveWhitespaces="true" pageEncoding="UTF-8"%> 
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 

<%@attribute name="value"%> 
<%@attribute name="var"%> 
<%@attribute name="id"%> 

<% 
    // Fetch the original request_uri from the request and extract its absolute path 
    String url_absolute; 
    String requestUri = (String) request.getAttribute("javax.servlet.forward.request_uri"); 
    if (requestUri == null) 
     requestUri = (String) request.getRequestURI(); 

    int lastSlash = requestUri.lastIndexOf('/'); 
    if (lastSlash >= 0) 
     url_absolute = requestUri.substring(0, lastSlash + 1); 
    else 
     url_absolute = "/"; // Should never happen 

    // If a value is defined in the TAG, we add it to the path 
    if (value != null) 
     url_absolute += value; 

    getJspContext().setAttribute("url_absolute", url_absolute); 

    // If a var is defined in the TAG, we set its attribute in the request 
    if (var != null) 
     request.setAttribute(var, url_absolute); 
%> 

<c:if test="${not empty id}"> 
    <input type="hidden" id="${id}" value="${url_absolute}"> 
</c:if> 

<c:if test="${empty var and empty id}"> 
    ${url_absolute} 
</c:if> 

このコードは、スコープを処理するために改良されコンテキストおよび属性をエスケープすることができます。

関連する問題