【问题标题】:Return same view controller using ModelAndView of Spring Web MVC使用 Spring Web MVC 的 ModelAndView 返回相同的视图控制器
【发布时间】:2010-12-11 04:43:26
【问题描述】:

我正在使用 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 中,我正在检查用户名和密码是否正确..

它工作正常,如果两者都匹配,那么它会重定向到 layout.jsp,这也很好。

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

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

提前谢谢..

【问题讨论】:

  • ...如果您不介意询问,如果您在使用 Spring 时遇到了麻烦,是否有特殊原因为什么您要推出自己的安全措施而不是实施 Acegi(或现在称为 Sprint 安全性)?
  • 是的,我使用过 ACEGI 并且非常了解它......但是,我目前的系统不需要那么多负载......这个问题只是基本的 spring 概念,如何我重定向到同一个名为 JSP 页面的控制器..
  • @vector:Spring Security 有点像怪物,对于简单的任务来说,它往往比它的价值更麻烦
  • 每次有人使用SimpleFormController,小宝贝耶稣都会哭……
  • 你解决了吗?如果是,请发布解决方案...

标签: java jakarta-ee spring-mvc


【解决方案1】:

...不确定这是否是您要查找的内容,但这就是我解决您问题的方法:

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

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

【讨论】:

  • 我不能这样做,因为我的登录页面需要初始化用户...所以,它会遇到麻烦,并给我一个错误,即从 login.jsp 中找不到用户命令..
  • ... 那么呢:return new ModelAndView(new RedirectView (getFormView()), model);
【解决方案2】:

如果你配置了InternalResourceViewResolver,你可以这样做:

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

在我看来,这更清楚。

【讨论】:

    【解决方案3】:

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

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

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

    谢谢...

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-16
        • 2011-05-24
        • 1970-01-01
        • 2021-04-05
        • 1970-01-01
        • 2013-11-10
        相关资源
        最近更新 更多