问题
I am using dwr for first time should I use dwr ? If yes the How to use DWR(Direct web remoting) in Spring MVC 4. If not then what to use?
回答1:
Yes, you can use DWR on spring project for Ajax kindof purpose. It can be easily implemented with one controller file and with xml file. All we have to do is call the method which is in controller from your script file.
回答2:
create Bean on dispatch servlet
<bean id="dwrBean"
class="dwrController">
<property name="myDAO" ref="MyDAOImpl" />
</bean>
Then create dwr.xml file.. and place the below code
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://directwebremoting.org/schema/dwr30.dtd">
<dwr>
<init>
<converter id="array" class="org.apache.commons.beanutils.converters.*"/>
</init>
<allow>
<create creator="new" javascript="MyDWRController">
<param name="class" value="com.kgisl.rhb.authenticate.service.AutheticateAjaxController"/>
</create>
//Give Your Object which are ti be returned using dwr
<convert converter="bean" match="MyBean" javascript="BeanVO"/>
</allow>
</dwr>
And Create Controller Class.
@RemoteProxy
@ Component
public class AutheticateAjaxController {
@Autowired
private static MyDAO dao;
@RemoteMethod
public List<BeanVO> fnGetCurrentDate() {
//Blah Blah Blah
return list;
}
}
And On your jsp
<script type="text/javascript"
src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript"
src="<%=request.getContextPath()%>/dwr/util.js"></script>
/*This File will be automatically generated by DWR*/
<script type="text/javascript"
src="<%=request.getContextPath()%>/dwr/interface/DWRController.js"> </script>
/*On your js Funtion*/
function fnGetCurrentDate() {
DWRController.getCurrentDate(fnLoadDate);
}
/*DWR Auto generated method*/
function fnLoadDate(data) {
var dates = data.split("-");
datepicker.value(new Date(dates[2], dates[1] - 1, dates[0]));
}
And Dont Forget to add dwr servlet on web.xml
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.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>
来源:https://stackoverflow.com/questions/31403846/how-to-use-dwrdirect-web-remoting-in-spring-mvc-4