繁体   English   中英

Spring MVC中ModelAndView中的重定向问题

[英]Issue with redirect in ModelAndView in Spring MVC

我有一个简单的索引页和控制器。 如果用户的凭据不匹配,我只希望控制器重定向到索引页面。 我在控制器中使用ModelAndView作为我的返回类型。

这是我的控制器代码:

public class LoginController {  

    @RequestMapping(value="/login",method = RequestMethod.POST)  
    public ModelAndView loginResult(HttpServletRequest req,HttpServletResponse res,RedirectAttributes redir,Model model) {
InfoEmployee inf = new InfoEmployee();
        InfoManager inf2 = new InfoManager();
        String uname=req.getParameter("username");  
        //Putting the username in the session object


        String pwd=req.getParameter("pwd");  
        String dept = req.getParameter("dept");
        String name1 = inf.getName();
        String message1 = "Welcome "+name1;  
        String name2 = inf2.getName();
        String message2 = "Welcome "+name2;
        model.addAttribute("message", "sorry message");
        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");
            //return modelAndView;
            //redir.addFlashAttribute(message1, "Sorry");
            return new ModelAndView("redirect:/index.jsp","message","Sorry username password error");

            //return new ModelAndView("Redirect:/index","message","Sorry, user name or password error");
            //return new ModelAndView(new RedirectView("index.jsp"),"message","Sorry, user name or password error");
            //return new ModelAndView("RedirectToIndex", "message","Sorry, user name or password error"); 
        }
    }     

我尝试使用Flash属性,但URL仍会附加传递的属性。 这是我的索引页面:

<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">
            <%
                String message=request.getParameter("message"); 
            %>
            <%=message %>
            <span>${message}</span>
            <span>${message1}</span>
            </div>
        </form>
    </div> 

我似乎无法在索引页中打印通过$ {message}通过重定向发送的消息。 每个方法都会在URL后面附加消息。 我想知道是否可以使用其他方法解决此问题,或者是否有人可以像我使用RedirectAttributes一样告诉我使用RedirectAttributes的原因,但它仍向URL添加了属性。

简而言之,我只想显示用户输入的用户名或密码在我的索引页面中是错误的。 如果我可以在没有重定向帮助的情况下转到索引页面,则该建议也将受到赞赏。

任何建议或帮助表示赞赏。 谢谢你也一样

编辑:

这是我的调度程序servlet

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-3.0.xsd">  

    <mvc:annotation-driven></mvc:annotation-driven>
<!--    <mvc:annotation-driven ignore-default-model-on-redirect="true" /> -->
<!--<context:annotation-config></context:annotation-config> -->
    <context:component-scan base-package="com.controller"></context:component-scan> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
      <mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/> 
<!--      <mvc:resources location="webapp/WEB-INF/jsp/stylesheets/main.css" mapping="/stylesheets/**"></mvc:resources>  -->
</beans>    

目录:

在此处输入图片说明

我还没有尝试过,但是可以从当前会话中存储的flashmap访问flashattributes。 请也尝试一下,看看是否有附加内容。

链接: https//docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-flash-attributes

index.jsp jsp文件夹中,然后像成功场景一样创建ModelAndView的对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM