I am trying to create a simple Spring project with a GET method and am having trouble allowing access to it. Here is my controller…
package controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class IndexController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
String message = "Hello, World!";
return new ModelAndView("index", "msg", message);
}
@RequestMapping(method = RequestMethod.GET)
public ModelAndView setupForm(@RequestParam("name") String name) {
return new ModelAndView("index", "msg", name);
}
}
When I type the URL: ‘http://localhost:8080/HelloWorld/index.htm?name=jon’ for example, it simply returns the top method. Any ideas?
Many thanks,
J
Try getting rid of AbstractController (don’t extend it) and follow the advice given by Petter (btw, it’s better to add @RequestMapping to both methods). Instead, mark the controller with @Controller annotation. After that it should work.