Spring Controller's URL request mapping not working as expected - model-view-controller

I have created a mapping in web.xml something like this:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/about/*</url-pattern>
</servlet-mapping>
In my controller I have something like this:
import org.springframework.stereotype.Controller;
#Controller
public class MyController{
#RequestMapping(value="/about/us", method=RequestMethod.GET)
public ModelAndView myMethod1(ModelMap model){
//some code
return new ModelAndView("aboutus1.jsp",model);
}
#RequestMapping(value="/about", method=RequestMethod.GET)
public ModelAndView myMethod2(ModelMap model){
//some code
return new ModelAndView("aboutus2.jsp",model);
}
}
And my dispatcher-servlet.xml has view resolver like:
<mvc:annotation-driven/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp"/>
To my surprise: request .../about/us is not reaching to myMethod1 in the controller. The browser shows 404 error. I put a logger inside the method but it isn't printing anything, meaning, its not being executed.
.../about works fine! What can be the done to make .../about/us request work? Any suggestions?

You need to use #RequestMapping(value="/us", method=RequestMethod.GET) or you need to request about/about/us

Since you have mapped "/about" in your web.xml, the url it will pass will be like this www.xyz.com/about/*
As your configuration says it will work for
www.xyz.com/about/about/us
www.xyz.com/about/about
In order to to work properly either use
/* in web.xml instead of /about
or change the controller's endpoint to
#RequestMapping(value="/us", method=RequestMethod.GET)
#RequestMapping(value="/", method=RequestMethod.GET)

Okay I got the thing working, here are things I added in the dispatcher-servlet.xml:
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="alwaysUseFullPath" value="true" />
</bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="alwaysUseFullPath" value="true" />
</bean>

Related

springmvc and url-pattern

web.xml
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
controller
#Controller
#RequestMapping("/car/*")
public class CarController extends BaseController {
#RequestMapping("baojia.html")
public ModelAndView baojia() {
ModelAndView view = new ModelAndView();
view.setViewName("baojia");
return view;
}
when i visit http://mydomain/car/baojia.html and has this error:
[carloan]2016-04-21 09:01:31,177 WARN [org.springframework.web.servlet.PageNotFound] - <No mapping found for HTTP request with URI [/views/baojia.jsp] in DispatcherServlet with name 'springMVC'>
spring.xml ViewResolver
<bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="cache" value="false"/>
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="prefix" value="/views/"/>
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
and i have file in /views/boajia.jsp
whether i writer, it don't work
<mvc:resources mapping="/views/" location="/views/**" />
and i have another question, i wan't to matching this url-pattern: /api/*
and the controller is:
#Controller
#RequestMapping("/api/*")
public class CarApiController extends BaseController {
#RequestMapping("get")
#ResponseBody
public JsonResult getCars()
but it can't work
try #RequestMapping("/car") instead of #RequestMapping("/car/*")
And check below two links to understand, how request mapping defined.
can anybody explain me difference between class level controller and method level controller..?
http://duckranger.com/2012/04/advanced-requestmapping-tricks-controller-root-and-uri-templates/
URL mapping declaration is not proper use #RequestMapping("/car") and #RequestMapping("/baojia.html")

The spring web mvc framework handle the jsp file dispatch as another request

I am new to spring web mvc framework,and I use struts 2 before.
I create a new dynamic web project using eclipse EE,and add all the jars to the /web-info/lib.
The whole hierarchy of the project is like this:
SpringMVCTest
WEB-INF
web.xml
example-servlet.xml
jsp
hello.jsp
lib
xxxx.jars
.....
This is the servlet definition:
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
This is the example-servlet.xml:
<context:component-scan base-package="com.kk.web.controllers" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
And the controller:
package com.kk.web.controllers;
#Controller("example")
#RequestMapping("/example")
public class ExampleController {
#RequestMapping("/hello")
#ResponseBody
public String hello() {
return "hello";
}
#RequestMapping("/hello_jsp")
public ModelAndView hello_jsp(){
ModelAndView mv=new ModelAndView("hello");
mv.addObject("message", "welcome");
return mv;
}
}
It worked when I run:
http://localhost:8080/SpringMVCTest/example/hello
But when I run:
http://localhost:8080/SpringMVCTest/example/hello_jsp
I got the warn:
2011-10-17 10:36:15 org.springframework.web.servlet.DispatcherServlet noHandlerFound
Warn: No mapping found for HTTP request with URI [/SpringMVCTest/WEB-INF/jsp/hello.jsp] in DispatcherServlet with name 'example'
It seems that the ExampleController works,it dispatch the request "/example/hello_jsp" to the right view "jsp/hello.jsp".
But then the spring take the file dispatch "/jsp/hello.jsp" as another request,then it will not find the matched url mapping in the "example" controller.
Why?? IMO,a requst must come from the client to server,the controller receive only one request here "/exmaple/hello_jsp",isn't it?
And How to fix it?
BTW,I can set the url pattern to "/*.xxx",but I do not want the suffix in the url.
Any ideas?
Previous answer did not work...
This posting looks similar: http://forum.springsource.org/showthread.php?55982-No-mapping-found-for-HTTP-request-with-MVC-requests
Summary: change
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
to
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
and checking my latest Spring MVC app I use the latter pattern (no * on the end).

RequestMapping not working with multi-level URLs

I have a scenario where I'm making a simple get request through a link and my #RequestMapping configuration is not behaving as I'd expect.
Within an anchor tag I reference a url with the following pattern '/action-plan/export/pdf?token=xxx&taskId=1111&taskId=2222...'
Within my controller class I have this mapping at the class level:
#RequestMapping("/action-plan/export")
And this mapping at the method level
#RequestMapping(value="/pdf", method=RequestMethod.GET)
public String exportToPdf(#RequestParam("taskId") String[] taskIds,
#RequestParam("token") String[] encryptedEmplId, ModelMap model)
But every time I try this I get a 404 page not found error and the following Spring exception:
org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException: No matching handler method found for servlet request: path '/pdf', method 'GET', parameters map['taskId' -> array['1962326', '1962264', '1962317', '1962328', '1962324', '1962427', '1962325', '1962323', '1963147', '1962327', '1962318', '1962329', '1962330'], 'token' -> array['xxxx']]
I've noticed that when I remove the "/pdf?" portion of the link and remove 'value="/pdf"' from the method #RequestMapping it works fine. For the life of me I don't understand why adding /pdf to the url and RequestMapping is not working.
I think danny.lesnik's answer was pretty close but I'm writing my own answer so I can be more verbose.
I was working on a different project and figured out why the above doesn't work. In reference to my original question here is the relevant web.xml servlet mapping:
<servlet-mapping>
<servlet-name>spring-dispatcherServlet</servlet-name>
<url-pattern>/action-plan/export/*</url-pattern>
</servlet-mapping>
I noticed that whatever portion of the path I included in the of web.xml was not being included in the evaluation of RequestMapping values. I would have thought this bean configuration would have prevented that scenario (note the "alwaysUseFullPath" property):
<bean id="annotationHandlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="2"/>
<property name="alwaysUseFullPath" value="true"/>
</bean>
Maybe someone can shed some light on this detail for me.
In any case, thanks danny.lesnik
I recreated your problem and solved it by mapping servlet using .action extentions.
For example:
#Controller
#RequestMapping(value="/test")
public class DefaultController {
#RequestMapping(value="/pdf.action", method=RequestMethod.GET)
public ModelAndView indexView(#RequestParam("taskId") String[] taskIds,
#RequestParam("token") String[] encryptedEmplId){
ModelAndView mv = new ModelAndView("index");
return mv;
}
Spring XML mapping:
<context:annotation-config />
<context:component-scan base-package="com.vanilla.controllers" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
and this web.xml servlet mapping
<display-name>SpringMvcServlet</display-name>
<servlet>
<servlet-name>SpringMvcServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMvcServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
This code resolves this url
/test/pdf.action?token=3&token=4&taskId=4
flawless.

mvc:view-controller causes PageNotFound in Spring Tiles2

I have a webapp based on Spring 3.0.6 which works fine on Tomcat 7.0.
The web.xml defines the dispatcher as following:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
The dispatcher defines the view resolver in the usual way:
<bean id="tilesViewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles-def.xml</value>
</list>
</property>
</bean>
I have a controller annotated with #RequestMapping("/home") and a "home" view defined in tiles-def.xml. When I point my browser to the /myapp/home.html, the Tiles page is opened.
If I add <mvc:resources mapping="/resources/**" location="/resources/" /> or <mvc:view-controller path="/" view-name="home.html"/> to my dispatcher xml file, pointing the browser to /myapp/home.html results in a 404. The log says:
21:34:22,128 WARN PageNotFound:947 – No mapping found for HTTP request with URI [/myapp/home.html] in DispatcherServlet with name 'dispatcher'
What am I doing wrong?
Thanks a lot
The problem in my application was due to the automatic view name resolution. My annotated method in my #Controller returned void, and the framework tried to guess the tiles view name using the request path.
I modified my annotated method as following, returning a String:
#RequestMapping(value="/page", method = RequestMethod.GET)
public String showForm(HttpServletRequest request, Model model) {
// TO BUSINESS LOGIC
// return tiles view name as configured in 'tiles-def.xml'
return "my_tiles_view_name";
}
With this change, everything works fine.

setting fullpath in controller

i have developed a spring application. all requests are dispatching to controllers (i have 2 controllers in my app) so web.xml is like below
in web.xml
<servlet-mapping>
<url-pattern>/*</url-pattern>
aaa controller
#Controller
#RequestMapping("/aaa")
bbb controller
#Controller
#RequestMapping("/bbb")
but now i need to add some jsp pages into my project since the "/*" in web.xml my jsp pages are not found. so i have change the servlet-mapping like below;
in web.xml
<servlet-mapping>
<url-pattern>/aaa/*</url-pattern>
<url-pattern>/bbb/*</url-pattern>
aaa controller
#Controller
#RequestMapping("/")
bbb controller
#Controller
#RequestMapping("/")
but i do not want to use this approach since i can access xxx servlet in aaa controler like /bbb/xxx.
so is there any alternative solution, for example can i set full path in controller or anything?
thanks in advance...
You need to pass jsp through the server as well.
You can map it as html extension
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
In example-servlet.xml just add the following jsp resolver
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
and then use ModelAndView Object in your controllers:
#Controller
#RequestMapping(value="/aaa")
public class aaaController{
#RequestMapping(value="/aaa.html", method=RequestMethod.GET)
public ModelAndView index(){
ModelAndView mv = new ModelAndView("aaa");
return mv;
}
}
#Controller
#RequestMapping(value="/bbb")
public class aaaController{
#RequestMapping(value="/bbb.html", method=RequestMethod.GET)
public ModelAndView index(){
ModelAndView mv = new ModelAndView("bbb");
return mv;
}
}
In that case first controller will return /aaa.jsp as you your model andView when you hit /aaa/aaa.html
and second controller will return /bbb.jsp as you your model and View when you hit /bbb/bbb.html
Hope it helps.

Resources