DWR与Struts1整合
1.下載DWR Version 2
https://dwr.dev.java.net/files/documents/2427/47504/dwr.jar
2.安裝DWR,把dwr.jar放到WEB-INF/lib下
web.xml中加入DWRServlet & ActionServlet
其中<load-on-startup>的部分要特別注意,ActionServlet要先初始化,所以數字要比較小.
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>10</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
dwr.xml中加入struts的設定,其中formBean的參數的value值,會對應到struts-config.xml中<form-beans>的設定
12345678
<dwr> <allow>
<create creator="struts" javascript="testFrm">
<param name="formBean" value="testActionForm"/>
</create> </allow>
</dwr>
struts-config.xml
<struts-config> <form-beans>
<form-bean name="testActionForm" type="test.struts.testActionForm" />
</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,取得現在最新的日期
package test.struts; 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());
mapping.findForward("display"); }}
date.jsp页面,在form的部分,請用struts 的 tag library,我把<html:text property="strDate" size="30" >改成<input type="text" name="strDate">後,無法正常的接受到值.
<%@ page contentType="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" οnclick="refreshDate();" value="更新日期"/><br/>
<html:submit>送出 </html:submit></html:form></body></html>
display.jsp 页面
<%@ page contentType="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>
转载于“冬雪”的blog,在此向其致意!本想在网上找一些关于dwr与struts2集成应用的小例子,来学习学习,但关于dwr与struts2集成的资料太少了。偶然间看见这篇struts1与dwr集成的例子,就把他收藏于此,供日后学习。如果哪位有关于struts2与dwr集成的资料或小例子,请告诉我一下,谢谢。
1.下載DWR Version 2
https://dwr.dev.java.net/files/documents/2427/47504/dwr.jar
2.安裝DWR,把dwr.jar放到WEB-INF/lib下
web.xml中加入DWRServlet & ActionServlet
其中<load-on-startup>的部分要特別注意,ActionServlet要先初始化,所以數字要比較小.
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>10</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
dwr.xml中加入struts的設定,其中formBean的參數的value值,會對應到struts-config.xml中<form-beans>的設定
12345678
<dwr> <allow>
<create creator="struts" javascript="testFrm">
<param name="formBean" value="testActionForm"/>
</create> </allow>
</dwr>
struts-config.xml
<struts-config> <form-beans>
<form-bean name="testActionForm" type="test.struts.testActionForm" />
</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,取得現在最新的日期
package test.struts; 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());
mapping.findForward("display"); }}
date.jsp页面,在form的部分,請用struts 的 tag library,我把<html:text property="strDate" size="30" >改成<input type="text" name="strDate">後,無法正常的接受到值.
<%@ page contentType="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" οnclick="refreshDate();" value="更新日期"/><br/>
<html:submit>送出 </html:submit></html:form></body></html>
display.jsp 页面
<%@ page contentType="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>
转载于“冬雪”的blog,在此向其致意!本想在网上找一些关于dwr与struts2集成应用的小例子,来学习学习,但关于dwr与struts2集成的资料太少了。偶然间看见这篇struts1与dwr集成的例子,就把他收藏于此,供日后学习。如果哪位有关于struts2与dwr集成的资料或小例子,请告诉我一下,谢谢。