Professional Documents
Culture Documents
Simple Servlet
package hall;
import java.io.*;
import javax.servlet.*;
Servlet Refresher
import javax.servlet.http.*;
(3) (4)
(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)
• ./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>
(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>
(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)
(23) (24)