Spring Book – Chapter 11 – Spring Web

Spring Security

Spring security is a Java/Java EE framework that provides flexible and powerful authentication, authorization and other security features for an enterprise application. It has been ported to other platforms (such as Python and Microsoft .NET) and represents a popular choice in developing applications in many business critical domains.

The project initially was branded as “Acegi Security” in later 2003 with Ben Alex leading the charge. The first release happened in March 2004, where it was publicly released under Apache license. Later on this got incorporated into Spring portfolio as Spring security and grew into the official Spring sub-project. Under the new name, Spring Security released its first version in April 2008 with version 2.0.0.

We will be covering Spring Security in detail in Chapter 15.

Spring JavaScript

Spring JavaScript is a lightweight abstraction over common JavaScript toolkits such as Dojo. It aims to provide a common client-side programming model for progressively enhancing a web page with rich widget behavior and Ajax remoting. It is packaged along with Spring Web Flow 2 distribution. Spring JavaScript can work with any server-side framework. The Web Flow 2 distribution includes convenient integration between Spring JavaScript and Spring Web MVC for processing Ajax requests.

Dojo is a collection of JavaScript utilities providing solutions for a vast array of problems faced by the professional JavaScript developer.

The basic goals for Spring JavaScript can be summarized as follows:

  • Simplify and encapsulate use of Dojo JavaScript toolkit to be used for common enterprise application use cases. Client-side validations and usage of Ajax are examples of such use case.
  • Promote progressive enhancement whereby it caches on maximum number of potential users and makes the web page more accessible. It also makes the application more robust as opposed to having a web page with conventional JavaScript during failure.

Progressive enhancement is a strategy employed in web design which employs using web technologies in a layered fashion which allows all users to access the basic content and functionality of a web page using any browser or Internet connection, while also providing an enriched version of the same page to those users having advanced browser software or better bandwidth.

In addition to features listed above, Spring JavaScript has other value additions coming along with it. It has a servlet, org.springframework.js.resources.ResourceServlet which can be used for serving static resources like JavaScript and CSS files from within a jar a well as from web application root directory. Listing 11-1 shows declaration of ResourceServlet in your web applications web.xml file. It also has a CSS framework which allows to structure common page layouts in your application.

Listing 11-1. ResourceServlet declaration in web.xml file

[xml]<servlet>

<servlet-name>Resource Servlet</servlet-name>

<servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>

</servlet>

<!– Map all /resources requests to the Resource Servlet for handling –>

<servlet-mapping>

<servlet-name>Resource Servlet</servlet-name>

<url-pattern>/resources/*</url-pattern>

</servlet-mapping>[/xml]

Listing 11-2 shows what needs to be done in your web application page so as to use Spring JavaScript. You need to include the public API, Dojo toolkit and API implementation.

Listing 11-2. Usage of Spring JavaScript in web pages

[xml]<!– Public API –>

<script type="text/javascript" src="/resources/spring/Spring.js"></script>

<!– Dojo toolkit javascript declaration –>

<script type="text/javascript" src="/resources/dojo/dojo.js"></script>

<!– API implementation declaration –>

<script type="text/javascript” src="/resources/spring/Spring-Dojo.js"></script>[/xml]

You can use the API to decorate all the HTML elements in your web applications. There are various types of decoration available. They are:

  • WidgetDecoration – Listing 11-3 shows adding decoration to an input field by prompting message when in focus.

Listing 11-3. Adding decoration to an input field

[html]<form:input id="origin" path="origin"/>

<script type="text/javascript">

Spring.addDecoration(new Spring.ElementDecoration({

elementId: "origin",

widgetType: "dijit.form.ValidationTextBox",

widgetAttrs: { promptMessage : "Search origin by city name, airport

name, IATA city code or IATA airport code." }}));

</script>[/html]

  • AjaxEventDecoration – Listing 11-4 shows decorating using AjaxEventDecoration which can be used for pagination in your application.

Listing 11-4. Usage of AjaxEventDecoration

[html]<form:input id="searchCriteria" path="searchCriteria"/>

<a id="backwardLink"

href="callsearch?searchCriteria=${criteria.searchCriteria}&

page=${criteria.page – 1}">Prev</a>

<a id="forwardLink"

href="callsearch?searchCriteria=${criteria.searchCriteria}&

page=${criteria.page + 1}">Next</a>

<script type="text/javascript">

Spring.addDecoration(new Spring.AjaxEventDecoration({

elementId: "backwardLink",

event: "onclick",

params: { fragments: "body" }

}));

Spring.addDecoration(new Spring.AjaxEventDecoration({

elementId: "forwardLink",

event: "onclick",

params: { fragments: "body" }

}));

</script>[/html]

  • ValidateAllDecoration – Listing 11-5 shows adding decoration to a submit button having rich validation behaviour and would like to prevent used from going forward until all validations are satisfied.

Listing 11-5, Usage of ValidateAllDecoration

[html]<input type="submit" id="submit" name="_eventId_submit" value="Submit" />

<script type="text/javascript">

Spring.addDecoration(new Spring.ValidateAllDecoration({ elementId:’submit’, event:’onclick’ }));

</script>[/html]

Page Visitors: 2237

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

1 thought on “Spring Book – Chapter 11 – Spring Web

Leave a Reply

Your email address will not be published. Required fields are marked *