Wednesday, June 25, 2014

Basics of Spring MVC with JBoss 7.1

Using JBoss 7.1.1 as the Application server.

Following files of concern for basic debugging

1. \jboss\jboss-as-7.1.1.Final\standalone\configuration\standalone.xml -->  This files contains the EE configurations files.

Use  deployment scanner to assign the different war,ear locations to be loaded for Jboss deployment.

Like the below contents has two deployment scanner for two different web apps
a.) One app1*.war resides in the standalone\deployments dir of JBoss home
b.) Another app2*.war resides in the eclipse runtime jboss tools dir. This dir is automatically chosen by eclipse when deploying an app on JBoss server.
To configure a Jboss sever with eclipse ( kepler ) : Add the url : to eclipse help-->New software-->works with

Then in eclipse preferences-->server add the Jboss7.1.* Runtime.
Then eclipse ''servers'' view perspective add the desired web app to the server

<subsystem xmlns="urn:jboss:domain:deployment-scanner:1.1">
            <deployment-scanner name="customscaner" path="deployments" relative-to="jboss.server.base.dir" scan-interval="5000" deployment-timeout="120"/>
            <deployment-scanner name="jbosstoolsscanner1" path="E:\workspace\.metadata\.plugins\\JBoss_7.1_Runtime_Server1403588064851\deploy" scan-interval="5000" deployment-timeout="120"/>

Use domain logging to configure the different log levels and the log files. 

The below is the snippet from standalone.xml

<subsystem xmlns="urn:jboss:domain:logging:1.1">
            <console-handler name="CONSOLE">
                <level name="INFO"/>

Springs Context and Web Context Configurations

Sample web.xml

<?xml version="1.0" encoding="UTF-8"?>


contextConfigLocation parameter values gives the path of the spring context file to be loaded by the dispatcher servlet.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
<context:component-scan base-package="com.goraksh" />
<mvc:annotation-driven />
<bean id="userDAO" class="com.goraksh.UserDAO">
        <property name="dataSrc" ref="userDataSrc"/>
<bean id="userDataSrc" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!--  max active connections by default is 8 -->
        <property name="driverClassName" value="${}"/>
        <property name="url" value="${}"/>
        <property name="username" value="${}"/>
        <property name="password" value="${}"/>
<bean id="securityTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSrc" ref="userDataSrc"/>
<context:property-placeholder location=""/>

Certains subtleties for new bees
Dispatcher servlet can be used only for the servlet and jsp files residing in the WEB-INF directory.
For the static resources like static htmls, css, js, etc. Dispatcher servlet mapping will not work.

So if I change from Url pattern to <url-pattern>/*</url-pattern> instead of <url-pattern>/user/*</url-pattern>

and removes the lines <mvc:default-servlet-handler/> from the spring-context.xml then an static html file, say hello.html, residing directly inside the web app context but outside the WEB-INF directory will not be loaded. The logs will report the the desired Dispatcher servlet could not find the mapping for ..../hello.html. !!!

To get away with this problem, there are multiple approaches

1. Give a more precise URI for View classes to handled by Dispatcher servlet. Hence the need for /user/*. So all URIs contains ''user'' alone will be handled by Dispatcher servlet.

2. To handler the static components, which cannot be loaded by Dispatcher, I have used
<mvc:default-servlet-handler/> in the spring-context.xml

3. This is an an alternate to step 2
Instead of step#2 , you can also use <mvc:resources mapping="/*" location="/" /> in the spring-context.xml. <mvc:resources mapping="/*" location="/" /> will replace the line <mvc:default-servlet-handler/>. This step is workable only for spring version 3.0.4 and above.

See the Stack Over Discussion