你的位置:首页 > Java教程

[Java教程]javascript 如何访问 action或者controller 传给 jsp 页面的值


<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><%@ page import="com.alibaba.fastjson.JSONObject"%><%  JSONObject jo = null;  String openid = null;  if(request.getSession().getAttribute("openidAndAccessToken") != null){    jo = (JSONObject)request.getSession().getAttribute("openidAndAccessToken");    if(jo != null)      openid = jo.getString("openid");  }    String accessToken = null;  if(request.getAttribute("access_token") != null){    accessToken = request.getAttribute("access_token");  }%>
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title><%=openid %></title><script type="text/javascript">var openid = "<%=openid %>";var accessToken = "<%=accessToken %>";
....
</script></head><body><p>openid:<%=openid %></p><p>openid:<%=accessToken %></p></body></html>

如上述代码所示,js访问 jsp 页面的值,需要使用双引号:var openid = "<%=openid %>"

而在页面显示则不需要使用引号:<p>openid:<%=openid %></p>