11

私は信じられないほど単純なコントローラー/ビューのセットアップを試みてきましたが、それを機能させることができません。私のでは、正常に実行されているという名前web.xmlを定義しました。で、私は設定しました:<servlet>servlet-context.xmlservlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"

<...other stuff in here... />

<mvc:annotation-driven />

とりわけ。私の理解では、@注釈を使用するために必要なのはこれだけです。

私のコントローラーには、次のものがあります。

@RequestMapping(value="/student/{username}/", method=RequestMethod.GET)
public String adminStudent(@PathVariable String username, @RequestParam String studentid) {
    return "student";
}

そして私のstudent.jsp見解では、私は持っています:

<p>This is the page where you would edit the stuff for ${username}.</p>
<p>The URL parameter <code>studentid</code> is set to ${studentid}.</p>

にリクエストするとhttp://localhost:8080/application/student/xyz123/?studentid=456、期待するビューが表示されますが、すべての変数が空白またはnullです。

<p>This is the page where you would edit the stuff for .</p>
<p>The URL parameter <code>studentid</code> is set to .</p>

自分web.xmlservlet-context.xml設定の仕方に問題があるのではないかと思いますが、どこにも犯人が見つかりません。私が見る限り、どのログにも何も表示されません。


更新:私は自分のコードをspring-mvc-showcaseのこの部分に基づいていました:

@RequestMapping(value="pathVariables/{foo}/{fruit}", method=RequestMethod.GET)
public String pathVars(@PathVariable String foo, @PathVariable String fruit) {
    // No need to add @PathVariables "foo" and "fruit" to the model
    // They will be merged in the model before rendering
    return "views/html";
}

...これは私にとってはうまくいきます。この例が機能する理由は理解できませんが、私の場合は機能しません。彼らが何か違うことをしているからですservlet-context.xmlか?

<annotation-driven conversion-service="conversionService">
    <argument-resolvers>
        <beans:bean class="org.springframework.samples.mvc.data.custom.CustomArgumentResolver"/>
    </argument-resolvers>
</annotation-driven>
4

4 に答える 4

17

モデル マップを作成し、それらのパラメーターの名前と値のペアをそれに追加します。

@RequestMapping(value="/student/{username}/", method=RequestMethod.GET)
public String adminStudent(@PathVariable String username, @RequestParam String studentid, Model model) {
    model.put("username", username);
    model.put("studentid", studentid);

    return "student";
}
于 2011-11-27T16:17:40.193 に答える
7

あはは!最後に、それを理解しました。

SPR-7543によると、 は Spring 3.1 を使用しており、モデルに s を自動的spring-mvc-showcaseに公開します。@PathVariable

@duffymo と @JB Nizet が指摘したように、モデルへの追加model.put()は、3.1 より前のバージョンの Spring で行うことです。

Ted Young は、Spring: E​​xpose @PathVariables To The Modelで正しい方向を示してくれました。

于 2011-11-27T17:24:52.590 に答える
4

@PathVariable呼び出された URL のパスから、注釈付きのメソッド引数を抽出する必要があることを意味します。@RequestParamアノテーション付きのメソッド引数をリクエスト パラメータから抽出する必要があることを意味します。これらのアノテーションのいずれも、アノテーション付きの引数をリクエスト、セッション、またはアプリケーションのスコープに入れることはありません。

${username}「ユーザー名属性の値(ページ、リクエスト、セッション、またはアプリケーションスコープで見つかった)をレスポンスに書き込む」ことを意味します。これらのスコープのいずれにもユーザー名属性を含めていないため、何も書き込まれません。

メソッドが ModelAndView オブジェクトを返し、モデルにusername属性と属性が含まれている場合、コードは機能しstudentidます。

于 2011-11-27T16:18:10.277 に答える