dwr框架在nutz中如何引用,
38 回复
在pom.xml中引入了dwr的jar包,配置了web.xml文件,在webINF下新建了dwr.xml文件,dwr.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd">
<dwr>
<allow>
<create creator="new" >
<param name="class" value="com.wonders.tdsc.workflow.DwrTest"/>
</create>
</allow>
</dwr>
新建java类,如下:
package com.wonders.tdsc.workflow;
public class DwrTest {
public String sayHello(String name){
System.out.println("hello "+name);
return "hello "+name;
}
}
jsp页面如下:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script type='text/javascript' src="dwr/engine.js"></script>
<script type='text/javascript' src='dwr/util.js'></script>
<script src="dwr/interface/DwrTest.js"></script>
<script>
Hello.sayHello("张三");
</script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
web.xml中配置了,如下:
<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<mime-mapping>
但是js hello找不到,浏览器错误如下
Uncaught ReferenceError: Hello is not defined
at dwrTest.jsp:12
(anonymous) @ dwrTest.jsp:12
封装了ctx,如下:<c:set var="ctx" value="${pageContext.request.contextPath}"/>,在jsp页面引用的时候加了ctx,还是不行,如下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ include file="/commonjsp/taglibs.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script type='text/javascript' src="${ctx}/dwr/engine.js"></script>
<script type='text/javascript' src='${ctx}/dwr/util.js'></script>
<script src="${ctx}/dwr/interface/DwrTest.js"></script>
<script>
Hello.sayHello("张三");
</script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>jygl</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>15</session-timeout>
</session-config>
<filter>
<filter-name>nutz</filter-name>
<filter-class>org.nutz.mvc.NutFilter</filter-class>
<init-param>
<param-name>modules</param-name>
<param-value>com.wonders.MainModule</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>^(.+[.])(jsp|png|gif|jpg|js|css|jspx|jpeg|html|xml|htm|doc|docx)$</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>nutz</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<servlet>
<description></description>
<display-name>SaveDreamOffice</display-name>
<servlet-name>SaveDreamOffice</servlet-name>
<servlet-class>com.wonders.tiles.dreamoffice.SaveDreamOfficeAt</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SaveDreamOffice</servlet-name>
<url-pattern>/SaveDreamOffice</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>OpenDreamOffice</display-name>
<servlet-name>OpenDreamOffice</servlet-name>
<servlet-class>com.wonders.tiles.dreamoffice.OpenDreamOfficeAt</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>OpenDreamOffice</servlet-name>
<url-pattern>/OpenDreamOffice</url-pattern>
</servlet-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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<mime-mapping>
<extension>doc</extension>
<mime-type>application/msword</mime-type>
</mime-mapping>
<mime-mapping>
<extension>ppt</extension>
<mime-type>application/ms-powerpoint</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xls</extension>
<mime-type>application/ms-excel</mime-type>
</mime-mapping>
<!-- <listener>
<listener-class>com.wonders.socket.SocketListener</listener-class>
</listener> -->
</web-app>
这个方法
package com.wonders.tdsc.workflow;
public class DwrTest {
public String sayHello(String name){
System.out.println("hello "+name);
return "hello "+name;
}
}
后台报错
严重: Servlet [dwr-invoker] in web application [/trademanage] threw load() exception
java.lang.ClassNotFoundException: org.directwebremoting.servlet.DwrServlet
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1139)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:518)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:499)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1102)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1038)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4996)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1092)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1834)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
原来dwr依赖都没加
<dependency>
<groupId>org.directwebremoting</groupId>
<artifactId>dwr</artifactId>
<version>3.0.M1</version>
</dependency>
pom.xml中有的
<dependency>
<groupId>org.directwebremoting</groupId>
<artifactId>dwr</artifactId>
<version>3.0.2-RELEASE</version>
</dependency>
添加回复
请先登陆