Archive

Posts Tagged ‘Spring MVC’

Drools 6 Decision Table with Spring MVC

June 8, 2015 Leave a comment

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.


<dependency>
 <groupId>org.drools</groupId>
 <artifactId>drools-core</artifactId>
 <version>6.2.0.Final</version> 
 </dependency>
 <dependency>
 <groupId>org.drools</groupId>
 <artifactId>drools-compiler</artifactId>
 <version>6.2.0.Final</version> 
 </dependency>
 <dependency>
 <groupId>org.kie</groupId>
 <artifactId>kie-spring</artifactId>
 <version>6.2.0.Final</version> 
 </dependency>
 <dependency>
 <groupId>org.kie</groupId>
 <artifactId>kie-api</artifactId>
 <version>6.2.0.Final</version> 
 </dependency>
 <dependency>
 <groupId>org.kie</groupId>
 <artifactId>kie-internal</artifactId>
 <version>6.2.0.Final</version> 
 </dependency>

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"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:kie="http://drools.org/schema/kie-spring"
 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" />
 </kie:kbase>
 </kie:kmodule>

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

</beans:beans>

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


<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/spring/root-context.xml
 /WEB-INF/spring/root-context.xml</param-value>
 </context-param>

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;
 
 //Result
 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

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..

@Autowired
private KieBase kieBase;

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


RouteAttribute routingComponent = new RouteAttribute();

routingComponent.setCountry("US");

StatelessKieSession statelessKieSession =  kbase.newStatelessKieSession();

statelessKieSession.execute(routingComponent);

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

Advertisements

Spring MVC with Sitemesh 3

July 11, 2013 3 comments

In one of my earlier posts I showed some steps to create a project with Struts Tiles. This post mentions the steps to get a basic Spring MVC project running with Sitemesh 3. Sitemesh is a decorator framework. More information on Sitemesh 3 can be found here.

CREATING A BASIC SPRING MVC PROJECT

We will use the Spring Developer Toolsuite and Maven 3 for our project. Go to the toolsuite workspace and select File>New>Spring Template Project and choose Spring MVC Project from the list. Click yes on the download prompt. We will name the project as Sitemesh-SpringMVC and the package as com.wordpress.codesilo.

SETTING UP SITEMESH 3

Open the pom.xml of the Project and add the following dependency.

<dependency>
 <groupId>org.sitemesh</groupId>
 <artifactId>sitemesh</artifactId>
 <version>${sitemesh.version}</version>
</dependency>

Also add the following under properties

<sitemesh.version>3.0-alpha-2</sitemesh.version>

To the web.xml add the following

<filter>
 <filter-name>sitemesh</filter-name>
 <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
</filter>
<filter-mapping>
 <filter-name>sitemesh</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>

Now create a sitemesh3.xml file in the web-inf folder of the project and add the decorator files mapping in the file like follows. We will create the decorator files next.

<sitemesh>
 <mapping path="/*" decorator="/WEB-INF/decorators/defaultDecorator.jsp"/>
 <mapping path="/user" decorator="/WEB-INF/decorators/userDecorator.jsp"/>
</sitemesh>

Create the decorators folder in your project and add the following  jsp files.

defaultDecorator.jsp

<html>
 <head>
 <title><sitemesh:write property='title'/></title>
 <sitemesh:write property='head'/>
 </head>

 <body>
 This is the default body in decorator:
 <sitemesh:write property='body'/>
 </body>
</html>

userDecorator.jsp

<html>
 <head>
 <title><sitemesh:write property='title'/></title>
 <sitemesh:write property='head'/>
 </head>

 <body>
 This is the decorator body in user:
 <sitemesh:write property='body'/>
 </body>
</html>

For the default decorator example we will use the home.jsp that was created as a part of the project template. For the user decorator, we will create a new view in the views folder. We will keep it very simple as follows.

user.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
 <title>User</title>
</head>
<body>
<h1>
 Hello User!
</h1>

<P> Welcome user ! </P>
</body>
</html>

We will also change the HomeController (created again as a part of the template) to add another method for the user request mapping. We will copy the method for the default request mapping and change it as follows. (Add the following code to the HomeConrtoller)

@RequestMapping(value = "/user", method = RequestMethod.GET)
public String user(Locale locale, Model model) {
 logger.info("Welcome home! The client locale is {}.", locale);
 return "user";
}

Now, if we run the application and try the following urls we will see the different results. Notice the content added by the decorators.

http://localhost:8080/codesilo/

SitemeshImage1

http://localhost:8080/codesilo/user

SitemeshImage2

ADDING POST TO SITEMESH3

I read somewhere that Sitemesh 3 had a bug that did not allow it to use POST on the requests. I tried to give it a shot. We will add a field on the default view to accept a name. We will send that as a POST to the user method and display that on the user view.

Here is User.java class.

package com.wordpress.codesilo;

public class User {

private String name;

public String getName() {
 return name;
 }

public void setName(String name) {
 this.name = name;
 }

}

The HomeController now looks like this:

 @RequestMapping(value = "/", method = RequestMethod.GET)
 public ModelAndView home(Locale locale, Model model) {
 logger.info("Welcome home! The client locale is {}.", locale);

 Date date = new Date();
 DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

 String formattedDate = dateFormat.format(date);

 model.addAttribute("serverTime", formattedDate );

 return new ModelAndView("home", "command", new User());
 }

 @RequestMapping(value = "/user", method = RequestMethod.POST)
 public String user(Locale locale, @ModelAttribute("SpringWeb")User user, Model model) {
 logger.info("Welcome " + user.getName());
 model.addAttribute(user);
 return "user";
 }

We will change the home.jsp to

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ page session="false" %>
<html>
<head>
 <title>Home</title>
</head>
<body>
<h1>
 Hello world!
</h1>

<P> The time on the server is ${serverTime}. </P>

<form:form method="POST" action="/codesilo/user">
 <form:input path="name" />
 <input type="submit" value="submit"/>
</form:form>

</body>
</html>

And, change user.jsp to

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
 <title>User</title>
</head>
<body>
<h1>
 Hello User!
</h1>

<P> Welcome ${user.name} ! </P>
</body>
</html>

Now if we call the urls we will see this..

http://localhost:8080/codesilo/

SitemeshImage3

Once we hit submit we will get the following

SitemeshImage4

It seems that the POST works too. Maybe it’s some specific case that I have not run into yet.