0

我正在尝试使用 Spring 3.1 REST 控制器实现简单的示例。问题是由于某种原因,我没有为我的请求找到任何资源。我做错了什么?我正在使用Tomcat 7.0。这是我所拥有的:

web.xml

   <?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

    <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>/mvc/*</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>

    <welcome-file-list>

mvc-调度程序-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="my.spring31.mvc.*">

</context:component-scan>

<mvc:annotation-driven />


<bean class="my.spring31.mvc.TestController">
    <property name="message">
        <value>Hello World</value>
    </property>
</bean>

<bean
   class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value></value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

控制器:

   package my.spring31.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
@RequestMapping("/movie")
public class TestController {
        //DI via Spring
        String message;

        @RequestMapping(value="/{name}", method = RequestMethod.GET)
        public String getMovie(@PathVariable String name, ModelMap model) {

            model.addAttribute("movie", name);
            model.addAttribute("message", this.message);

            //return to jsp page, configured in mvc-dispatcher-servlet.xml, view resolver
            return "list";

        }

        public void setMessage(String message) {
            this.message = message;
        }
}

还有我的 list.jsp:

    <html>
<body>
    <h1>Spring 3 MVC REST example</h1>

    <h3>Movie : ${movie} , DI message : ${message}</h3> 
</body>
</html>
4

0 回答 0