使用 Struts 1.x 和 AJAX 设置会话变量

发布于 2024-09-25 17:15:28 字数 2153 浏览 3 评论 0原文

我继承了一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋风の叶未落 2024-10-02 17:15:28

问题解决了。事实证明,这是 AJAX 请求的浏览器缓存问题。 IE(我的测试环境)自动缓存AJAX请求。我通过添加以下代码行纠正了该问题:

response.setHeader("Cache-Control", "no-cache"); 

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:

response.setHeader("Cache-Control", "no-cache"); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文