Friday, April 19, 2013

Spring MVC 3.1 and JBOSS 7.1.1


Create “Hello Spring !!!” Using  Spring MVC 3.1.2 and JBOSS 7.1.1  

Step 1:
Download Spring 3.1.2 jar from below link
 Download JBOSS 7.1.1 Final  from
 For installation of JBOSS on windows 7 please refer my previous post
Require Eclipse Java EE IDE for Web Developers. Version: Indigo Service Release 2

Step 2:

Open Eclipse IDE and Create New Dynamic Web project “Springmvcapp” . Copy all Spring Jar to lib directory of Web Content and Also add jstl.jar and common-logging.jar  (Download from java2s.com or maven repository)

 
Note:  Important is we need spring 3.1.2 jar for JBOSS 7.1.1 
If we try with spring 3.0.1 over JBOSS 7.1.1 following Error will throw at the time of Deployment
java.lang.NoClassDefFoundError: org/jboss/virtual/VirtualFileVisitor
To avoid this error we need to use Spring 3.1.2 jars over JBOSS 7.1.1 

Step 3:
Go to src folder and create package com.mahavirdhat.org.controller
And create WelcomeController.java

 
package com.mahavirdhat.org.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class WelcomeController {
      
       @RequestMapping("/hello.html")
       public ModelAndView helloFunction(){
              ModelAndView mav = new ModelAndView("hello");
           mav.addObject("hello", "Hello Spring!");
              return mav;
       }
      
}

 

Step 3: 

Create  index.jsp copy  into  WebContent  folder  of project
webcontent/index.jsp
From this  Page we can  redirect to hello.jsp  (hello.html  because *.html is configure in web.xml )

Index.jsp
--------------------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome To MVC Example </title>
</head>
<body>
 <a href="hello.html">Say Welcome </a>
</body>
</html>
---------------------------------------------------------------------------------------------------


Step 3:
Create  hello.jsp  inside jsp folder of WEB-INF
WEB-INF/jsp/hello.jsp
Hello.jsp
-------------------------------------------------------------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring MVC </title>
</head>
<body>
   ${hello}
</body>
</html>
--------------------------------------------------------------------------------
$(hello) is used because  in WelcomeController.java   helloFunction()
Return object is ModelandView   created with name of “hello”, because hello.jsp is present in WEB-INF/jsp folder .
Name of ModelandView  same name of jsp should be present in jsp folder of web-inf
Other way is that instead of ModelandView  we can redirect view from controller.
Step 4:
Create Web.xml  inside WEB-INF folder
------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 
   <display-name>Spring3MVC</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      
    </welcome-file-list>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
</web-app>
-----------------------------------------------------------------------------------------
Above file showing servlet name is spring  and mapping is *.html
So we are redirecting hello.html from index.jsp otherwise we can directly open

Step 5:  create spring-servlet.xml
In Web.xml dispatcher servlet name is spring so we need give web application context file name should be spring-servlet .xml (for more details Read MVC document from springsource.org)
Spring-servlet.xml will load by web server so need to add contextloadlistener into web.xml
To load Other spring xml files than spring-servlet.xml we need to add contextloadlistner and context param inside web.xml.
File Name : spring-servlet.xml
------------------------------------------------------------------------------------------
<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan
        base-package="com.mahavirdhat.org.controller" />
        
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>
------------------------------------------------------------------------------------------------------
Step 6:
Compile application using eclipse and create “springmvcapp.war” file using WAR export of eclipse
 Copy springmvcapp.war file into deployment folder of JBOSS 7.1.1
C:\jboss-as-7.1.1.Final\standalone\deployments
You can start JBOSS server if it is not started already.
Step 7:
Go to browser and hit following url





 




10 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thanks. Worked perfect. One small typo -> Hello.jsp should have been hello.jsp

    ReplyDelete
  3. List of the Top Casinos with Games by Casino Sites
    Best Casinos with Slots by 인스타 셀럽 Casino Sites 프리 벳 · 1. w88 com login BitStarz Casino – Top Review 2021 – 100% up 오프 후기 to €/$600 배팅사이트 Welcome Bonus · 2. SlotoCash – Best Overall Casino

    ReplyDelete