MVC中POST方法后重定向

MVC中POST方法后重定向

本文介绍了Spring MVC中POST方法后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我想重定向到我的索引页而不必看到附加在 URL 中的属性.

我发现解决方案是 addFlashAttributes,它不会在 URL 中附加属性,但我看不到此方法保存的任何消息.

代码:

控制器

@RequestMapping(value="/login",method = RequestMethod.POST)公共 ModelAndView loginResult(HttpServletRequest req,HttpServletResponse res,RedirectAttributes redir) {if (uname.equals(inf.getUsername()) & &pwd.equals(inf.getPassword()) && dept.equals(inf.getDept())){req.getSession().setAttribute("uname",inf.getName());return new ModelAndView("employeeLoginResult", "message", message1);}else if (uname.equals(inf2.getUsername()) &&pwd.equals(inf2.getPassword()) &&dept.equals(inf2.getDept())){req.getSession().setAttribute("uname", inf2.getName());return new ModelAndView("adminLoginResult", "message", message2);}别的{ModelAndView modelAndView = new ModelAndView();modelAndView.setViewName("重定向:/index.jsp");redir.addFlashAttribute("message","抱歉用户名密码错误");返回模型和视图;}}

现在我有用于验证的硬编码值,并且将来会将验证与数据库的 DAO 层集成.addFlashAttribute 为我完成了不将消息附加到 URL 的工作,但我也想在索引页面上显示消息.

索引页

index<身体><b><span class="heading">登录用户</span></b><div class="容器"><form action="login.html" method="Post"><div class="form_style"><input type="text" name="username" placeholder="输入用户名"/><input type="password" name="pwd" placeholder="输入密码"/><option>IT</option><option>Admin</option><option>HR</option><option>市场营销</option></选择><输入类型=提交"值=提交"><p>${message}</p>

</表单>

我做错了什么?谁能解释一下?

为什么 ${message} 不显示属性?

感谢任何帮助.同样感谢.

解决方案

你可以像下面一样改变你的控制器并使用相同的 index.jsp 文件.

控制器

@RequestMapping(value="/login",method = RequestMethod.POST)public ModelAndView loginResult(HttpServletRequest req,HttpServletResponse res, Model model){字符串消息 = "";字符串响应 = "";if(uname.equals(inf.getUsername())&&pwd.equals(inf.getPassword())&&dept.equals(inf.getDept())) {req.getSession().setAttribute("uname",inf.getName());消息 = 消息 1;response = "employeeLoginResult";}else if(uname.equals(inf2.getUsername())&&pwd.equals(inf2.getPassword())&&dept.equals(inf2.getDept())) {req.getSession().setAttribute("uname",inf2.getName());消息 = 消息 2;response = "adminLoginResult";}别的{message = "抱歉用户名密码错误";响应=索引";}model.addAttribute("message", message);返回响应;}

My problem is that I want to redirect to my index page without having to see the attributes appended in the URL.

I found the solution to be addFlashAttributes, it doesn't append attributes in the URL but I can't see any message saved by this method.

Code:

Controller

@RequestMapping(value="/login",method = RequestMethod.POST)
public ModelAndView loginResult(HttpServletRequest req,
                                HttpServletResponse res,
                                RedirectAttributes redir) {
    if (uname.equals(inf.getUsername()) & &pwd.equals(inf.getPassword()) && dept.equals(inf.getDept()))
    {
        req.getSession().setAttribute("uname",inf.getName());
        return new ModelAndView("employeeLoginResult", "message", message1);
    }
    else if (uname.equals(inf2.getUsername()) && pwd.equals(inf2.getPassword()) && dept.equals(inf2.getDept()))
    {
        req.getSession().setAttribute("uname", inf2.getName());
        return new ModelAndView("adminLoginResult", "message", message2);
    }
    else
    {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/index.jsp");
        redir.addFlashAttribute("message","Sorry Username Password Error");
        return modelAndView;
    }
}

Right now I have hardcoded values for validation and would integrate the validation with DAO layer of database in the future. The addFlashAttribute does the work for me of not appending the message to the URL, but I also want to display the message on the index page.

Index page

<head><tile>index</title>
</head>
<body>
    <b><span class="heading">LOGIN USER</span></b>
    <div class="container">
        <form action="login.html" method="Post">
            <div class="form_style">
            <input type="text" name="username" placeholder="Enter Username"/>
            <input type="password" name="pwd" placeholder="Enter password"/>
            <select name="dept">
                <option>IT</option>
                <option>Admin</option>
                <option>HR</option>
                <option>Marketing</option>
            </select>
            <input type="Submit" value="submit">
            <p>${message}</p>
            </div>
        </form>
    </div>
</body>

What am I doing wrong? Can anyone explain?

Why is ${message} not displaying the attribute?

Any help is appreciated. Thanks for the same.

解决方案

You can change your Controller as below and use same index.jsp file.

Controller

@RequestMapping(value="/login",method = RequestMethod.POST)
 public ModelAndView loginResult(HttpServletRequest req,HttpServletResponse res, Model model){

    String message = "";
    String response = "";

     if(uname.equals(inf.getUsername())&&pwd.equals(inf.getPassword())&&dept.equals(inf.getDept())) {
                    req.getSession().setAttribute("uname",inf.getName());
        message = message1;
        response = "employeeLoginResult";
            }
         else if(uname.equals(inf2.getUsername())&&pwd.equals(inf2.getPassword())&&dept.equals(inf2.getDept())) {
                 req.getSession().setAttribute("uname",inf2.getName());
        message = message2;
        response = "adminLoginResult";
            }
            else{
        message = "Sorry Username Password Error";
        response = "index";
            }
    model.addAttribute("message", message);
    return response;
}

这篇关于Spring MVC中POST方法后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 06:21