初心者研究spring統合dwr検証


最近、自分でデータベース管理モジュールを作る練習をして、dwrを使ったので、研究してみました.レベルが限られているうえに、基礎を築いているので、初心者にとってはレンガを投げて玉を引く役割を果たしているのではないでしょうか.今回はデータソースtestを書きました.まず代コードを貼りましょう.dwr付きjarパッケージ1のwebを配置します.xmlファイル
<!--   dwr     -->
< servlet >
    < servlet-name > dwr </ servlet-name >
    < servlet-class > org.directwebremoting.spring.DwrSpringServlet </ servlet-class >
    < init-param >
        < param-name > debug </ param-name >
        < param-value > true </ param-value >
    </ init-param >
    < load-on-startup > 1 </ load-on-startup >
</ servlet >

< servlet-mapping >
    < servlet-name > dwr </ servlet-name >
    < url-pattern > /dwr/* </ url-pattern >
</ servlet-mapping > 
<!--      -->
   <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

  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      -->  
    <dwr:configuration></dwr:configuration><!--    configuration -->  
    <dwr:controller id="dwrController" debug="true" />  
    <!-- DWR      -->  
        
     <!-- spring bean   -->  
     <bean id="databaseService" class="com.dexs.service.impl.DatabaseServiceImpl">  
     </bean>  
       
     <bean id="testConnDB" class="com.dexs.bean.TestConnDB">  
        <property name="databaseService" ref="databaseService" />  
        <!--      js   -->  
        <dwr:remote javascript="TestConnDB"></dwr:remote>  
    </bean>  
    
   
    

</beans> 

  3.JAvaクラスDatabaseSerive.java
 
package com.dexs.service;

public interface DatabaseService{



/**
	 *            
	 * @param databaseModel
	 * @return     
	 */
	public boolean validateConnection(String driverName,String url,String userName,String password);




}

 4.JAvaクラスDatabaseSeriveImpl.java
package com.dexs.service.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.dexs.bean.SearchSql;
import com.dexs.service.DatabaseService;
import com.mchange.v2.c3p0.DriverManagerDataSource;

/**
 * @author Administrator
 *
 */
public class DatabaseServiceImpl implements DatabaseService {

		
	/**       
	 * 
	 * return boolean
	 * @throws ClassNotFoundException 
	 */
	@Override
	public boolean validateConnection(String driverName,String url,String userName,String password){
		// TODO Auto-generated method stub
		
		 System.out.println(driverName+url+userName+password);
			
							Connection conn = null;
			
							if(driverName == null || driverName.equals(""))
							{
								 System.out.println("1");
								 return false;
								

								 
							}
							if(url == null || url.equals(""))
									
							{ System.out.println("2");
								return false;
								 
							}
							
							if(userName == null || userName.equals(""))
							{
								 System.out.println("3");
								return false;
								 
							}
							if(password == null || password.equals(""))
							{
								 System.out.println("4");
								return false;
								 
							}
			 
				 
								try {
									 System.out.println("5");	
										Class.forName(driverName);
								 
									} 
								catch (ClassNotFoundException e)
								{
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							 	try 
							 	{
							 			 conn = DriverManager.getConnection(url, userName,password);
							 			 System.out.println("6");
										
									 
									
								} 
							 	catch (SQLException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							 	 
								finally{
									
									if(conn!=null){
										try {
											 System.out.println("7");
											conn.close();
											return true;
										} 
										catch (SQLException e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
										}
									}
									
								}
							 
		 
		 
										 
				
		 
			 
			
			return false;
		
	
	}
	
	
}
		

	


	





 5.JAvaクラス
package com.dexs.bean;

import com.ehualu.dexs.service.DatabaseService;

public class TestConnDB {
	
	   	private  DatabaseService databaseService;
	   
	   
		public void setDatabaseService(DatabaseService databaseService) {
		this.databaseService = databaseService;
	}


		public boolean ValidateConnection(String driverName, String url, String userName, String password)
		{
			return databaseService.validateConnection(driverName, url, userName, password);
		}
}

 
 6.jspページ
<script type="text/javascript" src="dwr/util.js"></script>
	<script type="text/javascript" src="dwr/engine.js"></script>
	<script type="text/javascript" src="dwr/interface/TestConnDB.js"></script>
	
		<script language="javascript">  
		
		function ValidateConnection()
		{
		  
				 //var databasename = document.getElementById("databaseName").value;	/       
				//var databasetype = document.getElementById("databaseType").value;	//       
				//var ip = document.getElementById("ip").value;						//     ip  
				//var port = document.getElementById("port").value;					//        
				//var str = ":"
				//var url = "jdbc:oracle:thin:@"+ip+str+port+str+databasename;
				
				 if(document.getElementById("depictName").value=="")
	 				{
						alert("          ");
						document.getElementById("depictName").focus()
  						return false;
					 }
				var url = document.getElementById("url").value;
				var driverName = document.getElementById("driverName").value;
				var userName = document.getElementById("userName").value;
				var password = document.getElementById("password").value;
				TestConnDB.ValidateConnection(driverName,url,userName,password,f);
			 
			} 
			 var f = function(testconn){
 				if(testconn){
					 
					 	alert("        ");
				}
 				else{ 	 
						alert("        ");
				}
			}
	</script>
	   

 
 
 
具体的なspringプロファイルはリストされていません.テストしました.問題ありません.