Spring MVC中重定向的怪异行为

时间:2018-08-26 12:00:51

标签: java spring eclipse spring-mvc

我建立了一个登录索引页面,并用ModelAndView方法制作了一个控制器以映射到其他jsp。 如果登录凭据匹配并且凭据不匹配,我正在尝试在控制器中映射另一个jsp,然后我将通过消息重定向到索引页面,即抱歉,用户名或密码错误。这是代码:

def tk_start_gui():
    global root
    root = Tk()
    top = DProgram(root)
    root.mainloop()

def password(type):
    global passroot
    passroot = Tk()
    top = PassProgram(passroot, type)

class PassProgram:
    def __init__(self, top=None, passtype=None):
        bgcolor = '#d9d9d9'  # X11 color: 'gray85'

        top.geometry("300x100+480+222")
        top.title("Enter Password")
        top.configure(background=bgcolor)

        self.Frame1 = LabelFrame(top)
        self.Frame1.place(relx=0.02, rely=0.02, relheight=0.94, relwidth=0.96)
        self.Frame1.configure(text='Enter Password')
        self.Frame1.configure(relief=GROOVE)
        self.Frame1.configure(borderwidth="2")
        self.Frame1.configure(relief=GROOVE)
        self.Frame1.configure(background="#d9d9d9")
        self.Frame1.configure(width=575)

        self.Label1 = Label(self.Frame1)
        self.Label1.place(relx=0.01, rely=0.01)
        self.Label1.configure(background="#d9d9d9")
        self.Label1.configure(disabledforeground="#a3a3a3")
        self.Label1.configure(foreground="#000000")
        self.Label1.configure(text='''Enter password to access {} settings'''.format(passtype))

        self.Entry1 = Entry(self.Frame1)
        self.Entry1.place(relx=0.05, rely=0.1)
        self.Entry1.configure(width=46)
        self.Entry1.configure(show="*")
        self.Entry1.bind("<Return>", self.on_change)
        self.Entry1.pack(side=LEFT)

    def on_change(self, *args):
        pass = self.Entry1.get()
        print("function passed")
        self.Entry1.delete(0, 'end')
tk_start_gui()

此代码已成功重定向到索引页面,但消息却像这样添加到了URL:

return new ModelAndView("Redirect:/index.jsp","mess","Sorry, username or password error.");

喜欢这个。

我的问题是造成此问题的原因是什么。为什么邮件会附加在网址中? 任何建议或意见表示赞赏。 预先谢谢你。

1 个答案:

答案 0 :(得分:1)

如果不想附加到URL,请使用RedirectView(而不是ModelAndView)并将setExposeModelAttributes设置为false。

public class RedirectView extends AbstractUrlBasedView implements SmartView

from spring documentation: View that redirects to an absolute, context relative, or current request relative URL. The URL may be a URI template in which case the URI template variables will be replaced with values available in the model. By default all primitive model attributes (or collections thereof) are exposed as HTTP query parameters (assuming they've not been used as URI template variables), but this behavior can be changed by overriding the isEligibleProperty(String, Object) method.