Welcome To All: Gop I
Welcome To All: Gop I
opi
G
Introduction to MVC and
Struts Framework
Outline
Background (Servlets and JSPs)
JSP Design
MVC
Struts
Into the Future
Outline
Background (Servlets and JSPs)
JSP Design
MVC and JSP Model 2
Struts
Into the Future
Client (HTTP) Request
The web server searches for the required servlet and initiates it
The servlet then processes the client request and sends the
response back to the server, which is then forward to the client
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
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.
Tends not to be modular
There is no clear distinction between view and a controller. In Java terms, there is
JSP page (view and partial controller) which itself controls Business logic (Model)
that is why it is also called as page-centric architecture.
MVC II (Servlet-Centric)Architecture
Http Event
ActionServlet dispatch Business
Logic
Model
forward Resource
Bundle
JavaBean
Http Response View
get
JSP
Struts: The Model
To define a model in Struts is simple. Just create a java class
and provide some functions.
Your model classes should be coded independent of the Struts
framework to promote maximum code reuse by other
applications (i.e. if you have to reference javax.servlet.*
class in your model, you are doing something wrong)
Struts provides some default Model components, the most
important of which is ActionForm.
If you create a Model class by extending the Struts ActionForm
class, Struts will
Ensure your form bean is created when needed
Ensure form submittal directly updates your form object with the
inputted values
Your controller object will be passed the form bean
Model: ActionForm
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
Typically what will happen is Struts will see that errors are being
returned and will forward the user to a jsp page that has been setup as
the “failure” page.
Usually, the errors result from bad input on a form, so the failure page
will be set to the original form and any <html:errors> tags which are
found are replaced with the contents of the ActionErrors returned from
the validate method.
The Controller
The controller is the switch board of MVC.
It directs the user to the appropriate views by providing the view
with the correct model
The task of directing users to appropriate views is called
“mapping” in struts.
Luckily, the Struts framework provides a base object called
org.apache.struts.action.ActionServlet.
The ActionServlet class uses a configuration file called struts-
config.xml to read mapping data called action mappings
The ActionServlet class reads the incoming URI and tries to
match the URI against a set of action mappings to see if it can
find a controller class which would be willing to handle the
request
This process is described in a diagram on the following page
http://myhost/authorize.do
Instance of appropriate Action class is found and it’s perform() method is called
Action object handles the request and returns control to a view based where the user is within
the flow of the application
public class LogonAction extends Action {
if (myForm.getUserName().equals(“john”) &&
myForm.getPassword().equals(“doe”)) {
}
Controller: Forwards
You might be wondering what
mapping.findForward(“success”) means?
The mapping object passed to the Controller’s
perform() method is of type ActionMapping.
When you setup your struts-config.xml file you
can define forward tags that are available via the
ActionMapping.findForward() method.
In the previous example, our ActionMapping object
would have been loaded with values from the
<action-mapping> section defined for the
LogonAction controller in struts-config.xml.
<action-mappings>
<action path="/logon"
type="org.apache.struts.example.LogonAction"
name="logonForm"
scope="request"
input="/logon.jsp">
</action>
<forward name="success” path="/msgBoard.jsp"/>
<forward name=“failure” path="/msgError.jsp"/>
</action-mappings>
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
<%@ page language="java" %> <tr>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <th align="right">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <bean:message key="prompt.password"/>
</th>
<html:html locale="true"> <td align="left">
<head> <html:password property="password"
<title><bean:message key="logon.title"/></title> size="16" maxlength="16"/>
<html:base/> </td>
</head> </tr>
<body bgcolor="white">
<tr>
<html:errors/> <td align="right">
<html:submit property="submit"
<html:form action="/logon.do" focus="username"> value="Submit"/>
<table border="0" width="100%"> </td>
<td align="left">
<tr> <html:reset/>
<th align="right"> </td>
<bean:message key="prompt.username"/> </tr>
</th>
<td align="left"> </table>
<html:text property="username" size="16" maxlength="16"/>
</td> </html:form>
</tr>
</body>
</html:html>
Struts Strengths Simple Struts Program