Spring mvc怎么获取当前应用的url地址?即jsp页面中的${contextpath}怎么得到?

action类:

public class IndexController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
 Map<String ,Object> model=new HashMap<String,Object>();
  String contextpath;
 contextpath = request.getScheme() +"://" + request.getServerName()  + ":" +request.getServerPort() +request.getContextPath();
 model.put("contextpath",contextpath);
 return new ModelAndView("index",model);
}

如果调用IndexController 的http url是http://127.0.0.1:8080/test/index.do,以上request对象的各方法得到的结果分别是:

request.getScheme() :http

request.getServerName()  :127.0.0.1

request.getServerPort() :8080

request.getContextPath():/test

分别request还有一个有用的方法,request.getServletPath,获取的结果是:index.do,一般用不到。

jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>   xxxxxxxxx </title>
<script src="${contextpath}/ui/boot.js" type="text/javascript"></script>
<script src="${contextpath}/js/index.js" type="text/javascript"></script>
<link href="${contextpath}/css/index.css" rel="stylesheet" type="text/css" />
<link href="${contextpath}/css/icons.css" rel="stylesheet" type="text/css" />
</head>
<body>
<input type="hidden" class="mini-hidden"  required="false"  vtype="" value="${contextpath}" id="context" emptyText="">
<div id="layout1" class="mini-layout" style="width: 100%; height: 100%">
<div class="header" region="north" height="100" showSplit="false" showHeader="false">
<div id="header">
<div class="headerNav"><a class="logo" href="javascript:void(0);" οnclick="javascript:selectMainTab();"></a>
<div id="top_right"></div>
</div>
</div>
<div id="navMenu"><span id="nowDate" style="text-align: left"></span> <span style="text-align: left">欢迎您:<a class="mini-button" grid="datagrid1" realhref="${contextpath}/authuser/modifyPass/${CURRENT_USER.authUser.ID }" οnclick="javascript:add(this,540,440);"  plain="true">${CURRENT_USER.authUser.NAME}</a>【${CURRENT_USER.curOrgan.ORGAN_NAME}】</span> <span><a href="#" οnclick="logout(this);" realhref="${contextpath}/login/out">退出</a><input type="hidden"
name="backUrl" class="mini-hidden" value="${contextpath}/login/out" id="backUrl"></span><span><a href="#" οnclick="javascript:closeSystem();">关闭</a> </span></div>
</div>
<div title="center" region="center" style="border: 0;" bodyStyle="overflow:hidden;"><!--Splitter-->
<div class="mini-splitter" style="width: 100%; height: 100%;" borderStyle="border:0;">
<div size="210" maxSize="250" minSize="100" showCollapseButton="true" style="border: 0;"><!--OutlookTree-->
<div id="leftTree" class="mini-outlooktree" url="${contextpath}/authmenu/jsonUserMenu" onnodeclick="onNodeSelect" textField="text" idField="id" parentField="pid"></div>
</div>
<div showCollapseButton="false" style="border: 0;"><!--Tabs-->
<div id="lr_systembox" style="margin-right: 100px;margin-top: 0px;">
</div>
<div id="mainTabs" class="mini-tabs bg-toolbar" activeIndex="0" style="width: 100%; height: 100%;">
<div id="maintreeid" title="首页" name="homePage" iconCls="a38" url="${contextpath}/WEB-INF/jsp/main.jsp"></div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

jsp页面中的${contextpath}显示的值为http://127.0.0.1:8080/test

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值