通过jsp提交按钮寻址jsp会更改请求URL



我正在开发一个Spring Mvc应用程序,在那里我正在制作一个基本的登录应用程序,当我将其发送到控制器时,它会很好,并在登录失败时显示错误页面。现在,当我点击relogin按钮(一个提交按钮)时,我必须将路径指定为/jsps/Logn.jsp,因为我的jsp在jsps文件夹中再次提交请求,它会创建一个url,名称为/jsp/login.do,但这无法到达到我的控制器,因为它被映射到/Login.do,这是第一次

谁能帮我拿这个吗?

这是我的登录页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form action="Login.do">
        Enter Login Details <br> 
        UserName::  <input type="text" name = "username"><br>
        Password::  <input type="password" name = "password"><br>
        <input type= "submit" value = "Login">
    </form>
</body>
</html>

这是我的错误页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Error While logging in please try again <br>`enter code here`
<form action = "jsps/Login.jsp">
<input type = "submit" value  = "try again">
</form>
</html>

这是我的控制器代码

package controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/Login.do")
public class LoginController {

    @RequestMapping("/Login.do")
    public ModelAndView checkLoginDetails(HttpServletRequest request , HttpServletResponse response){

        if(request.getParameter("username") != null && request.getParameter("password") != null){
        if(request.getParameter("username").equalsIgnoreCase("admin") && request.getParameter("password").equalsIgnoreCase("admin")){
            return new ModelAndView("Home" );
        }else{
            return new ModelAndView("Error" );
        }
        }else{
            return new ModelAndView("Error" );
        }
    }
}

显示的URL为

当我第一次点击登录按钮时,url是

localhost/SpringLogiExample/Login.do?username=&密码=

当我在错误页面上点击重试按钮时,它会将我带回到登录页面,然后如果我点击登录按钮,生成的url是

localhost/SpringLogi例如/jsps/Login.do?username=&密码=

如果您的jsp位于war/jsps 中,那么您的XML配置应该是这样的

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/jsps/" />
    <property name="suffix" value=".jsp" />
</bean>

您应该避免在控制器中使用双重注释。类级别有一个全局注释,所有@RequestMapping方法都映射到该注释,但您也使用另一个方法级别的映射,这是多余的,因为它用于相同的url。我还将通过两个不同的url(如下所示)或使用GET请求显示页面和POST检查凭据来区分登录检查和纯加载登录页面。

@Controller
public class LoginController {
    @RequestMapping("/Login")
    public String loginPage(){
        return "Login"; // display a page that is named Login.jsp in your jsps folder
    }
    @RequestMapping("/Check")
    public ModelAndView checkLoginDetails(HttpServletRequest request , HttpServletResponse response){
    ...
    }
}

现在,您可以使用<form action = "/Check"> ...向控制器发送验证请求。不仅从您的登录页面,还从其他页面。不过:在登录失败后,您重新登录的方式并不是为用户提供另一种登录可能性的常用方式。

我推荐4种可能性:

  1. 只需提供一个返回登录页面<a href="/Login"> try again </a>的纯html链接
  2. 同时在错误页面中提供用户名和密码字段,并使用与原始登录页面<form action = "/Check">中相同的"操作"
  3. 纯Spring MVC的最佳解决方案是查看Spring提供的表单验证,这将允许您检查表单属性并返回到之前的页面,并为每个字段(此处为用户名或密码)提供自定义错误消息
  4. 在登录的情况下,你不必重新发明轮子。看看最适合为Spring web应用程序添加安全性/登录的Spring Security

您应该首先查看xml配置。检查其中是否有以下代码。

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/jsps/" />
    <property name="suffix" value=".jsp" />
</bean>

如果仍然不起作用,请告诉我,我会给出不同的解决方案来处理这个问题。

最新更新