2011-10-20 12 views
9

私は次のマッピングがあります。オプションのパス変数

@RequestMapping(value = "/{first}/**/{last}", method = RequestMethod.GET) 
public String test(@PathVariable("first") String first, @PathVariable("last") 
    String last) {} 

次のURIのために:

foo/a/b/c/d/e/f/g/h/bar 
foo/a/bar 
foo/bar 

マップのfooへの最初のバー最後にになり、正常に動作します。上

@RequestMapping(value = "/{first}/{middle:[some regex here?]}/{last}", 
    method = RequestMethod.GET) 
public String test(@PathVariable("first") String first, @PathVariable("middle") 
    String middle, @PathVariable("last") String last) {} 

プリティ・スタック:(最後のURIの例のように)何の真ん中がない場合

私が希望することは、単一パスのparamにfooとbarの間のすべてをマッピングし、何か、またはnullであります私が{middle:。*}のような単純なものであることを期待していたので、は/ foo/a/barに、または{middle:(。* /)*}にはマップされません。

は、正規表現パターンを適用する「/」前時AntPathStringMatcherのトークン化していますか? (交差する/不可能なパターンを作る)、あるいは解決策がありますか?

FYIこれは春3.1M2

これであるが@RequestMapping controllers and dynamic URLsに似ているようですが、私はそこに解決策を見ていません。

答えて

1

私が知る限り行うことはできません。指定したとおり、正規表現は各スラッシュでパスを分割した後でパス要素に適用されるため、正規表現は '/'と決して一致しません。

は手動でURLを検査し、要求オブジェクトからそれを自分で解析することができます。

1

これは、カスタムパス照合を書いて、それを使用するために春を構成することによって行うことができます。例えば、このような解決策はここに文書化されている:http://java.dzone.com/articles/spring-3-webmvc-optional-path

リンクは、カスタムパス照合を提供し、それを使用するように、スプリングを設定する方法を示しています。カスタムコンポーネントの作成に気をつけなければ、それはあなたのニーズを解決するはずです。

また、これは私がspringframeworkで、内側の変数を使用して、私のプロジェクトでWith Spring 3.0, can I make an optional path variable?

13

の複製である:

@RequestMapping(value = { "/trip/", // /trip/ 
     "/trip/{tab:doa|poa}/",// /trip/doa/,/trip/poa/ 
     "/trip/page{page:\\d+}/",// /trip/page1/ 
     "/trip/{tab:doa|poa}/page{page:\\d+}/",// /trip/doa/page1/,/trip/poa/page1/ 
     "/trip/{tab:trip|doa|poa}-place-{location}/",// /trip/trip-place-beijing/,/trip/doa-place-shanghai/,/trip/poa-place-newyork/, 
     "/trip/{tab:trip|doa|poa}-place-{location}/page{page:\\d+}/"// /trip/trip-place-beijing/page1/ 
}, method = RequestMethod.GET) 
public String tripPark(Model model, HttpServletRequest request) throws Exception { 
    int page = 1; 
    String location = ""; 
    String tab = "trip"; 
    // 
    Map pathVariables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); 
    if (pathVariables != null) { 
     if (pathVariables.containsKey("page")) { 
      page = NumberUtils.toInt("" + pathVariables.get("page"), page); 
     } 
     if (pathVariables.containsKey("tab")) { 
      tab = "" + pathVariables.get("tab"); 
     } 
     if (pathVariables.containsKey("location")) { 
      location = "" + pathVariables.get("location"); 
     } 
    } 
    page = Math.max(1, Math.min(50, page)); 
    final int pagesize = "poa".equals(tab) ? 40 : 30; 
    return _processTripPark(location, tab, pagesize, page, model, request); 
} 

HandlerMapping.html#URI_TEMPLATE_VARIABLES_ATTRIBUTE

-1

この

@RequestMapping(value = {"some mapped address","some mapped address with path variable","some mapped address with another path variable"}) 
を使用するようにしてくださいを参照してください。

配列の利用可能なURLのリスト方法

しかし、あなたはそれがnullに傾けるあなたのメソッドのシグネチャで@PathVariableを使用している際に、URLのリストを作成する上で注意してください。

希望このヘルプ