Spring MVC会话管理

问题描述

我对Spring MVC完全陌生,对于管理会话我真的很困惑。在这里,我基本上是在创建一个登录表单,它的作用是将其与数据库连接并检查用户是否存在,如果存在,则将其重定向到welcome.JSP页面。现在这是我的疑问,假设我无需登录即可直接访问欢迎页面,所以我想做的是,它应该自动重定向到登录页面。我尝试了许多方法,但结果仍然是错误的。请帮我解决这个问题。

Here is my Code,

控制器:

@RequestMapping("/")
    public String loginPage()
    {
        return "loginPage";
    }
    @RequestMapping(value="/login",method = RequestMethod.POST)
    public String loggingIn(LoginDTO login,Model model)
    {
        DatabaseConnection con=new DatabaseConnection();
        if(con.loginValidation(login))
        {
            model.addAttribute("welcome",login.getUserName());
            return "welcomePage";
        }
        return "loginPage";
    }

登录页面:

<body>
    <h1>User Login</h1>
    <div class="login">

        <form action="login" method="POST">
            <label for="userName">User Name: </label> <input type="text"
                name="userName"><br> <label for="password">Password:
            </label> <input type="password" name="password"><br>
            <p>
                <input type="submit" value="Login">
            </p>
        </form>
    </div>
    <p style="color: red;">${welcome}</p>
    <p style="color: green;">${success}</p>
    <p>
        <a href="newUser">New User?</a>
    </p>
</body>

欢迎页面:

<body>
    <h1 style="text-align: center;color:blue;">Welcome ${welcome}</h1>
</body>

当我尝试直接访问欢迎页面时,我得到了,

HTTP Status 405 – Method Not Allowed
Type Status Report
Message Request method 'GET' not supported
Description The method received in the request-line is known by the origin server but not supported by the target resource.
Apache Tomcat/9.0.37

***我想要的是,它应该自动重定向到登录页面。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)