内部js代码获取后台变量

java web中,内部js代码如何获取服务器后台的变量呢?

根据jsp页面使用的标签不同有一下几种情况:

(1)页面使用EL表达式,使用spring MVC

内部js代码:

alert("222:${sessionScope.practiceWay}");
var practiceWay=String("${sessionScope.practiceWay}");

 

(2)页面使用struts标签

var length=Number("<s:property value="list.size"  />");
  alert("length:"+length);
var isEdit = "<s:property value="isEdit"  />";
		var id = "<s:property value="goods.id"  />";
var supermarkets_size = Number("<s:property value="supermarkets.size"  />");

 

(3)页面使用jstl标签:

var sum_question=Number("${fn:length(studentPages.list)}");

 

错误的做法

 

var length=Number("<s:property value=\"list.size\"  />");

 

js外部文件中写<%=com.dict.Constant2.COOKIE_KEY_PASSWORD %> 是不对

$(function(){
	var $username=$("#username");
	var $password=$("#password");
	var $issavePasswd=$("input[name=issavePasswd][type=checkbox]");
	
	var usernameVal=null;
	var passwordVal=null;
	usernameVal=com.whuang.hsj.getCookie("<%=com.dict.Constant2.COOKIE_KEY_USERNAME %>");
	passwordVal=com.whuang.hsj.getCookie("<%=com.dict.Constant2.COOKIE_KEY_PASSWORD %>");
	if(com.whuang.hsj.isHasValue(usernameVal)){
		$username.val(usernameVal);
	}
	if(com.whuang.hsj.isHasValue(passwordVal)){
		$password.val(passwordVal);
		com.whuang.hsj.setSelectedCheckbox($issavePasswd.get(0));
	}
});

 

 

总结:在js 代码中可以直接写JSP代码,上述代码中,不要对引号转义。

 

示例:

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!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>
<base href="<%=basePath%>">
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Insert title here</title> <script type="text/javascript"
		src="<%=path%>/static/js/Module.js"></script>
	<script type="text/javascript" src="<%=path%>/static/js/common_util.js"></script>
	<script type="text/javascript" src="<%=path%>/static/js/test.js"></script>
	<script type="text/javascript" >
	 window.onload = function() {
		var isEdit = "${isEdit }";
		if (isEdit != null && isEdit == "yes") {
			//alert("edit");
			var inputform = com.whuang.hsj.$$one("inputform");
			//modify the target action fo form
			inputform.action = "osType/update";
			var titleSpan = document.getElementById("titleSpan");
			//alert(titleSpan);
			titleSpan.innerHTML = "edit os";
			var hidden_div = document.getElementById("hidden_div");
			hidden_div.innerHTML = '<input type="hidden" name="id" value="${os.id }" ></input>';//id of goods for update 

			var osTypeName=com.whuang.hsj.$$one("name");
			osTypeName.value=String("${os.name}");
			
		}
		//alert("222:${sessionScope.practiceWay}");
		var practiceWay=String("${sessionScope.practiceWay}");
		alert("333:"+practiceWay);
		a();
	};
	</script>
</head>
<body>
	<center>
	<h1><span id="titleSpan">add </span> </h1>
	<input type="hidden" value="${sessionScope.practiceWay}" name="hidpracticeWay" />
	<a href="javascript:history.go(-1)" >return </a>&nbsp;|&nbsp;
	<a href="index.jsp" >index</a>&nbsp;|&nbsp;<a href="osType/viewAll">view all</a>
		<form action="osType/save" name="inputform" >
			<table>
				<tr>
					<td>os name:</td>
					<td><input type="text" name="name" /></td>
				</tr>
				<tr>
					<td colspan="2" ><input type="submit"
						value="&nbsp;&nbsp;o&nbsp;k&nbsp;&nbsp;" id="submit_btn" /></td>
				</tr>
			</table>
			<div id="hidden_div"></div>
		</form>
	</center>
</body>
</html>

 

你可能感兴趣的:(内部js,获取后台变量,js获取后台变量,js内部代码,js获取变量)