SpringMVC表单提交Action的路径问题

最近在学习SprigMVC, 今天在做一个登陆Demo时遇到一个路径问题:
项目名称:SpringMVC_Test
做了一个登陆页面,代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>





Insert title here



username
password


这个login.jsp在webcontent/jsp/user下。
action中的路径,本来应该是要跳转到controller中login的方法,实现用户登录,然而我一点击提之后报错404,
路径变成了 http://localhost:8080/SpringMVC_Test/jsp/user/user/login,显然不正确,
一番尝试过后,我发现把login.jsp这个页面放在webcontent根目录下就不会有问题。
为什么?
本来应该正确跳转的路径为 http://localhost:8080/SpringMVC_Test/user/login,但此时却多了/jsp/user,这个/jsp/user显然是login.jsp在webcontent下的目录啊!
原来SpringMVC在action的跳转时,会自动加上你当前页面的根目录,结果就导致多了/jsp/user,所以这也解释了为什么我把login.jsp放在根目录下就没事!
解决办法:将action改为 action=”http://localhost:8080/SpringMVC_Test/user/login”,或者 action=”<%=basePath%>user/login”,在jsp文件的开头加上<%
String path = request.getContextPath();
String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
%>来获取绝对路径!

你可能感兴趣的:(SpringMVC)