简体   繁体   中英

Spring MVC 4.1.7 Class Level @RequestMapping

I am new to Spring MVC. I need a help, please.

I could deploy my app on the WebLogic 10.3.6 using Java EE Eclipse successfully. I could run mapped URL successfully on Controller's method level as below.

http://localhost:7001/SpringMVCHibernateProject/hello

When I try with class & method level @RequestMapping annotation it does not work. 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;
}

}

Can please someone help what could be wrong?

Thanks, Hitesh

You missed configuration in xml. <mvc:annotation-driven>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM