Home > Drools, Java, Spring > Drools 6 Decision Table with Spring MVC

Drools 6 Decision Table with Spring MVC

Drools is the Java Rules engine from Jboss. This article covers on how to use the Drools Decision Table using excel sheets with a Spring MVC project.

To start with, create a basic Spring MVC project using Spring STS. Add the following dependencies for drools on the pom file generated by the template in STS.


Create a spring-context.xml file in the following location /WEB-INF/spring with the following content

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://drools.org/schema/kie-spring http://drools.org/schema/kie-spring.xsd">

<kie:kmodule id="kmodule1">
 <kie:kbase name="kbase1" packages="com.codesilo.wordpress.rules">
 <kie:ksession name="ksession1" type="stateless" />

<beans:bean id="kiePostProcessor"
 class="org.kie.spring.KModuleBeanFactoryPostProcessor" />


The package here “com.codesilo.wordpress.rules” denotes the location where the decision tables are located. We will create that one we have created the POJO.

Add the /WEB-INF/spring/spring-context.xml on the contextConfigLocation in web.xml. The contextConfigLocation should now look like


We create the POJO named as RouteAttribute in the following package com.wordpress.codesilo.model. This object will be used as the fact for the rules defined,

package com.wordpress.codesilo.model;

public class RouteAttribute {

private String country;
 private String state;
 private boolean newEntry;
 private String ansOnRule;

public String getCountry() {
 return country;

public void setCountry(String country) {
 this.country = country;

public String getState() {
 return state;

public void setState(String state) {
 this.state = state;

public boolean isNewEntry() {
 return newEntry;

public void setNewEntry(boolean newEntry) {
 this.newEntry = newEntry;

public String getAnsOnRule() {
 return ansOnRule;

public void setAnsOnRule(String ansOnRule) {
 this.ansOnRule = ansOnRule;

Now we create the decision table (Rules.xls) on the following location com.codesilo.wordpress.rules


The file can also be downloaded from here.

As a last step, we change HomeController.java to fire the rules. This can be later moved to any controller/MVC flow. We inject the KieBase object by the following..

private KieBase kieBase;

Add the following in the default method on the HomeController class.

RouteAttribute routingComponent = new RouteAttribute();


StatelessKieSession statelessKieSession =  kbase.newStatelessKieSession();


Now, if we bring up the server, the default method is going to run and call the rules from above.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: