简体   繁体   English

集成Struts2 Sitemesh DWR

[英]Integrating Struts2 Sitemesh DWR

I'm attempting to integrate Struts2 ver 2.5.2, DWR ver 2.0,5, and Sitemesh ver 2.5. 我正在尝试集成Struts2 2.5.2版,DWR 2.0.5版和Sitemesh 2.5版。 My DWR is (almost) already work fine. 我的DWR(几乎)已经可以正常工作。 I can open [CONTEXT]/dwr/engine.js and util.js, and also [CONTEXT]/dwr/interface/Arithmetic.js. 我可以打开[CONTEXT] /dwr/engine.js和util.js,也可以打开[CONTEXT] /dwr/interface/Arithmetic.js。

The problem is, when I open index.jsp which contains dwr call, the dwr js variable is not defined as well the Arithmetic variable. 问题是,当我打开包含dwr调用的index.jsp时,dwr js变量也未定义为Arithmetic变量。

The index.jsp itself already decorated from the Sitemesh filter. index.jsp本身已经通过Sitemesh过滤器进行了修饰。

web.xml 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>Struts 2</display-name>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
    <servlet-name>dwr-invoker</servlet-name>
    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value> true </param-value>
    </init-param>
    <init-param>
        <param-name>crossDomainSessionSecurity</param-name>
        <param-value>false</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>dwr-invoker</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<!-- Filters -->
<filter>
    <filter-name>struts-prepare</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class>
</filter>
<filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter>
    <filter-name>struts-execute</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts-prepare</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>struts-execute</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
    <servlet-name>jspSupportServlet</servlet-name>
    <servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
    <load-on-startup>5</load-on-startup>
</servlet>

<listener>
    <listener- class>org.apache.struts2.dispatcher.listener.StrutsListener</listener-class>
 </listener>
</web-app>

decorators.xml decorators.xml

<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/decorators">
<excludes>
    <pattern>/dwr/*</pattern>
</excludes>
<decorator name="basic-theme" page="basic-theme.jsp">
    <pattern>/*</pattern>
</decorator>
</decorators>

dwr.xml dwr.xml

<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://directwebremoting.org/schema/dwr20.dtd">
<dwr>
<allow>
    <create creator="new" javascript="HelloAjaxService">
        <param name="class"   value="com.test.struts.ajax.HelloAjaxService" />
    </create>
 </allow>
</dwr>

index.jsp index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello World</title>
<script type="text/javascript" src="/strutsSitemesh/dwr/engine.js"></script>
<script type='text/javascript' src='/strutsSitemesh/dwr/util.js'></script>
<script type='text/javascript' src='/strutsSitemesh/dwr/interface /HelloAjaxService.js'></script>
</head>
<body>
<div
    style="background-color: #ffeaa7; font-weight: bold; width: 300px;"
    id="divResponse">
    <s:form action="hello" method="post">
        <s:textfield id="name" name="name" label="Name" size="20"
            onkeypress="sayHello()" />
        <s:textfield name="age" label="Age" size="20" />
        <s:submit name="submit" label="Submit" align="center" />
    </s:form>

    <script type="text/javascript">
        function sayHello() {
        console.log(dwr.util.getValue("name"));
            HelloAjaxService.sayHelloAjax(function(data) {
                dwr.util.setValue("divResponse", data);
            });
        }
    </script>
</body>
</html>

If I commented filter-mapping sitemesh ---- the DWR call will work just fine. 如果我评论了过滤器映射sitemesh ----,DWR调用将正常工作。 So I assume, the request will filtered by SitemeshFilter, and strangely it will not go to DWR filter. 因此,我认为该请求将由SitemeshFilter进行过滤,奇怪的是,该请求将不会进入DWR过滤器。 I tried to search about how to arrange filter in sequence, but nothing comes up. 我试图搜索如何按顺序排列过滤器,但没有任何反应。 Any help appreciated. 任何帮助表示赞赏。 Big thanks. 太谢谢了。

So, I solved it by moving dwr js declaration to the body tag of index.jsp. 因此,我通过将dwr js声明移到index.jsp的body标签来解决了这个问题。 It happened because in my decorators.jsp, I only put <decorator:body> , and because of that, the <head> tag in decorators.jsp take precedence of the actual .jsp file(In this case, index.jsp). 发生这种情况是因为在我的decorators.jsp中,我只放置了<decorator:body> ,因此,decorators.jsp中的<head>标记优先于实际的.jsp文件(在本例中为index.jsp)。

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

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