Struts
Struts
Tom Janofsky
Outline
Background (Servlets and JSPs)
JSP Design
MVC and JSP Model 2
Struts
Into the Future
Java Web Development
HTTP Request
HTTP Response
JSP file
Generated
Pre-processed Servlet
Servlet
Compiled .class file
JSP
Snippets of Java in HTML
Processed into servlets
Display easier to maintain
Much like ASP
"JSP technology should be viewed as
the norm while the use of servlets will
most likely be the exception." (Sun in
1999)
JSP
<%@ page info="Books" errorPage="error.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Book Page</title></head><body>
<h1>Book Form</h1>
<jsp:useBean id="bookBean" class="BookForm" scope="request"/>
<table>
<tr><td>Title:</td>
<td><input name="title" type="text" value='<jsp:getProperty name="bookBean" property="title"/>'</td></tr>
<tr><td>Author:</td>
<td><input name="author" type="text" value='<jsp:getProperty name="bookBean" property="author"/>'></td></tr>
<tr><td>Publisher:</td>
<td><input name="publisher" type="text" value='<jsp:getProperty name="bookBean" property="publisher"/>'></td></tr>
<tr><td>Year:</td>
<td><input name="year" type="text" value='<jsp:getProperty name="bookBean" property="year"/>'></td></tr>
<tr><td>URL:</td>
<td><input name="url" type="text" value='<jsp:getProperty name="bookBean" property="url"/>'></td>
</table>
<A HREF="/clearBook.jsp">Clear form</A><BR>
…
Problems with JSP
Mixing of code and display still not easy
to maintain
Tempting to put too much Java code in
the JSP (tightly coupled)
Embedded logic flow
Difficult to debug
Still no framework for designing a web
application
JSP Model I
JSP page processes request and
returns response to client
Separates content from presentation
using JavaBeans
Forms are submitted to the pages that
create them
JSP Model I
Request
Response
Browser
JavaBean Datastore
JSP/Servlet Container
Problems with JSP
Model I
Uses much scriplet code in JSP
Pages can be large and complicated
Still embeds navigation in page
Boundaries of responsibility are still
unclear.
Can lead to spaghetti code
Tends not to be modular
JSP Model II
Implements an MVC approach to web
development
Separates presentation from processing
from data
Clearly delineates responsibility
Change layout without changing code
What is MVC?
A design pattern used to separate business
logic from user interface, from program flow
View
How data is presented to the user
Model
Data or state within the system
Controller
Connects the model and the view. Handles
program flow.
MVC
Response
Browser
Struts
Open source implementation of an MVC framework
by the Apache group (jakarta.apache.org)
Contains
Controller (servlet)
“Action” classes
Custom JSP tag libs
Utility classes for XML processing and JavaBean
population
Version 1.0 released this spring
Under active development
Struts Components
Controller – The ActionServlet is responsible
for dispatching all requests. Mappings to
actions are defined in the struts-config.xml file
Action – Actions are defined by extending the
Action class. Actions perform actual work.
Model – The ActionForm can be extended to
provide a place to store request and response
data. It is a JavaBean (getters/settters)
View – JSP pages utilizing Struts tag libraries.
Struts UML
Struts Flow
HTTP Request
reads
Browser struts-config.xml
JSP ActionForm
HTTP Response
The View
You can begin your Struts app by
creating the view in a JSP page
Use struts taglibs for form elements
Implement logic using built in equals,
present, notPresent, and iterate tags
View Example
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<head>
<title>Page Title</title><html:base/></head>
<html:form action=“action.do">
<html:text property="name" size=“50" maxlength=“50"/>
<logic:equal name=“sampleForm" property="currentAction" value="Add">
<html:text property=“description" size="100" maxlength="100"/>
</logic:equal>
</table>
</html:form>
<logic:iterate id=“names" name=“sampleForm" property=“nameList">
<TR><TD><bean:write name=“names" property=“firstName" /></TD>
</TR>
</logic:iterate>
</body></html>
The Model
Beans should be location agnostic (I.e. they should not have
detailed knowledge of the screen on which they are used.
A field for every control on the page
Extending ActionForm gets you
Automatic creation
Automatic population
}
The Controller
Translates logical names for mappings via the
struts-config.xml
Controls flow of the application
Load and instantiates proper beans
Populates beans with values from requests
Passes bean to appropriate action
Uses ActionForward from the result to determine
destination
Handles errors
Forwards bean and control to appropriate view or
action (Mapping)
Add entry to struts-
config.xml
<form-beans>
<form-bean
name=“sampleForm"
type=“SampleForm"/>
</form-beans>
…
<action path="/SamplePage"
type=“SampleAction"
name=“sampleForm"
validate="false"
input="/sample.jsp">
<forward name="success" path="/sample.jsp"/>
</action>
The Action
Actions know where they will be used
They are invoked via their mapping in
the struts-config.xml
They can create ActionErrors on
failures, and return a forward to a
mapping
Action Sample
import org.apache.struts.action.*;
import javax.servlet.http.*;