I’m learning Spring 3.1.
My webapp name is “acme”.
The url is roughly https://blah.blah.blah/acme
That URL is set up to display the login.jsp
I have a “/login” mapping in my controller that my login.jsp submits to
If something goes wrong it return the user to the login.jsp with this url in the browser:
https://blah.blah.blah/acme/login
The “/login” mapping is set up to handle POST requests, so I am concerned about users bookmarking
https://blah.blah.blah/acme/login, and getting the error message of “GET request not supported”
So, I thought I would put in a function to handle GET requests to /login to reroute through my general mapping handler for “/” and “/home”:
Login.Controller.java
package gov.noaa.acme.controller;
import java.security.Principal;
import javax.servlet.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.validation.*;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.apache.log4j.Logger;
@Controller
public class LoginController {
private static final Logger logger = Logger.getLogger(LoginController.class);
@RequestMapping({"/","home"})
public String home(ModelMap model,HttpSession session,HttpServletRequest request) {
// Do some stuff
return "login";
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login_get(){
logger.debug("started...");
return "forward:home";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@ModelAttribute("laph") LAPH laph,
BindingResult bindingResult,
ModelMap model,
HttpSession session,
HttpServletRequest request,
HttpServletResponse response,
@RequestParam(required=true) String last_usertype) {
if (bindingResult.hasErrors()) {
logger.debug("Error returning home");
return "home";
}
logger.debug("Started ....");
// Do authentication
if (!isAuthenticated) {
model.put("status_message", error_message);
return "login";
}
// success, send newly authenticated user to a search page
nextView = "search";
return "redirect:" + nextView;
}
}// end class LoginController
My logs show that I am not even reaching the controller method for handling GET requests for /login, I’m still getting the error messages that GET is not supported for /login.
Any ideas on how I can fix this?
Thanks
Steve
Your method signatures are correct; with the annotations you have placed on
login_getandlogin, Spring will not be confused and will invoke the correct methods for GET and POST requests.Your method
homeis wrong; it returns the string"login", but I guess you do not have a view named login and that you would like it to invoke one of the login methods. In that case you should have returned “forward:login”, but this solution is not much better.My advice is:
/homeshould render the home view, by using filehome.jspor whatever view technology you’re using.