SpringMVC表单提交Action的路径问题

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

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

<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="<%=basePath%>user/login" method="POST">
<table>
	<tr>
		<td>username</td><td><input type="text" name="username"/></td>
	</tr>
	<tr>
		<td>password</td><td><input type="password" name="password"/></td>
	</tr>
	<tr>
		<td><input type="submit" value="提交"/></td>
	</tr>
</table>
</form>

</body>
</html>


这个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+”/”;
%>来获取绝对路径!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值