您的位置:首页 > 编程语言 > Java开发

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

2017-10-16 15:47 477 查看
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);" onclick="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 }" onclick="javascript:add(this,540,440);"
 plain="true">${CURRENT_USER.authUser.NAME}</a>【${CURRENT_USER.curOrgan.ORGAN_NAME}】</span> <span><a href="#" onclick="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="#" onclick="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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: