SolveITProblems is a project of intellectual club

SolveITProblems это проект интеллектуального клуба

вторник, 2 апреля 2013 г.

Maven (install-file - adding library) + Spring MVC + FTL (Freemarker) + JDBC. Step By Step

So,  its time to "select" from database and using freemarker templates. We need 3 steps for ftl and 5 steps for jdbc. Lets begin from easyest - adding freemarker templates:

Step 1.

Adding dependancy to pom.xml


Step 2.

Add a new view resolver to our spring xml configuration:

  <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
    <property name="templateLoaderPath" value="/WEB-INF/templates/"/>  
   <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
    <property name="cache" value="true"/>  
    <property name="prefix" value=""/>  
    <property name="suffix" value=".ftl"/>  

/WEB-INF/templates - its a place where you store your ftl templates.

Step 3.

Thats it! Third step is adding ftl file to the templates directory. At our first topic (Maven + Spring MVC) about Spring we use jsp for view, here we have the same scenario but with ftl templates.

OK. Lets go to the JDBC. we need to run a "select".

Step 1.

Step 1 is the easyest :) Adding dependancy to pom.xml:


We haven't here dependancy for database driver. You should add it manually because each driver has its own group, artifact and version. But if you are going to use oracle, you should take a look to step 2.

Step 2.

You can't find Oracle db jdbc driver at maven repositories, so we need to add it manually to the maven. Download the jar file and run the following command:

 mvn install:install-file -Dfile=c:\pathToMyLib.jar -DgroupId=com.example.code   
 -DartifactId=example -Dversion={version} -Dpackaging=jar  

Step 3.

Spring uses DAO pattern. So we should create DAO interface and DAO Implementation.

 package com.azercell.web.restvcc.dao;  
 import java.util.List;  
 public interface TestDao {   
    public List getList();  
 package com.azercell.web.restvcc.dao.impl;  
 import com.azercell.web.restvcc.dao.TestDao;  
 import java.sql.Connection;  
 import java.sql.ResultSet;  
 import java.util.ArrayList;  
 import java.util.List;  
 import javax.sql.DataSource;  
 import org.springframework.beans.factory.annotation.Autowired;  
 public class TestDaoImpl implements TestDao{  
   private DataSource dataSource;  
   public void setDataSource(DataSource dataSource)  
   public List getList() {  
     ArrayList list=new ArrayList();  
     try {  
       Connection con = dataSource.getConnection();        
       ResultSet rs = con.createStatement().executeQuery("select * from (select * from example) where rownum < 15");  
       while (  
     } catch (Exception e) {  
       System.out.println("Fuck :) "+e);  
     return list;  

So, our classes are ready to run the query.

Step 4.

The results of query will be shown by our ftl file through our controller. Lets prepare it:

 package com.azercell.web.restvcc.controllers;  
 import com.azercell.web.restvcc.dao.TestDao;  
 import com.azercell.web.restvcc.dao.impl.TestDaoImpl;  
 import java.util.ArrayList;  
 import java.util.Iterator;  
 import java.util.List;  
 import javax.annotation.Resource;  
 import javax.sql.DataSource;  
 import org.springframework.beans.factory.annotation.Autowired;  
 import org.springframework.jdbc.core.JdbcTemplate;  
 import org.springframework.stereotype.Controller;  
 import org.springframework.web.bind.annotation.RequestMapping;  
 import org.springframework.web.servlet.ModelAndView;  
 public class TestController {  
   private TestDao testBean;  
   public void setTestBean(TestDao testBean)  
   @RequestMapping(value = "test")  
   public ModelAndView test() {  
     ModelAndView mav = new ModelAndView();     
     mav.addObject("listit", testBean.getList());      
     return mav;  

To show our results in ftl a couple of lines will be enough:

 <#list listit as user>  

Step 5.
Configure the spring and run it:

  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"  
       p:driverClass="oracle.jdbc.driver.OracleDriver" p:jdbcUrl=""  
       p:user="subscription" p:password="subscription"     
   <bean id="testController" class="com.azercell.web.restvcc.controllers.TestController" >      
     <property name="testBean" ref="test" />  
   <bean id="test" class="com.azercell.web.restvcc.dao.impl.TestDaoImpl" >      
     <property name="dataSource" ref="dataSource" />  

Its not a full configuration file. Here 3 pieces of code: 1. - its our datasource (connection pool) 2. - our controller that gets a DAO 3. - our DAO implementation that gets a dataSource Ready :) .

1 комментарий:

  1. If you're using netbeans and embedded maven, better use netbeans own method:

    Project -> Dependencies -> (RightClick) Add Dependency (fill fields and OK) -> Then find the artifact in list, press Right Click and select - Manually install artifact.