user1608396
user1608396

Reputation:

Requestmapping annotation

Hi I am trying to use Requestmapping but it does not work,

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>

After running the code it shows this warning in console

WARNING: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Springapp' did not find a matching property.

then goes to first page but when I enter this URl http://localhost:8080/Springapp/hello it shows this message in console:

WARN : "org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/Springapp/hello] in DispatcherServlet with name 'appServlet'"

Thanks.

Upvotes: 0

Views: 1517

Answers (1)

Denis Zevakhin
Denis Zevakhin

Reputation: 624

you have to add mapping to class methods, as the endpoints to the urls. for example, to access http://localhost:8080/Springapp/hello , you could do something like:

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";
}

}

also, you may need to include the application context path in the url as well before '/Springapp/hello

Upvotes: 1

Related Questions