0

嗨我正在尝试使用 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 - 在 DispatcherServlet 中找不到带有 URI [/Springapp/hello] 的 HTTP 请求的映射,名称为 'appServlet'”

谢谢。

4

1 回答 1

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 中包含应用程序上下文路径

于 2012-08-18T17:38:19.607 回答