0
votes

I'm trying to get this done using Spring MVC: my welcome page is index.jsp located right in my /webapp folder so it can be set as welcome page. This page has a login form. When the login fails, I'd like to redirect to this welcome page (redirect:/) and pass an error message which can then be shown using JSTL. The problem is, when I do the redirect the model is gone and the errormessage too. But I can't just return the viewname because the index.jsp is in the webapp folder and my View Resolver is mapped on /WEB-INF/pages/...

Anyone know how I can get this to work? This is what I have so far:

web.xml

<web-app schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    <display-name>TDD oefening</display-name>

    <servlet>
        <servlet-name>springservlet</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/springservlet-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springservlet</servlet-name>
        <url-pattern>/web/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

springservlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="be.kdg.prog4.tdd"/>
    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <mvc:resources mapping="/resources/**" location="/resources/"/>
</beans>

My controller method for handling the login

@RequestMapping(value = "/login.do", method = RequestMethod.POST)
    public ModelAndView login(@RequestParam String username, @RequestParam String password) {
        boolean ok = service.checkLogin(username, password);
        ModelAndView mav;
        if (!ok) {
            mav = new ModelAndView("redirect:/");
            mav.addObject("error", "Wrong username or password");
            return mav;
        }

        mav = new ModelAndView("favorites");
        mav.addObject("username", username);
        mav.addObject("isRoot", username.equals("root"));
        return mav;
    }

I would like to display the message after redirect here:

<span id="errormsg" name="errormsg" style="color: red;">${error}</span>

All the necessary taglibs are present at the top of the .jsp page and jstl and cglib and such are loaded in my pom.xml.

Thanks in advance.

1

1 Answers

2
votes

To persist your model after redirecting, you need to use Flash attributes.

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-flash-attributes

Here is an example extracted from the JavaDoc:

@RequestMapping(value = "/accounts", method = RequestMethod.POST)
 public String handle(Account account, BindingResult result, RedirectAttributes redirectAttrs) {
   if (result.hasErrors()) {
     return "accounts/new";
   }
   // Save account ...
   redirectAttrs.addAttribute("id", account.getId()).addFlashAttribute("message", "Account created!");
   return "redirect:/accounts/{id}";
 }

And the JavaDoc :)

http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/mvc/support/RedirectAttributes.html

EDIT -- To reflect the comment made by @M. Deinum

You could add a simple refresh meta in your index.jsp and add and map a login.jsp page inside your spring application. That will make sure that every navigation done by your users will be controlled by the Spring DispatchServlet.

A lil snipped to illustrate the idea.

index.jsp

<meta http-equiv="refresh" content="0; url=/login" />

A request mapping defined somewhere in your controllers

@RequestMapping(value = "/login", method = RequestMethod.GET)
public String doLogin(Model model) {
    // This return sttm needs to reflect the right path to your login.jsp
    return "login";
}