demo.jsp used to display client interface service:
DemoAction is a Struts sub-class of Action to control the conversion,
DemoFacade business category is responsible for business processes.
Goods is a business entity class
Struts on the part of the configuration do not want to overlook the fact this application does not include the main Struts configuration dwr configuration to talk about, first of all need to increase the web.xml mapping servelt the following:
<servlet>
<servlet-name> dwr-invoker </ servlet-name>
<display-name> DWR Servlet </ display-name>
<servlet-class> uk.ltd.getahead.dwr.DWRServlet </ servlet-class>
<init-param>
<param-name> debug </ param-name>
<param-value> true </ param-value>
</ init-param>
</ servlet>
<servlet-mapping>
<servlet-name> dwr-invoker </ servlet-name>
<url-pattern> / dwr / * </ url-pattern>
</ servlet-mapping>
As uk.ltd.getahead.dwr.DWRServlet is the core of dwr to deal with javascript on the remote method call, there are other parameters please refer to the relevant documents.
Then, dwr.xml need to configure the appropriate remote method (with the specific needs of the client calls the methods), the following configuration for the demo application:
<dwr>
<allow>
<convert converter="bean" match="dwr.demo.Goods"/>
<create creator="new" javascript="DemoAction">
<include method="query4dwr"/>
<include method="copy4dwr"/>
<include method="paste4dwr"/>
</ create>
<create creator="new" javascript="DemoFacade">
<include method="queryList"/>
<include method="restore"/>
<include method="del"/>
</ create>
</ allow>
</ dwr>
As there is a converter (converter) is used for the bean mapping dwr.demo.Goods type, other types of converters please refer to the documentation; and two, respectively, creator of the creation of javascript classes and DemoAction category DemoFacade, corresponding dwr . demo.DemoAction category and dwr.demo.DemoFacade, the definition of methods which can be directly called from the javascript.
Finally, we need to include the appropriate page in the javascript:
<script src="/oblog312/dwr/interface/DemoAction.js"> </ script>
<script src="/oblog312/dwr/interface/DemoFacade.js"> </ script>
<script src="/oblog312/dwr/engine.js"> </ script>
<script src="/oblog312/dwr/util.js"> </ script>
As noted, dwr / interface / DemoAction.js and dwr / interface / DemoFacade.js is dwr auto-generated javascript file that contains the corresponding class and method, dwr / engine.js is the core engine dwr script calls the client to deal with conversion, dwr / util.js contains tools to simplify the page handling functions.
To check the following as an example, look at the specific use dwr:
DemoAction:
public List query4dwr (int type, boolean needClear, HttpServletRequest request) (
if (needClear) request.getSession (). removeAttribute ( "dwr.demo.goodsId");
return demoFacade.queryList (type);
)
demo.jsp:
function updateResults () (
DWRUtil.removeAllRows ( "goodsbody");
var type = document.getElementById ( "type"). value;
DemoAction.query4dwr (type, true, fillTable);
)
function fillTable (goods) (
document.forms [0]. select.checked = false;
document.getElementById ( "totalRecords"). innerHTML = goods.length;
DWRUtil.addRows ( "goodsbody", goods, [addCheckbox, getName, getPrice, getCount]);
)
As noted, DemoAction.query4dwr (type, true, fillTable) can directly call the method DemoAction here is fillTable function, dwr callback function through the follow-up approach to deal with. Compare the action in the javascript methods and parameters, HttpServletRequest can not spread, dwr automatically added, and the other is on the last callback parameters, it is a better way, please refer to documents in other ways.
Finally, look at, dwr integration with Struts, the following code:
public ActionForward query (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception (
String type = request.getParameter ( "type");
/ / Or values from the form in
List goodsList = query4dwr (Integer.parseInt (type), true, request);
request.setAttribute ( "goodsList", goodsList);
return mapping.findForward ( "success");
)
public List query4dwr (int type, boolean needClear, HttpServletRequest request) (
if (needClear) request.getSession (). removeAttribute ( "dwr.demo.goodsId");
return demoFacade.queryList (type);
)
Struts previous method is the way, but do not support dwr, so click below to reconstruction methods can be called dwr.
In fact, the only method is to use the HttpServletRequest methods need reconstruction, if not to use HttpServletRequest, we can directly call the business layer of the type of method that is simple and convenient way, as follows:
demo.jsp:
function restore () (
DemoFacade.restore (updateResults);
)
DemoAction:
public synchronized void restore () (
goodsList.clear ();
initGoods ();
)
Summary
dwr package in the ajax client interaction and service modules, client service through a direct call to the method of simplified type of client and server interaction. Although this is also the default tag of similar components, but has a large extent, the development of simplified ajax.







