ide:eclipse;
使用maven建構

package com.ct.web.controller;
import java.util.date;
import java.util.list;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.sessionattributes;
import com.ct.dao.ostypedao;
import com.ct.entity.ostype;
import com.time.util.timehwutil;
@controller
@requestmapping("/ostype")
@sessionattributes("practiceway")
public class ostypecontroller {
private ostypedao ostypedao;
private string redirectviewall="redirect:/ostype/viewall";
@requestmapping(value = "/add")
public string addinputostype(string practiceway,model model){
model.addattribute("practiceway", practiceway);
system.out.println("practiceway:"+practiceway);
return "ostype/addostype";
}
/******************************************************************/
public ostypedao getostypedao() {
return ostypedao;
@autowired
public void setostypedao(ostypedao ostypedao) {
this.ostypedao = ostypedao;
}
控制器方法addinputostype 對應的頁面(addostype.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>
</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>
<td colspan="2" ><input type="submit"
value="&nbsp;&nbsp;o&nbsp;k&nbsp;&nbsp;" id="submit_btn" /></td>
</table>
<div id="hidden_div"></div>
</form>
</center>
</body>
</html>
其中外部js檔案(test.js)如下:

alert("111:${sessionscope.practiceway}");
window.onload =function a()
{
alert("method:a");
alert("444:${sessionscope.practiceway}");
alert("hid:"+document.getelementsbyname("hidpracticeway")[0].value);
};
在浏覽器中輸入http://localhost:8088/demo_channel_terminal/ostype/add?practiceway=random
運作結果:
對應的是外部js檔案(test.js)的執行結果。
是以我們可以得出結論:
在外部js檔案擷取背景變量的方式就是通過擷取頁面隐藏域的值。
具體步驟:
(1)把背景變量設定到頁面隐藏域中,例如

<input type="hidden" value="${sessionscope.practiceway}" name="hidpracticeway" />
(2)在外部js檔案中擷取隐藏域的值,例如

alert("hid:"+document.getelementsbyname("hidpracticeway")[0].value);
那麼在jsp頁面中的js代碼中如何擷取背景變量的值呢?
請參閱我的下一篇日志