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.