SpringMVC中Redirect或RedirectModel问题的解决方案

我目前已经为我的索引页面构建了一个控制器,它也是一个登录页面。


使用控制器,如果用户的登录凭据匹配,我将映射 jsp,如果不匹配,我想重定向到带有消息“抱歉用户名,密码错误”的索引页面。


这是代码:


控制器


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

        {


            return new ModelAndView("redirect:/index.jsp","message","Sorry");

        }

索引页


<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">

            <span class='disp'>${message}</span>

            </div>

        </form>

    </div>

重定向有效,但消息会像这样附加在 Url 中:

http://img.mukewang.com/61162f1b000141e516640780.jpg

我只想用 ${message} 在索引文件上打印“对不起用户名密码错误”

我的问题:是否可以将 ModelAndView 与 RedirectView 一起使用,或者如果没有其他解决方案可以使用 ModelAndView 将重定向发送到我的索引页?

任何建议或意见表示赞赏。感谢同样的。


温温酱
浏览 262回答 2
2回答

呼啦一阵风

你能添加一个像 MyConfig 这样的类吗@EnableWebMvc@Configurationpublic class MyConfig {&nbsp; &nbsp;@Autowired&nbsp; &nbsp;private RequestMappingHandlerAdapter requestMappingHandlerAdapter;&nbsp; &nbsp;@PostConstruct&nbsp; &nbsp;public void init() {&nbsp; &nbsp; &nbsp;requestMappingHandlerAdapter.setIgnoreDefaultModelOnRedirect(true);}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java