使用 Struts 1.x 和 AJAX 设置会话变量
我继承了一个使用 Struts 1.2 开发的网站,我需要对其进行一些更新。对于其中一项更新,我需要根据用户单击按钮时从动态创建(使用 struts)下拉框中选择的值来设置会话变量。我尝试完成此任务的方法是使用按钮的“onclick”属性来调用 JavaScript 函数,该函数利用 AJAX 对 struts Action 类进行异步调用。
我已经成功实施了这个解决方案,但是,我遇到了一个偶尔发生的问题。在大多数情况下,当我单击按钮时,会话变量似乎已正确设置。但是,有时会话变量不会被设置/重置。这个问题的零星性质让我感到困惑。看起来 AJAX 代码块正在被调用和处理(返回 readyState = 4 和 status = 200),但是,Java struts 逻辑没有被输入。代码如下。
JavaScript AJAX 函数:
function createTaskOpp() {
var selectedOpp = document.getElementById("oppId");
var url="setTaskSessionOpportunity.do?opp=" + selectedOpp.value;
var ajaxRequest;
try
{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer Browsers
try
{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
// Something went wrong while trying to create our HTTP object. Resubmit the current page.
alert("Browser Error. Error creating Request Object: " + e);
window.location ="taskConfiguration.do";
}
}
}
ajaxRequest.open("GET", url, true);
ajaxRequest.onreadystatechange = function() {
if (ajaxRequest.readyState == 4)
{
if (ajaxRequest.status == 200)
{
window.location ="saveTask.do";
}
else {
window.location ="taskConfiguration.do";
}
}
};
ajaxRequest.send(null); }
Struts 操作:
public class SetTaskSessionOpportunityAction extends Action {
public ActionForward execute( ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
System.out.println("*****************");
System.out.println("In SetTaskOpp AJAX -Beginning");
HttpSession session = request.getSession();
String taskOpp = request.getParameter("opp");
session.setAttribute("task_opportunity", taskOpp);
ActionForward actionForward = new ActionForward("/viewTask.jsp");
return actionForward;
}
}
感谢您的帮助! -乔什
I've inherited a website developed using Struts 1.2 that I need to make some updates to. For one of the updates, I need to set a session variable based on a value selected from a dynamically created (using struts) dropdown box when the user clicks a button. The way I'm attempting to accomplish this task is by using the 'onclick' property of my button to call a JavaScript function that utilizes AJAX to make an asynchronous call to a struts Action class.
I've successfully implemented this solution, however, I'm running into an issue that only occurs sporadically. For the most part, when I click my button the session variable seems to be set correctly. However, occasionally, the session variable is not being set/reset. The sporadic nature of this issue is what is throwing me off. It looks like the AJAX code block is being called and processed (returning a readyState = 4 and status = 200), however, the Java struts logic is NOT being entered. Code is below.
JavaScript AJAX function:
function createTaskOpp() {
var selectedOpp = document.getElementById("oppId");
var url="setTaskSessionOpportunity.do?opp=" + selectedOpp.value;
var ajaxRequest;
try
{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer Browsers
try
{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
// Something went wrong while trying to create our HTTP object. Resubmit the current page.
alert("Browser Error. Error creating Request Object: " + e);
window.location ="taskConfiguration.do";
}
}
}
ajaxRequest.open("GET", url, true);
ajaxRequest.onreadystatechange = function() {
if (ajaxRequest.readyState == 4)
{
if (ajaxRequest.status == 200)
{
window.location ="saveTask.do";
}
else {
window.location ="taskConfiguration.do";
}
}
};
ajaxRequest.send(null); }
Struts Action:
public class SetTaskSessionOpportunityAction extends Action {
public ActionForward execute( ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
System.out.println("*****************");
System.out.println("In SetTaskOpp AJAX -Beginning");
HttpSession session = request.getSession();
String taskOpp = request.getParameter("opp");
session.setAttribute("task_opportunity", taskOpp);
ActionForward actionForward = new ActionForward("/viewTask.jsp");
return actionForward;
}
}
Thanks for the help!
-Josh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题解决了。事实证明,这是 AJAX 请求的浏览器缓存问题。 IE(我的测试环境)自动缓存AJAX请求。我通过添加以下代码行纠正了该问题:
Problem solved. It turns out it was a browser caching issue with AJAX request. IE (my test environment) automatically caches AJAX request. I corrected the issue by adding the following line of code: