直接贴代码吧
1.jsp的代码
<script type="text/javascript" src="<%=base %>/dwr/util.js"></script> <script type="text/javascript" src="<%=base %>/dwr/engine.js"></script> <script type="text/javascript" src="<%=base %>/js/jquery-1.4.min.js"></script> <script type="text/javascript" src="<%=base %>/dwr/interface/databaseRemoteService.js"></script> <script type="text/javascript"> function ValidateConnection() { var DatabaseModel = {driverName:null,url:null,userName:null,password:null}; dwr.util.getValues(DatabaseModel); databaseService.validateConnection(DatabaseModel,function(flg){ if(flg==true){ alert("连接成功了"); } else{ alert("连接错误了");} }); } </script>
?
?
2.dwr.xml
?
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd" default-autowire="byName"> <dwr:configuration> <dwr:convert type="bean" class="com.dexs.model.DatabaseModel"></dwr:convert> </dwr:configuration> <bean id="databaseService" class="com.dexs.service.impl.DatabaseServiceImpl"> <dwr:remote javascript="databaseRemoteService"> <dwr:include method="validateConnection"/> </dwr:remote> </bean>
?3.web.xml
?
?
<!-- 配置 dwr servlet--> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class> <load-on-startup>3</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> </web-app>
?? 4.daoimpl.java
?
/**验证数据库连接 * * return boolean */ @Override public boolean validateConnection(DatabaseModel connectionDbModel) { // TODO Auto-generated method stub boolean flg = false; Connection conn = null; if(connectionDbModel != null){ try { Class.forName(connectionDbModel.getDriverName()); try { DriverManager.getConnection(connectionDbModel.getUrl(), connectionDbModel.getUserName(),connectionDbModel.getPassword()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ if(conn!=null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return true; } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return flg; }
?
?
?
?