Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return same view controller using ModelAndView of Spring Web MVC

I am using Spring Web MVC and Hibernate for developing my application.

My login.jsp page has following code :

<form:form method="post" commandName="User">
   User Name : 
      <form:input path="email"/>
   Password : 
     <form:input path="password"/>

<input type="submit" align="center" value="Execute">

Now, My servlet.xml file has following code :

 <bean name="/uservalidate.htm" class="com.sufalam.mailserver.presentation.web.UserValidateFormController">
        <property name="sessionForm" value="true"/>
        <property name="commandName" value="User"/>
        <property name="commandClass" value="com.sufalam.mailserver.bean.User"/>
        <property name="formView" value="login"/>
        <property name="successView" value="layout.jsp"/>
        <property name="userSecurityProcessor" ref="IUserSecurityProcessor"/>
    </bean>

My UserValidateFormController has following code :

public class UserValidateFormController extends SimpleFormController {

    /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());
    private IUserSecurityProcessor userSecurityProcessor;

    public ModelAndView onSubmit(Object command)
            throws ServletException, SufalamException {
            ModelAndView mav = new ModelAndView();
            Map model = new HashMap();


        String username = ((User) command).getEmail();
        String password = ((User) command).getPassword();
        List userChecking = new ArrayList();
        userChecking = userSecurityProcessor.findByAll(username, password, 0.0);
        System.out.println("userChecking length = "+userChecking.size());
        if (userChecking.size() == 1) {
            return new ModelAndView("layout");
            //return new ModelAndView(new RedirectView(getSuccessView()));
        }

        return new ModelAndView("login", model);

    }

    protected Object formBackingObject(HttpServletRequest request) throws ServletException {
        User user = new User();
        return user;
    }

  public void setUserSecurityProcessor(IUserSecurityProcessor userSecurityProcessor) {
        this.userSecurityProcessor = userSecurityProcessor;

    }

In my UserValidateFormController at the time of handing submit event, i am checking that username and password are correct or not..

It's working fine & if both are matching then its redirecting to layout.jsp, that's also fine.

But if username or password are incorrect then i want to redirect to same login.jsp page and display appropriate error..

Please suggest me the solution that what to do redirecting to same view controller..

Thanks in advance..

like image 841
Nirmal Avatar asked Oct 29 '09 10:10

Nirmal


2 Answers

Finally solve this issue with following line of code :

return new ModelAndView(new RedirectView(getSuccessView()));

or

return new ModelAndView(new RedirectView("success.htm");

Thanks...

like image 110
Nirmal Avatar answered Sep 20 '22 12:09

Nirmal


If you have an InternalResourceViewResolver configured, you can do it like this:

return new ModelAndView("redirect:success.htm");

In my opinion, this is clearer.

like image 39
juan Avatar answered Sep 19 '22 12:09

juan