4

Spring 3.1.2 の使用

コントローラに関連する URL を作成できるように、jsp からコントローラ (メソッドではない) RequestMapping の注釈付きの値をどのように参照しますか。メソッド レベルのリクエスト マッピングが参照されると、リンクが機能しなくなるため、リンクをこれに含めることはできません。

例 (このコントローラーのマッピングは「/foo/test」であることに注意してください):

@Controller
@RequestMapping("/foo/test")
public class FooController {

    @RequestMapping(value="/this", method=RequestMethod.GET)
    public String getThis() {
        return "test/this";
    }

    @RequestMapping(value="/that", method=RequestMethod.GET)
    public String getThat() {
        return "test/that";
    }

    @RequestMapping(value="/other", method=RequestMethod.GET)
    public String getOther() {
        return "test/other";
    }
}

...そして私のthis.jspから:

<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%>
<s:url value="${controllerRequestMapping}" var="baseUrl"/>

<a href="${baseUrl}/that">That</a>
<a href="${baseUrl}/other">Other</a>

RequestMapping にアクセスする必要があるのは、ビューが複数のコントローラーからアクセスされる可能性があるためです。このコントローラのマッピングは「/bar/test」であることに注意してください!

@Controller
@RequestMapping("/bar/test")
public class BarController {

    @RequestMapping(value="/this", method=RequestMethod.GET)
    public String getThis() {
        return "test/this";
    }

    @RequestMapping(value="/that", method=RequestMethod.GET)
    public String getThat() {
        return "test/that";
    }

    @RequestMapping(value="/other", method=RequestMethod.GET)
    public String getOther() {
        return "test/other";
    }
}

コントローラー レベルのリクエスト マッピングの一部にはパス変数も含まれているため、リクエスト マッピングの文字列値だけを取得することはできません。次のように解決する必要があります。

@Controller
@RequestMapping("/something/{anything}/test/")
public class SomethingController {
...
}

アップデート

おそらく、Spring URL タグの前に Controller リクエスト マッピングをコンテキスト パスに追加してコンテキスト パスを変更する方法があれば、問題は解決するでしょう。

contextPath = contextPath/controllerRequestMapping

次に、Spring が現在のコンテキスト パスを自動的に取得すると信じているため、次のようなことができます。

<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%>

<a href="<s:url value="/that"/>">That</a>
<a href="<s:url value="/other"/>">Other</a>

もちろん、これが最適です。アイデア?想い…?

前もって感謝します!

4

5 に答える 5

3

サーブレット API を使用して URI を取得できます。私の知る限り、これを行う「春」の方法はありません。

@RequestMapping(value="/this", method=RequestMethod.GET)
public String getThis(HttpServletRequest request, Model m) {
    m.addAttribute("path", request.getRequestURI());
    return "test/this";
}

次に、JSP で次のようにします。

<a href="${path}">This</a>

HttpServletRequest の詳細については、こちらの API を参照してください

于 2012-09-29T04:03:56.187 に答える
0

4.1 以降、すべての @RequestMapping には、クラスの大文字と完全なメソッド名に基づいたデフォルト名が割り当てられます。たとえば、クラス FooController のメソッド getFoo には「FC#getFoo」という名前が割り当てられます。この命名戦略はenter code here 、HandlerMethodMappingNamingStrategy を実装し、RequestMappingHandlerMapping で構成することでプラグ可能です。さらに @RequestMapping アノテーションには、デフォルトの戦略をオーバーライドするために使用できる name 属性が含まれています。Spring JSP タグ ライブラリは、このメカニズムに基づいてコントローラー メソッドへのリンクを準備するために使用できる mvcUrl という関数を提供します。

たとえば、次のようになります。

@RequestMapping("/people/{id}/addresses")
public class MyController {
@RequestMapping("/{country}")
public HttpEntity getAddress(@PathVariable String country) { ... }
}

次の JSP コードでリンクを準備できます。

<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
...
<a href="${s:mvcUrl('MC#getPerson').arg(0,'US').buildAndExpand('123')}">Get Address</a>

両方の方法ではなく、Java Config または XML のいずれかの方法を使用して MVC を構成したことを確認してください。複数の RequestMappingInfo HandlerMapping が見つかったため、例外が発生します。

于 2015-05-20T05:57:08.603 に答える
0

Spring 3.1 以降を使用している場合は、以下のコードを参照してリクエスト マッピングにアクセスしてください。これがあなたが必要とするものかどうかわかりません。これは単なる試みであり、もちろん、簡単な方法はわかりません。リクエスト マッピングを持つメソッド内から getRequestMappings() を呼び出します。

public class FooController{

    private RequestMappingHandlerMapping handlerMapping = null;

    @Autowired
    public FooController(RequestMappingHandlerMapping handlerMapping){
        this.handlerMapping = handlerMapping;
    }

    public Set<String> getRequestMappings(){
        Map<RequestMappingInfo, HandlerMethod> handlerMap = handlerMapping.getHandlerMethods();
        Iterator<RequestMappingInfo> itr = handlerMap.keySet().iterator();
        while(itr.hasNext()){
            RequestMappingInfo info = itr.next();
            PatternsRequestCondition condition = info.getPatternsCondition();
            Set<String> paths = condition.getPatterns();
            HandlerMethod method = handlerMap.get(info);
            if(method.getMethod().getName().equals(Thread.currentThread().getStackTrace()[2].getMethodName()))
            return paths;

            }
        return new HashSet<String>();
    }



}
于 2012-09-30T13:50:30.730 に答える
0

にアクセスする組み込みの方法はありませんが@RequestMapping、モデルに URL を追加するだけで、その方法でビューから参照できます。

于 2012-09-28T21:30:00.877 に答える
0

私によると、 @RequestMapping アノテーションをクラスレベルとメソッドレベルの両方に配置しても違いはありません。その代わりに、組み合わせた @RequestMapping アノテーションをメソッドの上にのみ持つことができます。

したがって、メソッドレベルのアノテーションは次のようになります。

@RequestMapping(value="("/foo/test/this", method=RequestMethod.GET)

また

@RequestMapping(value="("/bar/test/this", method=RequestMethod.GET)

両方のメソッドが同じビューを返すため、両方に対して 1 つのメソッドのみを使用できます。また、注釈マッピング値では、foo と bar の代わりに 1 つのパス変数{from}を導入できます。最後に、アノテーションとメソッドは次のようになります。

@RequestMapping(value="("/{from}/test/this", method=RequestMethod.GET)
public String getThis(@PathVariable("from") String from) {       
    return "test/this";       
}

メソッドでこれを実行した後、取得したパス変数のランタイム値に基づいて、さまざまな計算または操作を実行できます。JSP ページでは、この値を で簡単に取得できます${from}

これがお役に立てば幸いです。乾杯。

于 2012-09-29T05:50:05.070 に答える