简体   繁体   中英

Spring-mvc: controller doesn't load new page after successful login

Using Spring MVC I created a login page named login.jsp and my main page, index.jsp. On the login page I have 2 text fields for username and password, and a button to submit the data. The values are checked against a database and for a valid id the index.jsp will be shown, while an error will be redirected to a separate exception.jsp. The problem is that the browser won't display either index.jsp or exception.jsp. It just stays on the login page. However, the HTTP response to the POST action contains the code of the correct page. The browser simply won't display it. Here is a snippet from my controller:

// -------------- Login ----------------

@RequestMapping(value = "/login", method = RequestMethod.GET)
public String showLogin(Locale locale, Model model) {
    logger.info("Welcome to login page!");
    return "login";
}


@RequestMapping(value = "/login", method = RequestMethod.POST, consumes = "application/json")
public String doLogin(@RequestBody LoginBean bean) throws Exception {
    logger.info("Trying to log in. User: " + bean.getBenutzername() + " Password: " + bean.getPasswort());
    long returnedIndex = inputManager.verifyLogin(bean);
    // check db;
    if(returnedIndex != -1){
        user = returnedIndex;
        logger.info("Success! Welcome, " + bean.getBenutzername());
        return "redirect:/index";
    }
    else {
        logger.info("Failed to log in");
        throw new Exception("User or password incorrect. Try again.");
    }
}

// -------------- Index ----------------

@RequestMapping(value = "/index", method = RequestMethod.GET)
public String showHome(Locale locale, Model model) throws Exception {
    logger.info("Welcome to home page!");
    outputManager.createBeans(1l);
    return "index";
}

// -------------- Exceptions ----------------
@ExceptionHandler(Exception.class)
public ModelAndView handleAllException(Exception ex) {
    logger.info("Catching exception: " + ex.getMessage());
    ModelAndView model = new ModelAndView("exception");
    model.addObject("msg", ex.getMessage());
    return model;
}

The problem was actually on the front end. We use AngularJS and the values returned by the methods in the controller are not interpreted correctly. I'm not sure what exactly went wrong (I'm not working on the front end), but I think the scripts we use are responsible for modifying only parts of a web page in a single page application, and not jumping from one page to another.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM