本文目錄一覽:
- 1、Spring mvc怎麼獲取當前應用的url地址
- 2、在jsp中如何取得當前頁面完整的URL(帶參數)?
- 3、如何用js獲取當前打開的頁面的路徑
- 4、如何得到一個jsp頁面所在的項目的路徑
- 5、JSP獲取頁面的元素的值有幾種方式
- 6、web項目中,request的方法詳解
Spring mvc怎麼獲取當前應用的url地址
action類:
public class IndexController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
MapString ,Object model=new HashMapString,Object();
String contextpath;
contextpath = request.getScheme() +”://” + request.getServerName() + “:” +request.getServerPort() +request.getContextPath();
model.put(“contextpath”,contextpath);
return new ModelAndView(“index”,model);
}
如果調用IndexController 的http url是,以上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” “”
html xmlns=””
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
spana href=”#” onclick=”logout(this);”
realhref=”${contextpath}/login/out”退出/ainput
type=”hidden”
name=”backUrl” class=”mini-hidden”
value=”${contextpath}/login/out”
id=”backUrl”/spanspana 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}顯示的值為
在jsp中如何取得當前頁面完整的URL(帶參數)?
JSP頁面
% String url = request.getScheme()+”://”+ request.getServerName()+request.getRequestURI()+”?”+request.getQueryString(); %
用JS可以的
window.location.href
如何用js獲取當前打開的頁面的路徑
lz
(1)window.location.href : 整個URl字符串(在瀏覽器中就是完整的地址欄)返回值: ;name=java#imhere(2)window.location.protocol :URL 的協議部分返回值:http:(3)window.location.host : URL 的主機部分返回值: : URL 的端口部分(如果採用默認的80端口(update:即使添加了:80),那麼返回值並不是默認的80而是空字符)返回值:””(5)window.location.pathname : URL 的路徑部分(就是文件地址)返回值:/order/index.html(6)window.location.search : 查詢(參數)部分 (除了給動態語言賦值以外,我們同樣可以給靜態頁面,並使用javascript來獲得相信應的參數值)返回值:orderid=1name=java(7)window.location.hash : 錨點返回值:#imhere(8)document.URL返回值: ;name=java#imhere//獲取Url傳過來的值
function Request(name)
{
new RegExp(“(^|)”+name+”=([^]*)”).exec(window.location.search.substr(1));
return RegExp.$2
}
如何得到一個jsp頁面所在的項目的路徑
在jsp和class文件中調用的相對路徑不同。 在jsp里,根目錄是WebRoot 在class文件中,根目錄是WebRoot/WEB-INF/classes 當然你也可以用System.getProperty(“user.dir”)獲取你工程的絕對路徑。
另:在Jsp,Servlet,Java中詳細獲得路徑的方法。
1.jsp中取得路徑:
以工程名為TEST為例:
(1)得到包含工程名的當前頁面全路徑:request.getRequestURI()
結果:/TEST/test.jsp
(2)得到工程名:request.getContextPath()
結果:/TEST
(3)得到當前頁面所在目錄下全名稱:request.getServletPath()
結果:如果頁面在jsp目錄下 /TEST/jsp/test.jsp
(4)得到頁面所在服務器的全路徑:application.getRealPath(“頁面.jsp”)
結果:D:\resin\webapps\TEST\test.jsp
(5)得到頁面所在服務器的絕對路徑:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
結果:D:\resin\webapps\TEST
2.在類中取得路徑:
(1)類的絕對路徑:Class.class.getClass().getResource(“/”).getPath()
結果:/D:/TEST/WebRoot/WEB-INF/classes/pack/
(2)得到工程的路徑:System.getProperty(“user.dir”)
結果:D:\TEST
3.在Servlet中取得路徑:
(1)得到工程目錄:request.getSession().getServletContext().getRealPath(“”) 參數可具體到包名。
結果:E:\Tomcat\webapps\TEST
(2)得到IE地址欄地址:request.getRequestURL()
結果:
(3)得到相對地址:request.getRequestURI()
結果:/TEST/test
JSP獲取頁面的元素的值有幾種方式
javascript中獲取jsp界面元素方法
1.getElementsByName(“name”)
可以獲取jsp界面中元素名字為”name”的所有元素,返回的是一個object[ ] 數組,數組中每個元素包含一個從jsp界面中獲得的元素。
2.getElementById(id)
根據id值來獲取jsp界面中惟一的對象。
servlet中獲取jsp界面元素方法
3.getParameter(“name”) 返回類型為String
用於獲取jsp界面中一個名字為”name”的對象的value值。
4.getParameterValues(“name”)返回類型為String[]
用於獲取jsp界面中多個名字為”name”的對象的value值。
web項目中,request的方法詳解
以點到面,知道了web中getContextPath()這種獲取路徑的方式,顯然其他的方式的是可以以此類推的。常說,工作學習找共同點嘛。 上一段我們也提高getContextPath()的含義,是jsp中獲取路徑的一種方式,返回當前頁面所在的應用的名字。知其然知其所以然,知道它大體上的含義,看看在項目中是如何使用的
String path = request.getContextPath();
String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;”
在程序中我們一般這樣使用。說到了這裡,我們可以看看request常用的方法:
request.getSchema(),返回的是當前連接使用的協議,一般應用返回的是http、SSL返回的是https;
request.getServerName(),返回當前頁面所在的服務器的名字;
request.getServerPort(),返回當前頁面所在的服務器使用的端口,80;
request.getContextPath(),返回當前頁面所在的應用的名字。
getContextPath()是jsp中獲取路徑的一種方式,那麼獲取到結果是什麼呢?jsp其他獲取路徑的方式是怎樣的呢?表示好奇,請聽分解:
以訪問的jsp為:,工程名為/dmsd-itoo-exam-log-web為例:
request.getContextPath(),得到工程名:/dmsd-itoo-exam-log-web;
request.getServletPath(),返回當前頁面所在目錄下全名稱:/course/index.jsp;
request.getRequestURL(),返回IE地址欄地址:;
request.getRequestURI() ,返回包含工程名的當前頁面全路徑:/dmsd-itoo-exam-log-web/course/index.jsp。
對jsp獲取當前應用的路徑方法和request常用方法有了基本的了解,下面後台通過request.getScheme()獲取當前頁面使用協議遇到的問題:request.getScheme()獲取到的確實http而不是https,與request.getRequestURL()拿到的一樣也是http。
這是為什麼呢?
原來,nginx+tomcat部署web服務,tomcat接收到的請求是來自nginx的http請求。我想這也是上一博文中說到項目用https啟動請求不到頁面的原因吧。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/187525.html