在javascript中从window.open()调用时不调用Action方法

时间:2013-04-18 07:16:26

标签: javascript jsp struts2

我正在尝试打开一个新窗口,并在struts 2的过程中调用一个动作。

问题是jsp打开但是直到我按下刷新或从新的jsp开始才动作。

我希望操作执行然后重定向到相应的jsp,但是现在它重定向到对应于第一个映射结果的jsp。

来自javascript的

动作调用:

function displayGraph(graphName)
        {   alert("displayGraph");
            var url = 'drawCMGraph?data=' + encodeURIComponent(graphName);
            alert(url);
            window.open(url, "_blank",
            "directories=no, status=no,width=550, height=200,top=0,left=0");
        }
struts.xml中

动作映射

<action name="drawCMGraph" class="com.casealerts.action.HomeAction" method="Drawgraph">
<result name="success">/casealerts/test.jsp</result>
<result name="error">/casealerts/Error.jsp</result>
        </action>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>Comp</display-name>
    <filter>  
        <filter-name>struts2</filter-name>  
     <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>  <!--This listener handles caching of data when the server context is loaded.-->
        <listener-class>CompListener</listener-class>
    </listener>
    <welcome-file-list>
        <welcome-file>/casealerts/Login.jsp</welcome-file>
    </welcome-file-list>
</web-app>

动作

package com.casealerts.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.casealerts.businessdelegate.HomePageCMDelegate;
import com.casealerts.formbean.HomeForm;



public class HomeAction extends ActionSupport implements ModelDriven<HomeForm>,Preparable,ServletRequestAware
{

    /**
     * 
     */
    private static final long serialVersionUID = 272240572969090988L;
    Logger log=new Log4jHelper().getLogger(getClass());
    HomeDelegate hpd=null;
    private HttpServletRequest request;
    HomeForm form=null;
    private HttpSession session=null;

    @Override
    public void setServletRequest(HttpServletRequest request) {
        // TODO Auto-generated method stub
        this.request=request;

    }

    @Override
    public void prepare() throws Exception {
        // TODO Auto-generated method stub
        form=new HomeForm();

    }

    @Override
    public HomeForm getModel() {
        // TODO Auto-generated method stub
        return form;
    }

    public String Drawgraph()
    {
        session=request.getSession(true);
        hpd=new HomeDelegate(form,session);

        String result=hpd.getGraphData("alert", 1);

        return result;
    }
}

2 个答案:

答案 0 :(得分:0)

var url = 'drawCMGraph?data=' + encodeURIComponent(graphName);
            alert(url);

尝试将其更改为

var url = 'drawCMGraph.action?data=' + encodeURIComponent(graphName);
            alert(url);

答案 1 :(得分:0)

我找到了解决问题的方法。似乎我们需要为动作类中的URL中发送的数据创建变量和getter以及setter。 这适用于IE,但它在eclipse浏览器中不起作用