3

So, say I have an existing, working page Display Cashier, which displays information about a cashier in a shop. Now, I add a button to this page that looks like:

<a href="handleGetManager.ctl?cashierId=${id}" class="btn btn-mini">Manager</a>

The request-mapping for this URL maps it (successfully) to a controller: HandleGetManager

the HandleGetManager controller looks like this:

@Controller
public class HandleGetManager{

    private employeeBO employeeBO; //BO handles all business logic

    //spring hooks
    public HandleGetManager(){} 
    public void setemployeeBo(employeeBO employeeBO){
    this.employeeBO = employeeBO;
    }

    //get controller 
    @RequestMapping(method=RequestMethod.GET)
    public String getManager(@RequestParam String cashierId){

    Long managerId = employeeBO.getManagerByCashierId(cashierId); 

    String redirectUrl = "/displayManager.ctl?managerId=" + managerId.toString();
    return redirectUrl;
    }

}

Here's what happens when I try it:

I hit the new button on the Display Cashier page, I expect the following to happen:

  • The browser sends a get request to the indicated URL

  • The spring request-mapping ensures that the flow of control is passed to this class.

  • the @RequestMapping(method=RequestMethod.GET) piece ensures that this method is evoked

  • The @RequestParam String cashierId instructs Spring to parse the URL and pass the cashierId value into this method as a parameter.

  • The EmployeeBo has been injected into the controller via spring.

  • The Business logic takes place, envoking the BO and the managerId var is populated with the correct value.

  • The method returns the name of a different view, with a new managerId URL arg appended

Now, up until this point, everything goes to plan. What I expect to happen next is:

  • the browsers is directed to that URL

  • whereupon it will send a get request to that url,

  • the whole process will start again in another controller, with a different URL and a different URL arg.

instead what happens is:

  • this controller returns the name of a different view

  • The browser is redirected to a half-right, half wrong URL: handleGetManager.ctl?managerId=12345

  • The URL argument changes, but the name of the controller does not, despite my explicitly returning it

  • I get an error

What am I doing wrong? Have I missed something?

Paul
  • 3,318
  • 8
  • 36
  • 60
  • 2
    Depending on your `ViewResolver` you can prefix `redirectUrl` with `redirect:` to make the redirect happen. – Bart Jul 01 '14 at 14:16
  • Hi Bart, Thanks for your reply, could you explain the difference? What does the "redirect:" do? Isn't the view specified by the string returned? – Paul Jul 01 '14 at 14:38
  • You don't return a view from a controller. You typically return a view name that will be translated into a `View` implementation. When using a `UrlBasedViewResolver` prefixes like `redirect:` and `forward:` have a special meaning. In case of `redirect:` it will be translated into a `RedirectView` pointing to the value after the prefix. – Bart Jul 01 '14 at 20:01
  • Well... Like Sotirios Delimanolis said :-) – Bart Jul 01 '14 at 20:02

1 Answers1

1

Assuming you have a UrlBasedViewResolver in your MVC configuration, the String value you return is a View name. The ViewResolver will take that name and try to resolve a View for it.

What you seem to want to do is to have a 301 response with a redirect. With view names, you do that by specifying a redirect: prefix in your view name. It's described in the documentation, here.

Here's a question/answer explaining all the (default) ways you can perform a redirect:

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Hi Sotirios, Thanks very much for your answer! Is the Dev required to implement/configure the ViewResolver in Spring, or can it run as default out of the box? – Paul Jul 02 '14 at 10:48