Requestmapping注释

时间:2012-08-18 08:02:27

标签: java-ee spring-mvc spring-annotations

您好我正在尝试使用Requestmapping,但它不起作用,

HelloController.java

package com.project.springapp;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/Springapp/hello")
public class HelloController {

@RequestMapping(method = RequestMethod.GET)
public String Hello(){
    return "hello";
}

}

servlet的context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org
/schema/mvc/spring-mvc-3.0.xsd
    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">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

   <beans:bean 
     class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

<beans:bean     
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

<resources mapping="/resources/**" location="/resources/" />


<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

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

 </beans:beans>

Web.xml中

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

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-
class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

运行代码后,它会在控制台

中显示此警告
  

警告:[SetContextPropertiesRule] {Context}设置属性'source'   到'org.eclipse.jst.jee.server:Springapp'没找到匹配的   属性。

然后转到第一页,但是当我输入此URl http://localhost:8080/Springapp/hello时 它在控制台中显示此消息:

  

警告:“org.springframework.web.servlet.PageNotFound - 没有映射   找到带有URI [/ Springapp / hello]的HTTP请求   DispatcherServlet,名称为“appServlet”“

感谢。

1 个答案:

答案 0 :(得分:1)

您必须添加映射到类方法,作为URL的端点。例如,要访问http://localhost:8080/Springapp/hello,您可以执行以下操作:

package com.project.springapp;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/Springapp")
public class HelloController {

@RequestMapping("/hello")
public String Hello(){
    return "hello";
}

}

另外,您可能需要在'/ Springapp / hello

之前在url中包含应用程序上下文路径