DWR与Struts整合
/*</url-pattern>??
</servlet-mapping>
dwr.xml中加入struts的定,其中formBean的的value值,到struts-config.xml中<form-beans>的定
"struts" javascript="testFrm">??????<param name="formBean" value="testActionForm"/>????
</create>??
</allow>??
</dwr>??
struts-config.xml
</form-beans>??
<action-mappings>????
<action name="testActionForm" path="/testAction" scope="session" type="test.struts.testAction" validate="false">??????
<forward name="display" path="/display.jsp" />????
</action>??
</action-mappings>??
<message-resources parameter="ApplicationResources" />
</struts-config>
testActionForm.java,getDate()透dwr,取得在最新的日期
import org.apache.struts.action.*;
import java.util.*;
public class testActionForm extends ActionForm {?????private String strDate;?????
public void setStrDate(String strDate) {????????this.strDate = strDate;????
}?????
public String getStrDate() {????????return strDate;????
}????
//dwr????public String getDate() {????????Date date = new Date();????????
return date.toString();???
}
}
testAction.java
package test.struts;import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import org.apache.struts.action.*;
public class testAction extends Action {????public ActionForward execute(ActionMapping mapping, ActionForm form,?????????????????????????????????
HttpServletRequest request,?????????????????????????????????
HttpServletResponse response) {?????????testActionForm actionForm = (testActionForm) form;???????
System.out.println(actionForm.getStrDate());????????
return mapping.findForward("display");????}
}
date.jsp,在form的部分,用struts 的 tag library,我把<html:text property="strDate" size="30" >改成<input type="text" name="strDate">後,法正常的接受到值.
"text/html; charset=Big5" %><%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html><head>
<title>title</title>??
<script type='text/javascript' src='dwr/interface/testFrm.js'></script>??
<script type='text/javascript' src='dwr/engine.js'></script>??
<script type='text/javascript' src='dwr/util.js'></script>
</head>
<SCRIPT LANGUAGE="JavaScript" type="">
function refreshDate() {???testFrm.getDate(populateDate)
;}
function populateDate(data){???DWRUtil.setValue('strDate', data);}
</script>
<body>
<html:form action="testAction.do">
date:<html:text property="strDate" size="30" ></html:text>
<input type="button" onclick="refreshDate();" value="更新日期"/><br/>???
<html:submit>送出?? </html:submit>
</html:form></body></html>
display.jsp
"text/html; charset=Big5" %><%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
%@page import="test.struts.*"%
<html>
<head>
<title>test</title>
</head><body bgcolor="#ffffff"><h1>您送出的日期:<br>
<bean:write name="testActionForm" property="strDate"/></h1>
</body>
</html>