Spring Static Pages Example

In this example show how to write a simple web based application using Spring MVC Framework, which can access static pages along with dynamic pages with the help of <mvc:resources> tag. To start with it, let us have working STS IDE in place and follow the following steps to develop a Dynamic Form based Web Application using Spring Web Framework:

Step 1: Create a Dynamic Web Project with a name SpringMVCStaticApp and create a package com.dineshonjava.controller under the src folder in the created project.

Step 2: Add below mentioned Spring 3.0 libraries and other libraries into the folder WebRoot/WEB-INF/lib

  • commons-logging-1.1.1.jar
  • org.springframework.asm-3.0.0.jar
  • org.springframework.beans-3.0.0.jar
  • org.springframework.context-3.0.0.jar
  • org.springframework.core-3.0.0.jar
  • org.springframework.expression-3.0.0.jar
  • org.springframework.web.servlet-3.0.0.jar
  • org.springframework.web-3.0.0.jar
  • spring-web.3.0.0.jar
 Step 3: Create a Java class WebStaticController under the com.dineshonjava.controller package.

Step 4: Create Spring configuration files Web.xml and sdnext-servlet.xml under the WebRoot/WEB-INF/ folder.

Step 5: Create a sub-folder with a name views under the WebRoot/WEB-INF folder. Create a view file welcome.jsp under this sub-folder and static.html under staticContents sub-folder.

Step 6: The final step is to create the content of all the source and configuration files name sdnext-servlet.xml under the sub-folder WebRoot/WEB-INF/config and export the application as explained below.  

Software versions used to run the sample code:

  • Spring 3.0
  • Java 1.6
  • Tomcat 7
  • JSTL 1.2
  • STS Java EE IDE
                                              Web Application Structure:
package com.dineshonjava.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

 * @author Dinesh Rajput
public class WebStaticController {
 @RequestMapping(value = "/welcome", method = RequestMethod.GET)
 public String welcome() {
  return "welcome";
 @RequestMapping(value = "/staticPage", method = RequestMethod.GET)
 public String redirect() {
  return "redirect:/staticContents/static.html";
Spring Web configuration file web.xml
<web-app id="WebApp_ID" version="3.0" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

another Spring Web configuration file sdnext-servlet.xml
<beans xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="

 <!-- Enable annotation driven controllers, validation etc... -->

<context:component-scan base-package="com.dineshonjava.controller">

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
 <property name="prefix" value="/WEB-INF/views/"></property>
 <property name="suffix" value=".jsp"></property>
<mvc:resources location="/WEB-INF/staticContents/" mapping="/staticContents/**">
Following is the content of Spring view file welcome.jsp. This will be a landing page, this page will send a request to access redirect service method which will redirect this request to another service method and finally a static.html page will be displayed.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <title>Spring Landing Page</title>
Spring Landing Pag</h2>
Click below button to get a simple HTML page
<form:form action="/sdnext/staticPage" method="GET"><table><tbody>
<tr>     <td><input type="submit" value="Get HTML Page" />      </td>     </tr>
Following is the content of Spring view file static.html. This is the final redirected page.
    <title>Spring Static Page</title>

A simple HTML page

</body> </html>
Once you are done with creating source and configuration files, export your application. Right click on your application and use Export > WAR File option and save your SpringMVCStaticApp.war file in Tomcat's web apps folder.

Now start your Tomcat server and make sure you are able to access other web pages from web apps folder using a standard browser. Now try a URL http://localhost:8080/sdnext/welcome and you should see the following result if everything is fine with your Spring Web Application:

Now click on "Get HTML Page" button to submit the form and to get final redirected static page. You should see the following result if everything is fine with your Spring Web Application:


No comments:

Post a Comment