您的位置:首页 > Web前端 > JavaScript

JSP里的basePath

2017-05-23 15:36 176 查看
参考文章:

jsp中的basePath和path (绝对路径 相对路径)

basePath的作用

当前所在的路径:

http://localhost:8000/forum/forum/userLockManagePage.html


下面是所要请求的代码。

@RequestMapping(value = "/forum/userLockManage", method = RequestMethod.POST)
public ModelAndView userLockManage(@RequestParam("userName") String userName ,@RequestParam("locked") String locked) {
ModelAndView view =new ModelAndView();
User user = userService.getUserByUserName(userName);
if (user == null) {
view.addObject("errorMsg", "用户名(" + userName + ")不存在");
view.setViewName("/fail");
} else {
user.setLocked(Integer.parseInt(locked));
userService.update(user);
view.setViewName("/success");
}
return view;
}


用下面的相对路径:

<body>
<%@ include file="includeTop.jsp" %>
//当前路径:http://localhost:8000/forum/forum/userLockManagePage.html
//下面的两种是等效果的,都是相对于当前路径去请求:http://localhost:8000/forum/forum/userLockManage.html
<!-- <form action="userLockManage.html" method="post" > -->
<form action="<c:url value="userLockManage.html" />" method="post" >

<table border="1px" width="100%">
<tr>
<td width="20%">用户</td>
<td width="80%"><select name="userName">

<option>请选择</option>
<c:forEach var="user" items="${users}">
<option value="${user.userName}">${user.userName}</option>
</c:forEach>
</select></td>
</tr>
<tr>
<td width="20%">锁定/解锁</td>
<td width="80%">
<input type="radio" name="locked" value="1" />锁定
<input type="radio" name="locked" value="0" />解锁
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="保存">
<input type="reset" value="重置">
</td>
</tr>
</table>

</form>
</body>


相比上面多加一个斜杠‘/’但是下面的两种效果就不同了。

<form action="/userLockManage.html" method="post" >
<form action="<c:url value="/userLockManage.html" />" method="post" >


效果是:

<form action="/userLockManage.html" method="post" >
<form action="<c:url value="/forum/userLockManage.html" />" method="post" >


绝对路径:

<%
String path = request.getContextPath();
String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
// http://localhost:8000/forum/ %>


在head标签中加入

<base href="<%=basePath %>" />


下面就可以用绝对路径了。

<form action="<c:url value="/forum/userLockManage.html" />" method="post" >
</form>
请求:http://localhost:8000/forum/forum/userLockManage.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jsp