如何解决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
***我想要的是,它应该自动重定向到登录页面。
解决方法
您需要在控制器中为“ /”映射实现GET。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。