javascript 外部文件获取后台变量

本文详细介绍了JavaWeb环境中,如何通过JS外部文件获取后台变量,包括控制器设置、页面隐藏域的使用及外部JS代码实现。重点展示了变量传递的过程和在外部JS文件中获取这些变量的具体方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java web中,js外部文件如何获取后台的变量呢?

项目使用spring MVC框架,

IDE:eclipse;

使用maven构建

控制器(OsTypeController.java)如下:

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>
				<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文件(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代码中如何获取后台变量的值呢?

请参阅我的下一篇日志 

 

附件是项目,采用maven构建

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值