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(); 還可以直接用參數來獲取。