1

Mkyong.com のチュートリアルで SpringMVC を学んでいます。彼のチュートリアルでは、Maven を使用してプログラムを作成しています。

このプログラムでは、Maven を使用してビルドしていません。

私のサンプルコード:

コーヒージャバ

package com.springMVC;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "coffee")
public class Coffee {

    String name;
    int quanlity;

    public String getName() {
        return name;
    }

    @XmlElement
    public void setName(String name) {
        this.name = name;
    }

    public int getQuanlity() {
        return quanlity;
    }

    @XmlElement
    public void setQuanlity(int quanlity) {
        this.quanlity = quanlity;
    }

    public Coffee(String name, int quanlity) {
        this.name = name;
        this.quanlity = quanlity;
    }

    public Coffee() {
    }

}

XMLController.java

package com.springMVC;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.springMVC.Coffee;

@Controller
@RequestMapping("/coffee")
public class XMLController {

    @RequestMapping(value="{name}", method = RequestMethod.GET)
    public @ResponseBody Coffee getCoffeeInXML(@PathVariable String name) {

        Coffee coffee = new Coffee(name, 100);

        return coffee;

    }

mvc-dispacter-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.springMVC" />

    <mvc:annotation-driven />

</beans>

web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring Web MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

私のコード構造:

MkYong.com のチュートリアル ( http://www.mkyong.com/spring-mvc/spring- 3-mvc-and-xml-example/ )

4

1 に答える 1

1

URL マッピングの仕組みは次のとおりです。

  1. web.xml は、プレフィックス付きの URL を一部のサーブレットにルーティングし、プレフィックス /rest/ を持つすべてのものを春の DispatcherServlet にマップしました。
  2. コントローラーのルーティングは、注釈を付けた@Controller注釈へのパラメーターによって処理されます/coffee
  3. 最後に、コーヒーの名前である変数があり、@PathVariable.

だからどこにも見えない/MVCXML/。mkyong.com は web.xml を提供していないため、web.xml と Spring のディスパッチャー サーブレットがどのように連携するかをまだ知らない場合、プレフィックスがどうあるべきかは少し不明です。

正しい URI は ですhttp://localhost:8080/rest/coffee/arabic

于 2013-09-09T17:47:49.880 に答える