使用Spring Web MVC的ModelAndView返回相同的视图控制器

Nir*_*mal 6 java spring-mvc java-ee

我正在使用Spring Web MVC和Hibernate来开发我的应用程序.

我的login.jsp页面有以下代码:

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

<input type="submit" align="center" value="Execute">
Run Code Online (Sandbox Code Playgroud)

现在,我的servlet.xml文件包含以下代码:

 <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>
Run Code Online (Sandbox Code Playgroud)

我的UserValidateFormController有以下代码:

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;

    }
Run Code Online (Sandbox Code Playgroud)

在处理提交事件时我的UserValidateFormController中,我正在检查用户名和密码是否正确.

它工作正常,如果两者都匹配,然后重定向到layout.jsp,这也没关系.

但如果用户名或密码不正确,那么我想重定向到相同的login.jsp页面并显示相应的错误..

请建议我做什么重定向到同一个视图控制器的解决方案..

提前致谢..

Nir*_*mal 13

最后用以下代码行解决这个问题:

return new ModelAndView(new RedirectView(getSuccessView()));
Run Code Online (Sandbox Code Playgroud)

要么

return new ModelAndView(new RedirectView("success.htm");
Run Code Online (Sandbox Code Playgroud)

谢谢...