Redirect Back with Spring 3.0 MVC

As I’m getting back into Java and particularly Spring MVC, it’s a bit of an adjustment period, as it always is when you’re dusting off old skills or trying something new. Things that should be easy are maddeningly beyond your reach. In light of that, I’m going to document a series of small milestones as I happen on them, in the hopes that someone else in my position or a complete newbie might find it useful.

So, today, I’m going to talk about processing a GET request without leaving the page. The reason I wanted to do this is that I have a page representing my house’s kitchen. The page has two buttons (really links styled as buttons through CSS) representing lights on and off. I’m providing a restful URL scheme to allow them to be turned on and off: kitchen/on and kitchen/off will turn the lights on and off, respectively. However, when this happens, I don’t have some kitchen/off.jsp page that I want handling this. I want them redirected right back to the kitchen page for further manipulation, if need be.

Here is how this was accomplished. Pay special attention to the kitchen() method taking the variable name and request as paramters:

@Controller
@RequestMapping("/kitchen")
public class KitchenController {

	@RequestMapping("/kitchen")
    public ModelAndView kitchen() {
    	String message = "Welcome to Daedalus";
        return new ModelAndView("kitchen", "message", message);
    }
	
	/*
	 * This takes kitchen and some request after it and satisfies the request
	 */
	@RequestMapping(value="/{name}", method = RequestMethod.GET)
	public String kitchen(@PathVariable String name, HttpServletRequest request) {
		try {
			Runtime.getRuntime().exec("/usr/local/heyu-2.6.0/heyu " + name + " A2");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:" + request.getHeader("Referer");
	}
}

So, the idea here is that I’m returning a redirect to the referrer. So, basic logic flow is that client sends an HTTP get request by clicking on the link. We process the request, take appropriate action, and then redirect back to where he started.

This certainly may not be the best way to accomplish what I’m doing, but it’s a way. And, my general operating philosophy is to get things working immediately, and then refactor and/or optimize from there. So, if you know of a better way to do it, by all means, comment. Otherwise, I’ll probably figure one out on my own sooner or later.

  • http://pwnetic.com Brian Romanowski

    What do you think about using some AJAX-y magic to send a PUT request to change the state of your lights instead of GET? Not only is it more “pure” REST, but it should avoid the need for a redirect. Of course, it’s more complicated, but apparently there is some easy JQuery way to do it (…and now you have 2 problems!).

  • http://www.daedtech.com Erik

    Hey Brian,

    Good to see you :)

    I appreciate the suggestion. I don’t know a lot about AJAX, so I dug around a bit and found this primer. I’m going to look at doing this since I’m not really “GET”-ing anything and I like the idea of an async callback to server rather than unnecessary redirects. Thanks for the tip — this is exactly the kind of feedback I was hoping for.

  • Madhu Chatterjee

    I also think, AJAX ( with very simple JavaScript ) will be better approach – more flexibility in Client GUI, instead of redirecting entire page.

  • http://www.daedtech.com Erik

    Hey Madhu — thanks for the feedback!

    So, with two more informed voices than mine agreeing on the subject, it’s decided. I will implement the AJAX solution, probably over the weekend. There may be a blog post to follow about my fumbling around with that, too. :)

  • Pingback: Setting Up AJAX and JQuery | DaedTech()