天天看点

javascript 外部文件获取后台变量

ide:eclipse;

使用maven构建

javascript 外部文件获取后台变量

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)如下:

javascript 外部文件获取后台变量

<?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> |   

    <a href="index.jsp" >index</a> | <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="  o k  " id="submit_btn" /></td>  

            </table>  

            <div id="hidden_div"></div>  

        </form>  

    </center>  

</body>  

</html>  

 其中外部js文件(test.js)如下:

javascript 外部文件获取后台变量

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

运行结果:

javascript 外部文件获取后台变量

对应的是外部js文件(test.js)的执行结果。

所以我们可以得出结论:

在外部js文件获取后台变量的方式就是通过获取页面隐藏域的值。

具体步骤:

(1)把后台变量设置到页面隐藏域中,例如

javascript 外部文件获取后台变量

<input type="hidden" value="${sessionscope.practiceway}" name="hidpracticeway" />  

 (2)在外部js文件中获取隐藏域的值,例如

javascript 外部文件获取后台变量

alert("hid:"+document.getelementsbyname("hidpracticeway")[0].value);  

那么在jsp页面中的js代码中如何获取后台变量的值呢?

请参阅我的下一篇日志