【问题标题】:Spring MVC session managementSpring MVC 会话管理
【发布时间】:2020-12-16 07:59:14
【问题描述】:

我对 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

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

【问题讨论】:

    标签: java spring-mvc jsp servlets


    【解决方案1】:

    您需要在控制器中为“/”映射实现 GET。

    【讨论】:

      猜你喜欢
      • 2012-04-30
      • 1970-01-01
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多