简体   繁体   English

在刷新页面之前未设置会话属性

[英]session attribute not being set until I refresh thepage

I have some code: 我有一些代码:

$(document).ready(function(){    
   $(":button").click(function(){
       var formData = {"field1":field1, "oper1":oper1, "value1":value1, "field2":field2, "oper2":oper2, "value2":value2, "field3":field3, "oper3":oper3, "value3":value3};
            $.post("<%=request.getRequestURL().toString()%>getInfo.jsp", formData, function(response){alertHere(response)});    
        })
})

function alertHere(response){
        alert("Post successful");
        alert(response);
        renderResults();
}

that posts form data and queries for the data and sets the session attribute with 发布表单数据并查询数据,并设置会话属性

 session.setAttribute("directoryString", xml);

when a button is clicked. 单击按钮时。 In the response function, renderResults is called, which grabs the returned xml: 在响应函数中,调用renderResults,它获取返回的xml:

 function renderResults(){
       alert("inside renderResults()");
       element = document.getElementById("person");
       xmlString = '<%=session.getAttribute("directoryString")%>';
       console.log('XML String: ' + xmlString);

The rest of the function is written to parse out the xml and display it on the page. 该函数的其余部分用于解析xml并将其显示在页面上。

My problem is that when I first go to the page and post data, the xmlString variable is null. 我的问题是,当我第一次转到页面并发布数据时,xmlString变量为null。 When I refresh the page and go back to "Page Source" in my web console, the variable is set correctly. 当我刷新页面并返回到Web控制台中的“页面源”时,变量设置正确。 I'm not exactly sure what's going on. 我不确定到底发生了什么。 Is it possible that my function is trying to call the session attribute before it's set? 我的函数是否有可能在设置属性之前尝试调用session属性?

PS I know that scriplets aren't the best way of doing it, but that's the way we do it around here. 附言:我知道,小蜘蛛并不是做到这一点的最佳方法,但这就是我们在此处所做的方法。

<%=session.getAttribute("directoryString")%> will only be evaluated when the JSP is rendered into the HTML page. <%=session.getAttribute("directoryString")%>仅在将JSP呈现到HTML页面中时进行评估。 Once the rendered HTML is in the browser, it can only be changed either by: 呈现的HTML在浏览器中后,只能通过以下方式之一进行更改:

  • reloading the page 重新加载页面
  • JavaScript. JavaScript。

Make an ajax call to get the updated value of the attribute and update the variable (for example, you can set document.myXmlString = '' and use that value in your renderResults . 进行ajax调用以获取属性的更新值并更新变量(例如,您可以设置document.myXmlString = ''并在renderResults使用该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM