我是DWR的新手。我试图使用DWR脚本访问java类。
我使用带有struts的DWR作为web层,并在内部弹簧用于依赖注入。
我的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:jsp="http://java.sun.com/xml/ns/javaee/jsp" 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_TAPE" version="3.0">
<welcome-file-list>
<welcome-file>Login.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
<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>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>
我的dwr.xml是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://getahead.org/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="task">
<param name="class" value="com.Task"/>
</create>
</allow>
</dwr>
在我的JSp中,我在下面添加了三行:
<script type='text/javascript' src="<%=contextString %>/dwr/engine.js"></script>
<script type='text/javascript' src="<%=contextString %>/dwr/util.js"></script>
<script type='text/javascript' src="<%=request.getContextPath() %>/dwr/interface/task.js"></script>
这就是我尝试访问“任务”脚本的方式:
var count = task.getTaskCount();
alert(count);
方法getTaskCount是一个返回int值的简单方法。
现在,在上面的代码中,我可以使用访问engine.js和util.js文件 / DWR /.
但是当我尝试使用url访问task.js文件时: /dwr/interface/task.js,脚本未生成,并且在控制台上生成以下错误:
2014-04-23 18:48:55,896 ["http-bio-8444"-exec-9] WARN org.directwebremoting.servlet.ExceptionHandler:67 - Unhandled Exception
java.lang.SecurityException: No class by name: taskBL
at org.directwebremoting.impl.DefaultCreatorManager.getCreator(DefaultCreatorManager.java:189)
at org.directwebremoting.impl.DefaultRemoter.generateInterfaceScript(DefaultRemoter.java:158)
at org.directwebremoting.servlet.InterfaceHandler.handle(InterfaceHandler.java:50)
at org.directwebremoting.servlet.UrlProcessor.handle(UrlProcessor.java:101)
at org.directwebremoting.servlet.DwrServlet.doPost(DwrServlet.java:146)
at org.directwebremoting.servlet.DwrServlet.doGet(DwrServlet.java:132)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
感谢任何帮助。
提前致谢。
答案 0 :(得分:0)
我没有使用dwr.xml文件(我有空文件)。我使用注释(例如@RemoteProxy),但我不确定您是否正确使用javascript属性(在您的dwr.xml文件中)。我认为它不是您在Javascript中用来调用远程Java对象的标识符。
尝试包含这样的js脚本(注意大写的 T 字母):
<script type='text/javascript' src="<c:url value='/dwr/interface/Task.js' />"></script>
url标签来自<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
,它对我有用,我觉得它更漂亮。
然后Javascript中的方法调用看起来像那样(大写的 T 字母):
Task.getTaskCount();
请查看我的Web.xml。我使用注释,所以我需要init-param:classes。它告诉dwr哪些类将被远程控制。
<servlet>
<display-name>DWR Servlet</display-name>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>classes</param-name>
<param-value>com.Task</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>