CodeIn House

  • Laravel
  • WordPress
  • jQuery
  • Javascript
  • Contact
Home   JAVA   How to Integrate JSF 2 + Spring JDBC Template Using Maven ?

How to Integrate JSF 2 + Spring JDBC Template Using Maven ?

February 10, 2018 by SNK

Integrate JSF 2 + Spring JDBCTemplate With Maven in JAVA

Today in this tutorial we are going to integrate jsf 2 and spring jdbc template while jsf 2 will be used for frontend with rich faces and spring jdbc to read and write operation and to perform CRUD.

We are going to use maven and pull dependencies we want from maven repository. So, it will be the maven project. We are also going to use jetbrains intellij IDEA (IDE) for this project.

Lets get started,

Step 1

  1. File -> New Project -> Maven Project and tick archetype button.
  2. Scroll down to org.richfaces.archetypes:richfaces-archetype-simpleapp and expand.
  3. Select richfaces-archetype-simpleapp:4.5.17.Final and press next.

 

JSF 2.0 With Richfaces With Maven

project_name

project setup step two

I have chosen version 4.5.17.Final for my project because i was having trouble with version 5 as maven was not able to setup my project completely.

You may try different version, that might work with you. Give your project name and proceed until finish.

Step 2

Go to line : 136 by pressing control + G after that add these spring dependencies just after the line : 136.

pom.xml
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-jdbc</artifactId>
     <version>4.1.5.RELEASE</version>
</dependency>
 
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.1.5.RELEASE</version>
</dependency>
 
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.1.5.RELEASE</version>
</dependency>
 
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>4.1.5.RELEASE</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>
 
        
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.1.5.RELEASE</version>
</dependency>
 
 
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.44</version>
</dependency>

If you have selected Auto-Import then it will start downloading all the spring dependency in your maven libraries.

downloaded spring dependencies

Also create new database with MySQL workbench or through command line.

Step 3

1. Create new file named AppConfig.java inside <project_root>/src/main/java/<project_name> and paste this code inside it.

AppConfig.java
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package jsf_springjdbc;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
 
@Configuration
@ComponentScan(basePackages = "jsf_springjdbc")
public class AppConfig {
 
    @Bean
    public DataSource getDataSource() {
        DriverManagerDataSource connectionSource = new DriverManagerDataSource();
        connectionSource.setDriverClassName("com.mysql.jdbc.Driver");
        connectionSource.setUrl("jdbc:mysql://localhost:3306/springjdbc_db");
        connectionSource.setUsername("root");
        connectionSource.setPassword("");
 
        return connectionSource;
    }
}

2. Now, we need to create spring bean configuration file. Create new file inside <project_root>/src/main/webapp/WEB-INF called applicationContext.xml and paste these codes below :-

applicationContext.xml
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
 
 
    <context:component-scan base-package="jsf_springjdbc" />
</beans>

Now, comes the view resolver tag. Open faces-config.xml which is inside your <project_root>/src/main/webapp/WEB-INF and paste this code between <faces-config> tag.

faces-config.xml
XHTML
1
2
3
<application>
    <el-resolver> org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>

Finally, the context listener. Open up web.xml file inside <project_root>/src/main/webapp/WEB-INF and these line of codes just below the <display-name> tag.

web.xml
XHTML
1
2
3
4
5
6
7
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 
<listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>

Finally, to use JdbcTemplate template in your class file. Open RichBean.java and use the JdbcTemplate like the image below :-

Using Spring JDBC Template With DataSource

Also, your final directory structure will look like this.

Complete Directory Structure

This is it, If you like this tutorial thumbs up and comment if any problem.

SHARE ON
Buffer

Enjoyed this article?

Like us on

JAVA jsf and spring jdbc template integration spring jdbc and jsf using maven spring jdbc template with jsf

Avatar for SNK

About SNK

Hello Welcome to my Blog. I develop Websites Using Laravel Framwork & WordPress. Get Latest updates on Facebook | Twitter

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get Connected !! With Us

TOP POSTS

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

TUTORIALS TILL DATE

  • September 2022 (6)
  • June 2021 (7)
  • October 2020 (5)
  • September 2020 (6)
  • September 2018 (14)
  • August 2018 (3)
  • July 2018 (4)
  • March 2018 (8)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (2)
  • August 2017 (8)
  • May 2017 (1)
  • April 2017 (1)
  • March 2017 (4)
  • February 2017 (3)
  • January 2017 (4)

CATEGORIES

  • Angular (2)
  • CSS3 (3)
  • D3 (3)
  • HTML5 (7)
  • JAVA (11)
  • Javascript (20)
  • jQuery (8)
  • Laravel (35)
  • Others (3)
  • PHP (11)
  • Spring (2)
  • WordPress (10)

Top Categories

  • Angular
  • CSS3
  • D3
  • HTML5
  • JAVA
  • Javascript
  • jQuery
  • Laravel
  • Others
  • PHP
  • Spring
  • WordPress

Get in Touch

DMCA.com Protection Status

Recent Articles

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

© 2012-22 CodeIn House.  •  All Rights Reserved.