【问题标题】:Spring MVC 4.1.7 Class Level @RequestMappingSpring MVC 4.1.7 类级别@RequestMapping
【发布时间】:2015-08-04 21:17:13
【问题描述】:

我是 Spring MVC 的新手。我需要帮助。

我可以使用 Java EE Eclipse 在 WebLogic 10.3.6 上成功部署我的应用程序。我可以在 Controller 的方法级别上成功运行映射 URL,如下所示。

http://localhost:7001/SpringMVCHibernateProject/hello

当我尝试使用类和方法级别的 @RequestMapping 注释时,它不起作用。 http://localhost:7001/SpringMVCHibernateProject/welcome/hello

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" id="WebApp_ID" version="2.5">  
<display-name>SpringMVCHibernateProject</display-name>

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

<servlet-mapping>
 <servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

spring-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context" 
   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-4.0.xsd 
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="au.com.snh.controllers" />

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

HelloAnnotationController.java

package au.com.snh.controllers;

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.servlet.ModelAndView;

@Controller
@RequestMapping("/welcome")
public class HelloAnnotationController {

@RequestMapping(value = "/hello", method = RequestMethod.GET)
public ModelAndView sayHello(){
    ModelAndView model = new ModelAndView("/pages/hello");
    model.addObject("msg", "Hello Spring MVC World");

    return model;
}

@RequestMapping("/hi")
public ModelAndView sayHi(){
    ModelAndView model = new ModelAndView("/pages/hi");
    model.addObject("msg", "Hi Spring MVC World");

    return model;
}

}

可以请人帮忙解决什么问题吗?

谢谢, 希特什

【问题讨论】:

  • 不要只说它不起作用,您能否显示确切的错误消息、堆栈跟踪,如果可能的话,还提供org.springframework 的信息或调试级别?跨度>
  • 嗨,谢尔盖,感谢您的回复。当我尝试使用类级别映射和方法级别运行时,它只是显示 404 错误,如上所示。日志文件或控制台中没有错误。

标签: java spring model-view-controller


【解决方案1】:

您错过了 xml.&lt;mvc:annotation-driven&gt; 中的配置

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 2012-01-07
    • 2015-06-22
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多