|
|
||
Simon Brown's BlogComparing webapp frameworks : WebWorkPosted by simongbrown on March 24, 2006 at 03:02 PM | Comments (5)Like Struts, WebWork is a framework that is fairly established within the J2EE webapp space although it's interesting that I've only ever come across two types of WebWork users - those that have never heard of it and those that love it. WebWork, like most other frameworks, is designed around the web MVC pattern and uses the command and controller implementation strategy. What's slightly different about WebWork is that it's built on top of XWork, a separate framework that provides an implementation of the command pattern that is independent of the Servlet API.
In XWork, actions (or commands, depending on your terminology) implement the
Right, let's get on with seeing how a WebWork implementation of the sample application compares to the others. I'm using WebWork 2.1.7 and installing it is a simple matter of copying a few JAR files into the <servlet> <servlet-name>webwork</servlet-name> <servlet-class>com.opensymphony.webwork.dispatcher.ServletDispatcher</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>webwork</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <taglib> <taglib-uri>webwork</taglib-uri> <taglib-location>/WEB-INF/lib/webwork-2.1.7.jar</taglib-location> </taglib>
Two additional configuration files that we need are called <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
<validators>
</validators>
The other file ( Home page The home page presents the user a list of recent blog entries and with WebWork, the code that finds these blog entries get wrappeds up inside an XWork action as follows. package action;
import com.opensymphony.xwork.ActionSupport;
import domain.Blog;
import domain.BlogService;
/**
* Action responsible for finding blog entries, ready to be displayed.
*
* @author Simon Brown
*/
public class ViewBlogEntriesAction extends ActionSupport {
/** the blog that owns the blog entries */
private Blog blog;
/**
* Performs the processing associated with this action.
*
* @return a String defining the outcome of this action
*/
public String execute() throws Exception {
BlogService blogService = new BlogService();
this.blog = blogService.getBlog();
return SUCCESS;
}
/**
* Gets the blog that this action is operating upon.
*
* @return a Blog instance
*/
public Blog getBlog() {
return blog;
}
}
This implementation is very straightforward, just looking up the <!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-1.0.dtd">
<xwork>
<include file="webwork-default.xml" />
<package name="default" extends="webwork-default">
<default-interceptor-ref name="defaultStack" />
<action name="viewBlogEntries" class="action.ViewBlogEntriesAction">
<result name="success" type="dispatcher">viewBlogEntries.jsp</result>
</action>
</package>
</xwork>
Here, an action called <%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="webwork" prefix="ww" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><ww:property value="blog.name" /></title>
<link rel="stylesheet" href="screen.css" type="text/css" />
</head>
<body>
<div id="container">
<h1><ww:property value="blog.name" /></h1>
<h2><ww:property value="blog.description" /></h2>
<ww:iterator id="blogEntry" value="blog.blogEntries">
<div class="blogEntry">
<h3><ww:property value="title"/></h3>
<ww:if test="excerpt != null">
<ww:property value="excerpt"/>
<p>
<a href="<ww:url value="'viewBlogEntry.action'">
<ww:param name="'id'" value="id"/>
</ww:url>">Read more</a>
</p>
</ww:if>
<ww:else>
<ww:property value="body"/>
</ww:else>
<p>
Posted on <ww:property value="date" />
</p>
</div>
</ww:iterator>
</div>
</body>
</html>
If you're familiar with something like the Struts bean/logic tags or JSTL, you'll probably pick up the WebWork tags pretty quickly.
Blog entry detail page package action;
import com.opensymphony.xwork.ActionSupport;
import domain.Blog;
import domain.BlogEntry;
import domain.BlogService;
/**
* Responsible for finding a specific blog entry.
*
* @author Simon Brown
*/
public class ViewBlogEntryAction extends ActionSupport {
/** the ID of the blog entry to display */
private String id;
/** the blog that owns the blog entries */
private Blog blog;
/** the blog entry to be displayed */
private BlogEntry blogEntry;
/**
* Performs the processing associated with this action.
*
* @return a String defining the outcome of this action
*/
public String execute() throws Exception {
BlogService blogService = new BlogService();
blog = blogService.getBlog();
blogEntry = blog.getBlogEntry(id);
if (blogEntry == null) {
return "notfound";
} else {
return SUCCESS;
}
}
/**
* Setter for the id property.
*
* @param id a String
*/
public void setId(String id) {
this.id = id;
}
/**
* Gets the blog that this action is operating upon.
*
* @return a Blog instance
*/
public Blog getBlog() {
return blog;
}
/**
* Gets the blog entry to be displayed.
*
* @return a BlogEntry instance
*/
public BlogEntry getBlogEntry() {
return blogEntry;
}
}
Next up is the fragment of XML that needs to be inserted into the <action name="viewBlogEntry" class="action.ViewBlogEntryAction">
<result name="success" type="dispatcher">viewBlogEntry.jsp</result>
<result name="notfound" type="dispatcher">404.jsp</result>
</action>
And finally is the JSP page itself. <%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="webwork" prefix="ww" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><ww:property value="blogEntry.title" /> : <ww:property value="blog.name" /></title>
<link rel="stylesheet" href="screen.css" type="text/css" />
</head>
<body>
<div id="container">
<h1><ww:property value="blog.name" /></h1>
<h2><ww:property value="blog.description" /></h2>
<div class="blogEntry">
<h3><ww:property value="blogEntry.title" /></h3>
<ww:property value="blogEntry.body" />
<p>
Posted on <ww:property value="blogEntry.date" />
</p>
</div>
</div>
</body>
</html>
Summary Bookmark blog post: CommentsComments are listed in date ascending order (oldest first) | Post Comment
| ||
|
|