Spring控制器在提交表单时重新加载同一页面

发布于 2024-12-08 15:30:15 字数 1674 浏览 0 评论 0原文

我在创建一个 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. lang.IllegalStateException:BindingResult 和 bean 名称“命令”的普通目标对象都不能作为请求属性”

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

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

I'm having a problem with creating a Spring controller that would reload the same page when a form is filled in.
What I want to achieve is a page for changing passwords. It should ask for old password, a new password and the new password re-typed. When the user fills in everything and submits the form, the action should be the same page and the controller should be able to either show something like "passwords do not match" or "password successfully changed".
I know how to achieve this by using several controllers but I think it would be better to use one controller.

Below is my controller:

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.");
        }       
    }

The first time the page loads it shows the form correctly, but if one leave the password empty and submit then glassfish shows:

"org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute"

I don't understand why this happens, I thought it should show the form like the first time accessed.

In fact what I need is to get the Password model to the viewer and also some text (error message or successfull message). I have seen people returning maps to the viewer but I haven't succeeded doing that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北陌 2024-12-15 15:30:15

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

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

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

详情请参阅这篇文章:
Spring MVC 自定义作用域 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.");
        }       
    }

You should map request as GET and POST. GET for rendering page and its messages and POST for the submit itself.

You should redirect to avoid Duplicate submit problem.

and in order to show message in model and view you should use FlashScope implementation.

See this post for details:
Spring MVC custom scope bean

You can do it this way:

@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.");
        }       
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文