Stories about Software


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:

public class KitchenController {

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

Newest Most Voted
Inline Feedbacks
View all comments
Brian Romanowski
12 years ago

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!).

Madhu Chatterjee
Madhu Chatterjee
12 years ago
Reply to  Erik Dietrich

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


[…] s.parentNode.insertBefore(po, s); })(); So, in response to feedback from my previous post about my   home automation server site, I’ve decided to incorporate AJAX and JQuery into my […]