1、页面中获取项目的路径
如项目根目录为 TestWeb, url访问路径http://10.0.220.110:20/TestWeb/index.jsp
<%= page import="java.util.*"%>
<%=
String path = request.getContextPath(); // "/TestWeb"
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getPort() + "/" + path // http://10.0.220.110:20/TestWeb
%>
或者
<script>
var _contextPath = "${pageContext.request.contextPath}"; // "/TestWeb"
</script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/index.js">
</script>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/index.css" />
注: 在jsp页面中使用java代码方式为"<%=%>"; 使用EL表达式为${};大括号中的内容自动会在page,request, session, application等对象中寻找,一般是查找request中的内容,相当于request.getAttribure("XX")
2、 页面中的meta
<meta http-equiv="X-UA-Compatible" content="IE=emulteIE7" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
3、 javascript获取当前日期时间”yyyy-MM-dd HH:MM:SS”
function getNowFormatDate() {
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
+ " " + date.getHours() + seperator2 + date.getMinutes()
+ seperator2 + date.getSeconds();
return currentdate;
}
4、javascript获取select下拉框选中的值
分别使用javascript原生的方法和jquery方法
<select id="test" name="">
<option value="1">text1</option>
<option value="2">text2</option>
</select>
code:
一:javascript原生的方法
1:拿到select对象: var myselect=document.getElementById("test");
2:拿到选中项的索引:var index=myselect.selectedIndex ; // selectedIndex代表的是你所选中项的index
3:拿到选中项options的value: myselect.options[index].value;
4:拿到选中项options的text: myselect.options[index].text;
二:jquery方法(前提是已经加载了jquery库)
1:var options=$("#test option:selected"); //获取选中的项
2:alert(options.val()); //拿到选中项的值
3:alert(options.text()); //拿到选中项的文本