简体   繁体   English

如何从 Ajax 或 JavaScript 调用 Struts1 Action?

[英]How to call Struts1 Action from Ajax or JavaScript?

I need to call an action on load of a JSP.我需要在加载 JSP 时调用一个操作。 To keep track of number of users who visited that page.跟踪访问该页面的用户数量。

I have an action VisitorCounterAction , where it updates the database.我有一个动作VisitorCounterAction ,它会更新数据库。 On load of the JSP I'm calling an ajax function callCounter() ;在加载 JSP 时,我正在调用 ajax 函数callCounter()

{

        alert("callCounter");
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        // i need some correction here
        xmlhttp.open("GET",VisitorCounterAction,false);
        xmlhttp.send(null);
        alert("callCounter returned from Action");
        }

I am getting an exception as:我遇到了一个例外:

/web/guest/content?p_p_id=31&p_p_lifecycle=0&p_p_state=pop_up&p_p_mode=view&_31_struts_action=%2Fimage_gallery%2Fview_slide_show&_31_folderId=10605 generates exception: null

Please help me with this.请帮我解决一下这个。 Or any other way to call the Action.或任何其他方式来调用动作。 I can't reload the page as it'll call onload function again.我无法重新加载页面,因为它会再次调用 onload 函数。

Thanks, Dj谢谢,DJ

You need to specify the correct url when calling the action.您需要在调用操作时指定正确的 url。 In your struts.xml file you would have created an action entry with a name.在您的 struts.xml 文件中,您将创建一个带有名称的操作条目。 Let's say that name is VisitorCounterAction.假设名称是 VisitorCounterAction。 Note that this is not the name of the Action class but the name you use in struts.xml.请注意,这不是 Action 类的名称,而是您在 struts.xml 中使用的名称。

Also you would have configured struts to recognize a particular extension.此外,您还需要配置 struts 来识别特定的扩展。 By default it is .do.默认情况下它是 .do。

To request the action you will then use: VisitorCounterAction.do.要请求该操作,您将使用:VisitorCounterAction.do。

So try:所以试试:

  xmlhttp.open("GET","VisitorCounterAction.do",false);

Note also the double quotes around the URL as this is a String in the xmlHttp object's open() method.还要注意 URL 周围的双引号,因为这是 xmlHttp 对象的 open() 方法中的字符串。

The second parameter to the XMLHttpRequest object's open method should be a string that is the URL of the request. XMLHttpRequest 对象的open方法的第二个参数应该是一个字符串,它是请求的 URL。 In your case it should be the Struts' Action Mapping that maps to your VisitorCounterAction .在您的情况下,应该是 Struts 的 Action Mapping 映射到您的VisitorCounterAction Something like:就像是:

xmlhttp.open("GET", "/viewCounterAction.do", false); 

If you are using Liferay 4.2, then this link might help you ( http://www.liferay.com/web/guest/community/wiki/-/wiki/Main/Ajax+Toolkit ).如果您使用的是 Liferay 4.2,那么此链接可能会对您有所帮助( http://www.liferay.com/web/guest/community/wiki/-/wiki/Main/Ajax+Toolkit )。 Liferay 4.2 have own set of javascript API for doing AJAX functionality(Implemented in jquery). Liferay 4.2 有自己的一组用于执行 AJAX 功能的 javascript API(在 jquery 中实现)。

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

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