我试图整合Struts2 ver 2.5.2,DWR ver 2.0,5和Sitemesh ver 2.5。我的DWR(几乎)已经正常工作了。我可以打开[CONTEXT] /dwr/engine.js和util.js,还可以打开[CONTEXT] /dwr/interface/Arithmetic.js。
问题是,当我打开包含dwr调用的index.jsp时,dwr js变量也没有定义为Arithmetic变量。
index.jsp本身已经从Sitemesh过滤器中修饰。
的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
<?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
<!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
<%@ 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>
如果我评论了过滤映射sitemesh ,那么DWR调用就可以了。所以我假设,请求将被SitemeshFilter过滤,奇怪的是它不会转到DWR过滤器。我试图搜索如何按顺序排列过滤器,但没有任何结果。任何帮助赞赏。非常感谢。
答案 0 :(得分:1)
所以,我通过将dwr js声明移动到index.jsp的body标签来解决它。之所以发生这种情况,是因为在我的decorators.jsp中,我只放了<decorator:body>
,因此,decorators.jsp中的<head>
标记优先于实际的.jsp文件(在本例中为index.jsp) )。