Solved

Weird problem on redirect in spring controller

Posted on 2012-03-14
4
1,486 Views
Last Modified: 2012-03-19
I have a weird issue in a spring controller (Spring vs 3.1.1).  I am redirecting to a URL and the dispatcher servlet cannot resolve the handler method.  It works when I click a link in the JSP but its broke when I create the redirect URL.  The URL's that are created look identical but there is some difference because I can copy and paste one into a new browser tab and it works and the other one I cannot.

This method gets called fine via clicking a link on the UI.  Here are the logs.

 2012-03-14 11:54:38,091 DEBUG [org.springframework.web.servlet.DispatcherServlet] Flow Id:e38704d2-bcb1-4d81-981a-8bf3a7f9b7a2 Flow Name:Unknown - <DispatcherServlet with name 'appServlet' processing GET request for [/storedashboard/shelfLocation/view]>
2012-03-14 11:54:38,091 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] Flow Id:e38704d2-bcb1-4d81-981a-8bf3a7f9b7a2 Flow Name:Unknown - <Looking up handler method for path /shelfLocation/view>
2012-03-14 11:54:38,091 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] Flow Id:e38704d2-bcb1-4d81-981a-8bf3a7f9b7a2 Flow Name:Unknown - <Returning handler method [public org.springframework.web.servlet.ModelAndView com.xxxx.xxxx.store.dashboard.controller.ShelfLocationController.view(long)]>
2012-03-14 11:54:38,091 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] Flow Id:e38704d2-bcb1-4d81-981a-8bf3a7f9b7a2 Flow Name:Unknown - <Returning cached instance of singleton bean 'shelfLocationController'>
2012-03-14 11:54:38,091 DEBUG [org.springframework.web.servlet.DispatcherServlet] Flow Id:e38704d2-bcb1-4d81-981a-8bf3a7f9b7a2 Flow Name:Unknown - <Last-Modified value for [/storedashboard/shelfLocation/view] is: -1>

Open in new window



The code for the link in the jsp looks like this:
<a target="_blank" href="<c:url value="/shelfLocation/view?shelfLocationId='+shelfLocationId+'"/>">

Open in new window


The controller handling it looks like this:
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public ModelAndView view(@RequestParam(value = "shelfLocationId", required = true)
    final long id)
    {
		//stuff omitted for brevity
        return new ModelAndView("cms/shelfLocation", "shelfLocationFormBean", shelfLocationFormBean);
    }

Open in new window


Now all of that works fine but when I try to redirect to that url I get a 404.  Here is the controller method doing the redirecting:

 @RequestMapping(value = "/createShelfDisplay", method = RequestMethod.POST)
    public String createShelfDisplay(ShelfLocationFormBean mediaUploadForm, BindingResult result)
    {
		//stuff omitted for brevity
        UriComponents redirectUri = UriComponentsBuilder.fromPath("/shelflocation/view")
            .queryParam("shelfLocationId", shelfLocation.getId()).build().encode();
        return "redirect:"
            + redirectUri.toUriString();

    }

Open in new window


Now the dispatcher servlet cannot figure out the mapping see the logs:
	2012-03-14 11:42:24,853 DEBUG [org.springframework.web.servlet.DispatcherServlet] Flow Id:f02cb747-be90-4702-8a1d-5dbc2bb1536c Flow Name:CMS: Create Shelf Display - <DispatcherServlet with name 'appServlet' processing GET request for [/storedashboard/shelflocation/view]>
2012-03-14 11:42:24,853 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] Flow Id:f02cb747-be90-4702-8a1d-5dbc2bb1536c Flow Name:CMS: Create Shelf Display - <Looking up handler method for path /shelflocation/view>
2012-03-14 11:42:24,853 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] Flow Id:f02cb747-be90-4702-8a1d-5dbc2bb1536c Flow Name:CMS: Create Shelf Display - <Did not find handler method for [/shelflocation/view]>
2012-03-14 11:42:24,853 WARN [org.springframework.web.servlet.PageNotFound] Flow Id:f02cb747-be90-4702-8a1d-5dbc2bb1536c Flow Name:CMS: Create Shelf Display - <No mapping found for HTTP request with URI [/storedashboard/shelflocation/view] in DispatcherServlet with name 'appServlet'>

Open in new window


The below link is generated on the redirect and does not work.  I cannot copy and paste this into a new browser tab and get in my handler method.
http://localhost:8080/storedashboard/shelflocation/view?shelfLocationId=1000 

The below link is generated from the href link in the jsp.  It does work.  I can copy and paste it into a new tab on my browser and get into my handler method.
http://localhost:8080/storedashboard/shelfLocation/view?shelfLocationId=1000

What am I doing wrong?
0
Comment
Question by:Mr_Oz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 7

Expert Comment

by:gudii9
ID: 37721022
0
 
LVL 1

Author Comment

by:Mr_Oz
ID: 37721172
I think its an issue with the URL encoding or something I guess I do not see the relevance with any of the links you have given.  Also I am using 3.1.1 some of the information you posted is not even applicable doing things the Spring 3.1.1 way.  Also I do not have any file extensions, I don't know where you got that from.
0
 
LVL 1

Accepted Solution

by:
Mr_Oz earned 0 total points
ID: 37721665
Arrgh I spent all day on this.  Apparently I had c+p the word 'redirect:' from something into eclipse and that was the problem typing it out into the IDE fixed the problem.  it was a fluke there is no problem with the code.
0
 
LVL 1

Author Closing Comment

by:Mr_Oz
ID: 37736805
this was a c+p fluke.  When copying and pasting into the ide somethign weird had happened but was not apparent by looking at it.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
CSV file parsing thru Java 13 54
wild fly 8 startup error 2 82
jmeter usage 4 65
dao vs facade design patterns 2 72
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question