简体   繁体   English

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

[英]Return same view controller using ModelAndView of Spring Web MVC

I am using Spring Web MVC and Hibernate for developing my application. 我正在使用Spring Web MVC和Hibernate开发我的应用程序。

My login.jsp page has following code : 我的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">

Now, My servlet.xml file has following code : 现在,我的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>

My UserValidateFormController has following code : 我的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;

    }

In my UserValidateFormController at the time of handing submit event, i am checking that username and password are correct or not.. 在处理提交事件时,在我的UserValidateFormController中,我正在检查用户名和密码是否正确。

It's working fine & if both are matching then its redirecting to layout.jsp, that's also fine. 它工作正常,并且如果两者都匹配,则将其重定向到layout.jsp,这也很好。

But if username or password are incorrect then i want to redirect to same login.jsp page and display appropriate error.. 但是,如果用户名或密码不正确,那么我想重定向到相同的login.jsp页面并显示适当的错误。

Please suggest me the solution that what to do redirecting to same view controller.. 请向我建议将重定向到同一视图控制器的解决方案。

Thanks in advance.. 提前致谢..

Finally solve this issue with following line of code : 最后,使用以下代码行解决此问题:

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

or 要么

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

Thanks... 谢谢...

If you have an InternalResourceViewResolver configured, you can do it like this: 如果您配置了InternalResourceViewResolver ,则可以这样进行:

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

In my opinion, this is clearer. 我认为这更清楚。

... not sure if this is what you're looking for, but is this how I solve your problem: ...不确定这是否是您要寻找的东西,但这是我如何解决您的问题:

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

... otherwise I missed something in your question. ...否则我错过了您的问题。 It seems to me you're pretty far to get stuck like this. 在我看来,您像这样陷入困境已经相当遥远了。

I would say all you have to do is populate your model before using it again: 我想说的是,您需要做的就是在再次使用模型之前填充模型:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Spring MVC框架中使用ModelAndview重定向到同一视图 - How to redirect to the same view using ModelAndview in Spring MVC framework Java Spring MVC Controller不重定向modelandview - Java Spring MVC Controller is not redirecting modelandview Spring MVC控制器中的ModelAndView和byte [] - ModelAndView and byte[] within Spring MVC controller spring:从控制器返回JSON作为ModelAndVIew - spring: return JSON from controller as ModelAndVIew 使用@Responsebody返回列表或从spring控制器返回ModelAndView哪个更好 - Returning list using @Responsebody or return ModelAndView from spring controller which one better 当您使用Spring Forward重定向到其他控制器时,如何从ModelAndView返回响应对象? - How do you return the response object from ModelAndView when you are using spring forward to redirect to a different controller? 当使用.jsp的view spring mvc并传递ModelAndView时,如何输出模型? - When using .jsp's view spring mvc, and passing ModelAndView, how to output model? 如何在Spring中在HttpEntity和ModelAndView之间交替Web控制器响应 - How to alternate the web controller response between HttpEntity and ModelAndView in Spring 我可以在Spring MVC中的ModelAndView中返回两个模型 - Can i return two models in ModelAndView in Spring MVC 返回ModelAndView Spring MVC PDF对象作为可保存的附件 - Return a ModelAndView Spring MVC PDF Object as a saveable attachment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM