• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

@RequestMapping at class level issue

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am new to spring.this question may be too silly but i realyy need help.
I am having issues when I am using @RequestMapping in class level.

Please have a look and let me know the cause of this problem:
CONTROLLER CLASS:
package com.gontuseries.hellocontroller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/greet/*")
public class HelloController {

@RequestMapping("/welcome")
protected ModelAndView Hello() {
ModelAndView modelandview = new ModelAndView("HelloPage");
modelandview.addObject("welcomeMessage", "Jyoti");

return modelandview;
}


}

FRONT CONTROLLER:
<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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:component-scan base-package="com.gontuseries.hellocontroller" />
<mvc:annotation-driven/>

<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>

when it is returning the path, i dont know why "/greet/" is getting appended to the classpath..beacuse of which the correct location of jsp page is misleading and i get a 404 error.

ERROR MESSAGE:
type Status report

message /NewSpPr/greet/WEB-INF/HelloPage.jsp

description The requested resource (/NewSpPr/greet/WEB-INF/HelloPage.jsp) is not available.



Please reply to my issue.
 
Sheriff
Posts: 17405
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that you think you're saying one thing but Spring is taking what you said to mean something different. What do you think you're telling Spring by putting that annotation at the class level? Because Spring is taking it to mean that all handling methods on this controller are relative to the /greet path.
 
joe das
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please refer the below code:
Controller:

package com.gontuseries.hellocontroller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

@RequestMapping("greet/welcome")
protected ModelAndView Hello() {
ModelAndView modelandview = new ModelAndView("HelloPage");
modelandview.addObject("welcomeMessage", "Jyoti");

return modelandview;
}

@RequestMapping("greet/home")
protected ModelAndView Hello() {
ModelAndView modelandview = new ModelAndView("HelloPage");
modelandview.addObject("welcomeMessage", "Jyoti");

return modelandview;
}

@RequestMapping("greet/country")
protected ModelAndView Hello() {
ModelAndView modelandview = new ModelAndView("HelloPage");
modelandview.addObject("welcomeMessage", "Jyoti");

return modelandview;
}

}

In the above code, i dont want to repeat "greet" everytime, so i want to use it in class level...but when I am using it in class level while returning the response "greet" is getting appended in the classpath.So what is the solution?
 
joe das
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please consider the three method names are different for the above sample code
:P
 
Junilu Lacar
Sheriff
Posts: 17405
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code.

And Welcome to the Ranch!
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your InternalResourceViewResolver bean, the prefix should be "/WEB-INF/", not "\WEB-INF/".
Also ensure that HelloPage.jsp is under /WEB-INF directory.
 
joe das
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You so much for the solution.such a silly mistake
Thanks a ton
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic