del.icio.us Digg DZone Reddit StumbleUpon
Make Web Services Transparent with Spring 2.5 and Apache CXF 2.0 - Willie Wheeler
« Previous | 1 | 2 | 3 | 4 | Next »

Creating the Client Application

This sample app is based on Spring MVC. For that we'll need to create a couple of MVC controllers, a couple of JSPs, a web.xml file, and a Spring application context file.

Spring MVC controller for viewing messages

First we'll create a Spring MVC controller called myapp.ViewMessagesController.

package myapp;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import contactus.ContactUsService;

@Controller
public final class ViewMessagesController {
    private ContactUsService contactUsService;
    
    public void setContactUsService(ContactUsService contactUsService) {
        this.contactUsService = contactUsService;
    }
    
    @RequestMapping("/viewmessages.do")
    public ModelMap viewMessages() {
        return new ModelMap("messages", contactUsService.getMessages());
    }
}

Since this is an article about Spring/CXF integration and not about Spring MVC, I won't go into the details of Spring MVC or annotation-based configuration. If however you are interested in learning more, please see my article Annotation-Based MVC in Spring 2.5.

At any rate, the code is easy enough to understand. We have a dependency injection method setContactUsService. Any implementation of ContactUsService could go in there, including the one we wrote in the previous article, contactus.ContactUsServiceImpl. In this case, though, we're going to use a CXF-generated dynamic proxy as shown below.

The viewMessages method simply grabs a list of messages from the ContactUsService and puts it in a model that the JSP will be able to see.

JSP for viewing messages

You will need to put this file at /WEB-INF/jsp/viewmessages.jsp in order for the request mapping to work as specified in ViewMessagesController above and myapp-servlet.xml below. For those who are unfamiliar with Spring's annotation-based MVC configuration, the @RequestMapping("/viewmessages.do") annotation maps the given path to the viewMessages method, and the view resolver in myapp-servlet.xml tells Spring to look inside /WEB-INF/jsp/ for the corresponding JSP.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
    <head>
        <title>View Messages</title>
    </head>
    <body>
        <h1>View Messages</h1>
        <ul>
            <c:forEach var="message" items="${messages}">
                <li>${message.lastNameFirstName} (${message.email}): ${message.text}</li>
            </c:forEach>
        </ul>
    </body>
</html>

This just displays a list of user messages. I'm using JSTL and JSTL EL. The ${messages} reference in the forEach tag refers to the list of messages that I placed in the ModelMap in ViewMessagesController.viewMessages above. Spring takes care of making the contents of the ModelMap available to the JSP.

web.xml

<?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_2_5.xsd"
    id="ws" version="2.5">
    
    <servlet>
        <servlet-name>myapp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>myapp</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

This just sets up the Spring MVC DispatcherServlet and tells the servlet container that the *.do extension mapping goes to the DispatcherServlet.

Now let's dig into the guts of it—the Spring application context.

Social bookmarks: del.icio.us Digg DZone Reddit StumbleUpon
« Previous | 1 | 2 | 3 | 4 | Next »
Show comments (15)

Post a comment

Your name:
Your e-mail address (won't be displayed):
Your web site (optional):
example: www.xyz.com
Your comment:
Please help us prevent comment spam:

What's New?

2008-10-20 - I've added a new mailing list feature to the site. Sign up to receive e-mail updates about new articles.
2008-09-30 - We've released chapter 4 (User registration) and chapter 5 (Authentication) of Spring in Practice.
2008-09-11 - By popular demand, I've added an RSS feed to the site.
Home | Consulting | Tech Articles | Mailing List | About | Contact | Spring Blog
Copyright © 2008 Wheeler Software, LLC.