1

我正在通过 Mkyong.com 教程学习 SpringMVC。在他的教程中,Maven 用于构建程序。

我没有使用 Maven 在这个程序中构建它。

我的示例代码:

咖啡.java

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>

我的代码结构:

请给我一个 URI(例如:https:\localhost:8080/MVCXML/rest/coffee/arabic)来查看 MkYong.com 教程中的输出(http://www.mkyong.com/spring-mvc/spring- 3-mvc-and-xml-example/ )

4

1 回答 1

1

以下是 URL 映射的工作原理:

  1. web.xml 将带有前缀的 url 路由到某个 servlet,并且您已将带有前缀 /rest/ 的所有内容映射到 spring DispatcherServlet。
  2. 控制器路由由@Controller注释的参数处理,您已使用注释/coffee
  3. 然后在末尾有一个变量是咖啡的名称,它被放置在@PathVariable.

所以我看不到那里的任何地方/MVCXML/。mkyong.com 没有提供 web.xml,所以如果您还不知道 web.xml 和 Spring 的调度程序 servlet 如何协同工作,那么前缀应该是什么有点不清楚。

正确的 URI 应该是http://localhost:8080/rest/coffee/arabic.

于 2013-09-09T17:47:49.880 回答