Sie sind auf Seite 1von 5

Objectives Introduction to Jakarta Struts

Understand Short
Aaron A. Kagawa ICS 414 Spring 2005 Information and Computer Sciences University of Hawaii Honolulu HI 96822
(1) (2)

the motivation for adopting a framework for the Model-View-Controller application architecture. introduction to Struts

Be

able to implement a Model-ViewController application with Struts.

Model 1 Model 1 and Model 2 Refresher

(3)

(4)

JSP Scriplets
if (Math.random() < 0.5) { out.println("Have a <B>nice</B> day!"); } else { out.println("Have a <B>lousy</B> day!"); }

Simple Servlet
package hall; import java.io.*; import javax.servlet.*; 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>"); }

(5)

(6)

Problems with Model 1


Benefits Good for really simple web applications Problems Not scaleable Not standardized Buggy Ugly

Model 2

(7)

(8)

Model 2
Model 2 Model View Controller separate components to handle different responsibilities. Model responsible for business state knowledge View responsible for presentation Controller responsible for controlling flow and state of user input

StackMVC
Model Stack subsystem Commands
-edu.hawaii.stackmvc.control.command.PopCommand.java,

edu.hawaii.stackmvc.control.command.PushCommand.java, etc

View JSP pages Controller Controller

Commands
(9) (10)

-edu.hawaii.stackmvc.control.Controller.java -edu.hawaii.stackmvc.control.command.PopCommand.java,
edu.hawaii.stackmvc.control.command.PushCommand.java

Major Problem with Model 2


There is one major problem in Model 2. It is just a Model. Model 2 can be implemented many different ways. StackMVC is just one way.

Struts Framework Overview


Overview: Struts is an open source framework which encourages use of the MVC design pattern for Java Servlet and JavaServerPages technology. Struts has the same 3 major components as MVC Model (business logic) View (presentation components & JSP pages) Controller (delegates request)

Note: This is just an overview of the struts

framework of which we are using for this class. There is much more to which struts can do. Please look it up your self.

(11)

(12)

Motivation
Isnt any model-view-controller design pattern good enough? MVC is just a design pattern and can have many implementations. -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 APIs.

Model
The Model: business logic Virtually unchanged from the original MVC design pattern.

(13)

(14)

View
The View: presentation components & JSP pages org.apache.struts.action.ActionForm is a bean (setters and getters) of the each input in a form. -checks the validity of the users input. -saves or resets users input to that form.

Controller
The Controller: delegates requests Action class (extend org.apached.action.Action) -Replaces commands. -Process a request and return ActionForward object that identifies where to forward the response to.

ApplicationResources.properties

provides a separation of design from content in the JSP page. -e.g. messages, titles, headings, etc

Action Mapping Configuration File (struts-

Struts provides several tag libraries which can be


used (similar in nature to JSTL) to present information in JSP pages.

config.xml) -Replaces Controller.java -Used by the struts controller servlet to map http requests to application actions.

(15)

(16)

stackstruts: A simple Struts example


The stackstruts system provides a simple implementation of stackmvc using struts.

Additions: ./lib/jar/struts.jar ./webapp/WEB-INF/struts-config.xml ./webapp/WEB-INF/struts.tld ./webapp/WEB-INF/struts-bean.tld ./webapp/WEB-INF/struts-html.tld ./webapp/WEB-INF/struts-logic.tld ./webapp/WEB-INF/lib/struts.jar ./webapp/WEB-INF/classes/ApplicationResources.properties


Deletions:

Addition/Deletions/Changes to stackmvcdb

./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.

(17)

(18)

What are those .tld files?


./webapp/WEB-INF/struts.tld ./webapp/WEB-INF/struts-bean.tld ./webapp/WEB-INF/struts-config.xml ./webapp/WEB-INF/struts-html.tld ./webapp/WEB-INF/struts-logic.tld
Are the struts tag libraries. These can be used like any other tag libraries.

Html tag library example:


<%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <title><bean:message key="page.index.title"/></title> <table border="0"> <tr> <td> <html:form styleId="PushForm" action="/push" focus="number"> <bean:message key="prompt.number"/> <html:select size="1" property="number"> <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>
(20)

(19)

ApplicationResources.properties
page.index.title=Stack MVC DB using Struts page.index.heading=Stack MVC DB using Struts prompt.number=Select a number:

struts-config.xml example:
<struts-config> <!-- ========== Form Bean Definitions =================================== --> <form-beans> <!-- Logon form bean --> <form-bean name="pushForm" type="edu.hawaii.stackmvcdbstruts.view.PushForm"/> </form-beans> <!-- ========== Global Forward Definitions ============================== --> <global-forwards> <forward name="failure" path="/index.jsp"/> <forward name="success" path="/index.jsp"/> </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>
(22)

(21)

web.xml example:
<!-- Action Servlet Configuration --> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-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> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </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>
(23) (24)

Demo

What to do next
Readings: Struts home page -http://struts.apache.org/ Struts Api -http://struts.apache.org/api/index.html Developer Guide -http://struts.apache.org/userGuide/index. html

Assignment
1. Download and Run the stackstruts web app Download http://csdl.ics.hawaii.edu/~kagawaa/struts/stac kstruts-4.0.106.zip Bring up tomcat, Run ant, Run ant junit Print out a screen shot Bring screen shot to Wednesdays Class

(25)

(26)

Assignment Cont.
1. Download and Run the stackstruts web application Download http://csdl.ics.hawaii.edu/~kagawaa/struts/stac kstruts-4.0.106.zip Bring up tomcat, Run ant, Run ant junit Print out a screen shot

Optional
1. Provide a new button called double in the stackstruts index.jsp page. The double button invokes the DoubleAction. The DoubleAction takes the current contents of the 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 should print out an explanatory message. Write a TestDoubleAction HttpUnit test.

(27)

(28)

Questions
Struts Website http://struts.apache.org/ Aaron Kagawa Post 307b kagawaa@hawaii.edu

(29)

Das könnte Ihnen auch gefallen