You are on page 1of 4

Objectives

Introduction to Jakarta Struts •Understand the motivation for adopting a


framework for the Model-View-Controller
application architecture.

•Short introduction to Struts


Aaron A. Kagawa
Jesse Tom •Be able to implement a Model-View-
Controller application with Struts.
Information and Computer Sciences
University of Hawaii
Honolulu HI 96822
(1) (2)

Simple Servlet
package hall;
import java.io.*;
import javax.servlet.*;

Servlet Refresher
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {


public void doGet(HttpServletRequest
request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD " + "
HTML 4.0 Transitional//EN\">\n" + "<HTML>\n" +
"<HEAD><TITLE>Hello WWW</TITLE></HEAD>\n" +
"<BODY>\n" + "<H1>Hello WWW</H1>\n" +
"</BODY></HTML>");
}
}

(3) (4)

JSP Scriplets Problems with Model 1


if (Math.random() < 0.5) { Problems
out.println("Have a <B>nice</B> day!"); •Not scaleable
} •Not standardized
else { •Buggy
•Ugly
out.println("Have a <B>lousy</B> day!");
}

(5) (6)

1
Model 2 Struts Framework Overview
Model 2 – Model View Controller – separate Overview:
components to handle different responsibilities. • Struts is an open source framework which
encourages use of the MVC design pattern for
Model Java Servlet and JavaServerPages technology.
• responsible for business state knowledge
View Struts has the same 3 major components as MVC
• responsible for presentation • Model (business logic)
Controller • View (presentation components & JSP pages)
• responsible for controlling flow and state of user • Controller (delegates request)
input
• Note: This is just an overview of the struts
framework of which we are using for this class.
There is one major problem in Model 2. There is much more to which struts can do.
• It is just a Model. Please look it up your self.
(7) (8)

Motivation Model
Isn’t any model-view-controller design pattern good enough? The Model: business logic
• MVC is just a design pattern and can have many • Virtually unchanged from the original MVC design
implementations.
pattern.
-Problem: if two developers work on a single web application
using different implementations of MVC, the web application
may not “mix” well.
-Problem: seeking help on your own implementation of MVC is
hindered by the uniqueness of that implementation.
What Struts brings to the table.
• Struts provides a framework for MVC
-Plus: provides a standard of which the MVC design pattern is
implemented.
-Plus: Like most frameworks, it provides simpler API’s.

(9) (10)

View Controller
The View: presentation components & JSP pages The Controller: delegates requests
• org.apache.struts.action.ActionForm is a bean • Action class (extend org.apached.action.Action)
(setters and getters) of the each input in a form. -413 note: Replaces commands.
-checks the validity of the user’s input. -Process a request and return ActionForward
-saves or resets users input to that form. object that identifies where to forward the
response to.
•ApplicationResources.properties provides a
separation of design from content in the JSP • Action Mapping Configuration File (struts-
page.
config.xml)
-e.g. messages, titles, headings, etc…
-413 note: Replaces controller.java
• Struts provides several tag libraries which can be -Used by the strut’s controller servlet to map
used (similar in nature to JSTL) to present http requests to application actions.
information in JSP pages.

(11) (12)

2
Addition/Deletions/Changes to
stackstruts: A simple Struts example
stackmvcdb
Additions:
• ./lib/jar/struts.jar
• ./webapp/WEB-INF/struts-config.xml
• ./webapp/WEB-INF/struts.tld
The stackstruts system provides a simple • ./webapp/WEB-INF/struts-bean.tld
implementation of stackmvc using struts. • ./webapp/WEB-INF/struts-html.tld
• ./webapp/WEB-INF/struts-logic.tld
• ./webapp/WEB-INF/lib/struts.jar
• ./webapp/WEB-INF/classes/ApplicationResources.properties
cvs module name: stackstruts
Deletions:
• ./webapp/WEB-INF/lib/standard.jar
• ./webapp/WEB-INF/lib/request.jar
• ./webapp/WEB-INF/lib/js.jar
Changes
• ./webapp/WEB-INF/web.xml
• Most of the java files.

(13) (14)

Html tag library example:


What are those .tld files?
<%@ page language="java" %>
• ./webapp/WEB-INF/struts.tld <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
• ./webapp/WEB-INF/struts-bean.tld
• ./webapp/WEB-INF/struts-config.xml …
<title><bean:message key="page.index.title"/></title>
• ./webapp/WEB-INF/struts-html.tld …

• ./webapp/WEB-INF/struts-logic.tld
<table border="0">
<tr>
<td>
<html:form styleId="PushForm" action="/push" focus="number">
<bean:message key="prompt.number"/>
Are the struts tag libraries. These can be <html:select size="1" property="number">
used like any other tag libraries. <html:option value="1">1</html:option>
<html:option value="2">2</html:option>
<html:option value="3">3</html:option>
<html:option value="4">4</html:option>
</html:select>
<html:submit property="submit" value="Push"/>
</html:form>

(15) (16)

struts-config.xml example:
ApplicationResources.properties <struts-config>
<!-- ========== Form Bean Definitions =================================== -->
… <form-beans>
<!-- Logon form bean -->
<form-bean name="pushForm"
type="edu.hawaii.stackmvcdbstruts.view.PushForm"/>
</form-beans>
page.index.title=Stack MVC DB using Struts <!-- ========== Global Forward Definitions ============================== -->
page.index.heading=Stack MVC DB using Struts <global-forwards>
<forward name="failure" path="/index.jsp"/>
<forward name="success" path="/index.jsp"/>
prompt.number=Select a number: </global-forwards>

<!-- ========== Action Mapping Definitions ============================== -->


<action-mappings>
… <!-- Process a push -->
<action path="/push"
type="edu.hawaii.stackmvcdbstruts.controller.PushAction"
name="pushForm"
scope="request"
input="/index.jsp">
</action>
</action-mappings>
</struts-config>

(17) (18)

3
web.xml example:
<!-- Action Servlet Configuration -->
<servlet>
stackmvcdbstruts as a standard
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class> Struts is now the standard for implementing
web applications in this class.
<init-param>
<param-name>application</param-name>
<param-value>ApplicationResources</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value> Use stackstruts as the standard on how struts
</init-param>
<load-on-startup>1</load-on-startup>
should be used in this class.
</servlet>

stackstruts is not perfect please feel free to


<!-- Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern> scrutinize the code and design issues.
</servlet-mapping>

<!-- Struts Tag Library Descriptors -->


<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>

(19) (20)

What to do next
Readings:
•Struts home page
-http://jakarta.apache.org/struts/
•Struts Api
Demo -http://jakarta.apache.org/struts/api/index
.html
•Developer Guide
-http://jakarta.apache.org/struts/userGuid
e/index.html

(21) (22)

Assignment Assignment continued


1. Provide a new button called “double” in the stackstruts Due Tuesday.
index.jsp page.
• The “double” button invokes the DoubleAction.
• The DoubleAction takes the current contents of the Turn in screen shots of new index.jsp page.
stack and doubles it. So, if the initial contents (from top
to bottom) is “1 2 3”, the result of doubling is “1 2 3 1
2 3”.
• It is an error to try to double an empty stack, and you Need help?
should print out an explanatory message. •Aaron Kagawa kagawaa@hawaii.edu
• You must write a TestDoubleAction httpunit test.
2. Provide a new input form and new button that uses that
input form in the index.jsp page.
• Purpose: to practice creating an ActionForm and
associating that ActionForm with an Action.
• Httpunit test is optional.

(23) (24)

You might also like