JSP里的basePath

参考文章:
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值