环境: jdk1.7 Eclipse tomcat7
架包:dwr.jar log4j.jar commons-logging.jar
1.导入架包
2.配置web.xml
<servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class> org.directwebremoting.servlet.DwrServlet </servlet-class> <!--2.0.3以前版本加这个配置 --> <init-param> <param-name>classes</param-name> <param-value>java.lang.Object</param-value> </init-param> <init-param> <param-name>logLevel</param-name> <param-value>DEBUG</param-value> </init-param> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/dwr.xml</param-value> </init-param> <!-- 多配置文件起名开头以config-什么什么 --> <init-param> <param-name>config-2</param-name> <param-value>/WEB-INF/dwr2.xml</param-value> </init-param> <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>
3.新建dwr.xml和dwr2.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr//dwr20.dtd"> <dwr> <allow> <create javascript="JDate" creator="new" scope="application"> <param name="class" value="java.util.Date"></param> <include method="toString"/> </create> </allow> </dwr> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr//dwr20.dtd"> <dwr> <allow> <create javascript="getTime" creator="new" scope="application"> <param name="class" value="com.niwh.bean.time"></param> <include method="getTime"/> <include method="timebean"/> </create> <convert match="com.niwh.bean.timeBean" converter="bean"> <param name="include" value="hourText,secondText,minuteText"/> </convert> </allow> </dwr>
4.java类
package com.niwh.bean; import java.util.Date; public class time { public String getTime(){ return (new Date().toLocaleString()); } public timeBean timebean(){ timeBean tb = new timeBean(); tb.setHourText("12"); tb.setMinuteText("34"); tb.setSecondText("50"); return tb; } }
5.javabean
package com.niwh.bean; public class timeBean { private String hourText; private String minuteText; private String secondText; /** * @return the hourText */ public String getHourText() { return hourText; } /** * @param hourText the hourText to set */ public void setHourText(String hourText) { this.hourText = hourText; } /** * @return the minuteText */ public String getMinuteText() { return minuteText; } /** * @param minuteText the minuteText to set */ public void setMinuteText(String minuteText) { this.minuteText = minuteText; } /** * @return the secondText */ public String getSecondText() { return secondText; } /** * @param secondText the secondText to set */ public void setSecondText(String secondText) { this.secondText = secondText; } }
6.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <Meta http-equiv="pragma" content="no-cache"> <Meta http-equiv="cache-control" content="no-cache"> <Meta http-equiv="expires" content="0"> <Meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <Meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <script type='text/javascript' src='<%=path%>/dwr/interface/JDate.js'></script> <script type='text/javascript' src='/DWRWebTest/dwr/interface/getTime.js'></script> <script type='text/javascript' src='<%=path%>/dwr/engine.js'></script> <script type='text/javascript' src='<%=path%>/dwr/util.js'></script> <script type="text/javascript"> function showTime(){ JDate.toString(callbackfun1); } callbackfun1=function(toStringReturnTimeString){ //alert("当前的服务器端的时间为:"+toStringReturnTimeString); document.getElementById("showTimeResult").innerHTML =toStringReturnTimeString; //$("showTimeResult").innerHTML =toStringReturnTimeString; } function showTime2(){ getTime.getTime(callbackfun2); } callbackfun2 = function(returnSystemTimeString){ document.getElementById("showTimeResult2").innerHTML = returnSystemTimeString; } function showbean(){ getTime.timebean(callbackfun3); } callbackfun3 = function(returnBean){ var returnTime = returnBean.hourText+":"+returnBean.minuteText+":"+returnBean.secondText; document.getElementById("showTimeResult3").innerHTML = returnTime; } </script> <body> <a href="#" onclick="javascript:showTime()"> 点击我可以获得当前服务器端的时间 </a> <div id="showTimeResult"> </div> <a href="#" onclick="javascript:showTime2()"> 点击我可以获得time类方法获得的服务器端的时间 </a> <div id="showTimeResult2"> </div> <a href="#" onclick="javascript:showbean()"> 点击我可以获得timebean类方法获得的服务器端的时间 </a> <div id="showTimeResult3"> </div> </body> </html>原文链接:https://www.f2er.com/ajax/164683.html