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

时间:2009-10-29 10:32:14

标签: java java-ee spring-mvc

我使用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">

现在,我的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>

我的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;

    }

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

它运作良好&amp;如果两者都匹配,那么它重定向到layout.jsp,那也没关系。

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

请告诉我解决方法,该操作重定向到同一个视图控制器..

提前致谢..

4 个答案:

答案 0 :(得分:13)

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

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

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

...谢谢

答案 1 :(得分:2)

如果您配置了InternalResourceViewResolver,则可以这样执行:

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

在我看来,这更清楚。

答案 2 :(得分:0)

...不确定这是否是您正在寻找的,但这就是我如何解决您的问题:

    else { return new ModelAndView( "login", model ); }

...否则我错过了你的问题。在我看来,你很难被这样的卡住。

答案 3 :(得分:0)

我想说你要做的就是在再次使用它之前填充你的模型:

    if (userChecking.size() == 1) {
        return new ModelAndView("layout");
        //return new ModelAndView(new RedirectView(getSuccessView()));
    }
    model.put("User", command);
    return new ModelAndView("login", model);