1、新建Liferay Projext, Plugin Type选择Porlet,Project Name输入:DWRTest
2、下载DWR的jar包,http://directwebremoting.org/dwr/downloads/index.html
3、将dwr.jar放置到Lib文件夹下
4、配置web.xml
<servlet> <servlet-name>DwrServlet</servlet-name> <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class> <load-on-startup>12</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DwrServlet</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
5、新建dwr.xml
<?xml version="1.0" encoding="UTF-8"?> <dwr> <allow> <create creator="new" javascript="DWRTestService"> <param name="class" value="com.example.DwrTest.DwrMain"/> <include method="echo" /> <include method="getThisPrivateDate" /> <include method="getThisPrivateDate2" /> <include method="getThisPrivateDate3" /> </create> <convert match="com.example.DwrTest.DwrBean" converter="bean"> <param name="include" value="id,name"></param> </convert> </allow> </dwr>
6、添加 java的package, package名: com.example.DwrTest
java类:DwrMain.java 普通测试用
DwrBean.java bean测试用
7,DwrMain.java的代码如下:
package com.example.DwrTest; import javax.servlet.http.HttpSession; import uk.ltd.getahead.dwr.WebContext; import uk.ltd.getahead.dwr.WebContextFactory; public class DwrMain { /** * public static test * * @param message * @return * @throws Exception */ public static String echo(String message) throws Exception { WebContext webContext = WebContextFactory.get(); HttpSession session = webContext.getSession(); String msg = message + " " + session.getId(); return msg; } /** * private test * @return */ private String getThisPrivateDate() { return "getThisPrivateDate"; } /** * public test * @param session * @return */ public String getThisPrivateDate2(HttpSession session) { return "getThisPrivateDate2" + session.getId(); } /** * bean test * @return */ public DwrBean getThisPrivateDate3() { DwrBean bean = new DwrBean(); bean.setId("id1"); bean.setName("name1"); return bean; } }DwrBean.java的代码如下:
package com.example.DwrTest; public class DwrBean { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
8、view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%> <portlet:defineObjects /> DWR TEST <script src='<%=renderResponse.encodeURL(renderRequest.getContextPath() + "/dwr/engine.js")%>' ></script> <script src='<%=renderResponse.encodeURL(renderRequest.getContextPath() + "/dwr/util.js")%>'></script> <script src='<%=renderResponse.encodeURL(renderRequest.getContextPath() + "/dwr/interface/DWRTestService.js")%>'></script> <script type="text/javascript"> DWRTestService.echo("qwer",function(dataFromService) { alert(dataFromService); }); DWRTestService.getThisPrivateDate2(function(dataFromService) { alert(dataFromService); }); DWRTestService.getThisPrivateDate3(function(dataFromService) { alert(dataFromService.id + dataFromService.name); }); </script>9,启动服务把portlet拖到画面中,测试一下吧。
10、DWR的相关配置方法,请谷歌一下吧。
HttpServletRequest,HttpServletResponse,HttpSession 除了可以用WebContext webContext = WebContextFactory.get(); 还可以直接用参数来获取。
原文链接:https://www.f2er.com/ajax/166147.html