【问题标题】:Spring controller to reload same page when form is submitted提交表单时,Spring控制器重新加载同一页面
【发布时间】:2011-10-05 08:15:05
【问题描述】:

我在创建一个在填写表单时会重新加载同一页面的 Spring 控制器时遇到问题。 我想要实现的是一个更改密码的页面。它应该要求输入旧密码、新密码和重新输入的新密码。当用户填写所有内容并提交表单时,操作应该是同一页面,并且控制器应该能够显示“密码不匹配”或“密码成功更改”之类的内容。 我知道如何通过使用多个控制器来实现这一点,但我认为使用一个控制器会更好。

下面是我的控制器:

package controllers;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import models.Password;



@Controller
public class ChangePassword {
    @RequestMapping(value = "/changepassword", method = RequestMethod.GET)
    public ModelAndView changePassword(@ModelAttribute("changepassword")Password password, BindingResult result) {
        if(password.isEmpty())
        {
            return new ModelAndView("changepassword", "command", new Password());
        }
        else if(password.isValid())
        {
            return new ModelAndView("changepassword", "message", "The password was successfully changed!");
        }
        else{
            return new ModelAndView("changepassword", "message", "The passwords did not match and/or the password was not 8 at least 8 characters long.");
        }       
    }

页面第一次加载时会正确显示表单,但如果将密码留空并提交,则 glassfish 会显示:

“org.springframework.web.util.NestedServletException: 请求处理失败;嵌套异常是 java.lang.IllegalStateException: Bean name 'command' 的 BindingResult 和普通目标对象都不能用作请求属性”

我不明白为什么会发生这种情况,我认为它应该像第一次访问一样显示表单。

事实上,我需要的是向查看器提供密码模型以及一些文本(错误消息或成功消息)。我见过有人将地图返回给查看器,但我没有成功。

【问题讨论】:

    标签: spring jsp


    【解决方案1】:

    您应该将请求映射为 GET 和 POST。 GET 用于呈现页面及其消息,POST 用于提交本身。

    您应该重定向以避免重复提交问题。

    为了在模型和视图中显示消息,您应该使用 FlashScope 实现。

    详情请看这篇文章: Spring MVC custom scope bean

    你可以这样做:

    @Controller
    public class ChangePassword {
        @RequestMapping(value = "/changepassword", method = RequestMethod.GET)
     public ModelAndView renderCPassPage(@ModelAttribute("changepassword")Password password, BindingResult result) {
           ModelAndView mv = new ModelAndView("changePassword");
           mv.addObject("password" password);
           return mv;
        }
    
       @RequestMapping(value = "/changepassword", method = RequestMethod.POST)
     public ModelAndView renderCPassPage(@ModelAttribute("changepassword")Password password, BindingResult result) {
           ModelAndView mv = new ModelAndView("redirect:changepassword");
              if(password.isEmpty())
            {
                return new ModelAndView("message", "password Empty");
            }
            else if(password.isValid())
            {
                return new ModelAndView("message", "The password was successfully changed!");
            }
            else{
                return new ModelAndView("message", "The passwords did not match and/or the password was not 8 at least 8 characters long.");
            }       
        }
    

    【讨论】:

      猜你喜欢
      • 2019-11-26
      • 1970-01-01
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      • 2020-01-27
      • 1970-01-01
      相关资源
      最近更新 更多