日期:2014-05-16 浏览次数:20570 次
index.jsp
<body> <form name="myform"> <input value="test" type="button" onclick="test();" /> <input value="text" name="datevalue" /> </form> </body> <script> //(1)获取对象实例 var xmlHttp = GetXmlHttpObject(); function test() { //(2)注意这里的url格式,<action path="/do/test"..的写法如下 var url = "do/test.do"; //(3)设定open参数 xmlHttp.open("POST",url,true); //(4)设置状态改变时候的执行函数 xmlHttp.onreadystatechange=setValue; xmlHttp.send(null); } function GetXmlHttpObject() { var xmlHttp = null; try { // Firefox, Opera 8.0+, Safari xmlHttp = new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } function setValue() { //(5)成功接收之后的处理 if(xmlHttp.readyState == 4) { document.myform.datevalue.value=xmlHttp.responseText; } } </script>
?
struts_config.xml
<action-mappings> <action path="/do/test" type="com.test.TempAction"/> </action-mappings>
?
TempAction.java
@Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { PrintWriter pw = response.getWriter(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String today = df.format(new Date()); //(1)这里的print就是index.jsp中要获取的responseText pw.print(today); //(2)记得要flush pw.flush(); //(3)如果是ajax的请求,这里不要返回页面,不然会将整个页面html源码赋值到responseText中 return null; }?