Home > DeveloperSection > Forums > Why spring MVC optionally returning no view in a single mapping
Simons Hood
Simons Hood

Total Post:70

Points:494
Posted on    February-02-2015 10:29 PM

 ASP.Net ASP.NET MVC 
Ratings:


 1 Reply(s)
 725  View(s)
Rate this:

I have a case where i need to :

  • Return a 304 not modified status if the blog hasnt been modified
  • Or return the blog view if it's been modified since If-Modified-Since request header

The problem is when i want to return 304 status, how do i tell spring mvc not to assume another view from the null return, and start sending the response with the status immediately ?

@RequestMapping(value={"/blogs/{blogId}"}, method=RequestMethod.GET)

public String hello(final HttpServletRequest req, final HttpServletResponse resp, final Model model,

        @PathVariable("blogId") final String blogId) {

    if (isModified(req, blogId)) {

        resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);

        return null; // this doesnt stop spring mvc to assume a view name

    }

 

    populate(model, grabBlog(blogId));

    return "blog";

}



Brad Pitt
Brad Pitt

Total Post:61

Points:429
Posted on    February-02-2015 10:48 PM

From the HTTP perspective returning a view doesn't make sense at all. The Spring documentation covers that use case:

@RequestMapping

public String myHandleMethod(WebRequest webRequest, Model model) {

 

    long lastModified = // 1. application-specific calculation

 

    if (request.checkNotModified(lastModified)) {

        // 2. shortcut exit - no further processing necessary

        return null;

    }

 

    // 3. or otherwise further request processing, actually preparing content

    model.addAttribute(...);

    return "myViewName";

}

 


Don't want to miss updates? Please click the below button!

Follow MindStick