05 - MVC With Struts
05 - MVC With Struts
Previous Attempts:
Model 1 Design Pattern
for every JSP page p
for every type of request r to p
insert in p code to implement the action requested by r
students.jsp
If request to insert student
perform SQL INSERT
If request to delete student Messy
perform SQL UPDATE JSP!
1
The MVC Design Pattern:
Separating Model, View & Controller
• Development “Best
Practice”
• Known well before
web items
– Smalltalk pioneered
• Model: Access to
Underlying Databases
and Info Sources
• Controller: Control
Flow of Web App
• View: Look-and-Feel
2
Data Entry Example – MVC Attempt
students.jsp View
Controller/Actions
Delete Update Insert
Student Student Student
DB
5
COMMUNICATION
Analysis/
Specification business process • Informal, imprecise
Phase and specification specification by customer
of Web application • Accompanied by hard-to-
built demos and diagrams
Chief Architect/
Technical Project Leader
COMMUNICATION
Development • Code developed may be
Phase technical specification
inconsistent with spec
and development
• Significant effort in
communicating spec formally
Developer
3
Struts
4
Struts Single Request Processing
View Request/Session
Scope
Ac1onForward Ac1onForward get
(Page or Ac1on) (Page or Ac1on) Data
set
HTTP Response HTTP Response
7 6
HTTP 5
Request 2 Ac1on ModelBean
Ini1a1ng
Page Ac1onServlet
Form
4 3
Validation Ac1onForm
Error 1
Controller
DB
Model
struts‐config.xml
10
5
Struts Single Request Processing (cont’d)
11
12
6
Struts Single Request Processing (cont’d)
13
14
7
Install Struts
15
Struts Examples
16
8
Pass Control to ActionServlet
web.xml
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
17
success students.jsp
showStudents.do
Insert
(buIon) Delete
StudentForm Update (buIon)
!validate InsertUpdate (buIon)
StudentForm
success !validate InsertUpdate
StudentForm
insertStudent.do
Delete
success
updateStudent.do
success
deleteStudent.do
18
9
Data Entry Example - 7th Attempt
menu.jsp Web Appliation
set
Request Scope
get Workflow
Students
(hyperlink)
StudentsRowSet
success students.jsp
showStudents.do
Insert
(buIon) Delete
StudentForm Update (buIon)
!validate InsertUpdate (buIon)
StudentForm
success !validate InsertUpdate
StudentForm
insertStudent.do
Delete
success
updateStudent.do
StudentModel.insertStudent()
(INSERT INTO Students…) success
deleteStudent.do
StudentModel.updateStudent()
(UPDATE Students…)
StudentModel.getAllStudents()
(SELECT * FROM Students) DB StudentModel.deleteStudent()
(DELETE FROM Students…)
19
5 success set
3 4
menu.jsp …/showStudents.do Ac1onServlet ShowStudentsAc1on StudentModel
2
Controller
1
DB
struts‐config.xml Model
20
10
showStudents.do Configuration
struts-config.xml
<struts-config>
...
<action-mappings>
<action
path="/showStudents"
type="dataentry.actions.ShowStudentsAction">
<forward
name="success”
path="/pages/students.jsp"/>
</action>
...
</action-mappings>
...
</struts-config>
21
ShowStudentsAction.java
package dataentry.actions;
import javax.sql.RowSet;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import dataentry.model.StudentModel;
...
22
11
showStudents.do Action Bean
ShowStudentsAction.java (cont’d)
...
public ActionForward execute(
ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws DBException {
return mapping.findForward("success");
}
}
23
StudentsModel.java
package dataentry.model;
...
public class StudentModel {
private static String selectStr = ...;
private static String insertStr = ...;
private static String updateStr = ...;
private static String deleteStr = ...;
24
12
showStudents.do ActionForward
students.jsp
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
...
<%-- -------- Iteration Code -------- --%>
<% RowSet crsStudents = (RowSet) request.getAttribute("crsStudents");
while (crsStudents.next()) { %>
<tr>
...
<td>
<html:text property=”middle" size="15"
value="<%=crsStudents.getString(\”middleName\")%>" />
</td>
...
</tr>
<% } %>
...
25
7 success
2 5
6
…/insertStudent.do InsertStudentAc1on StudentModel
students.jsp Ac1onServlet
Form Valida1on
Error 4 StudentFormInsertUpdate
1 3
Controller DB
struts‐config.xml Model
26
13
insertStudent.do Configuration
struts-config.xml
...
<form-bean name="studentFormInsertUpdate”
type="dataentry.forms.StudentFormInsertUpdate"/>
...
<action
path="/insertStudent”
type="dataentry.actions.InsertStudentAction"
validate="true”
scope="request"
Not what you
think it is!
input="/showStudents.do”
name="studentFormInsertUpdate">
<forward name="success" path="/showStudents.do”
redirect="true"/>
</action>
27
StudentFormInsertUpdate.java
package dataentry.forms;
...
public class StudentFormInsertUpdate extends ActionForm {
28
14
insertStudent.do ActionForm Bean
StudentFormInsertUpdate.java (cont’d)
...
/**
* Reset all properties to their default values.
*/
public void reset(ActionMapping mapping,
HttpServletRequest request) {
setId(null);
setFirst(null);
setMiddle(null);
setLast(null);
}
...
29
StudentFormInsertUpdate.java (cont’d)
...
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
return errors;
}
}
30
15
insertStudent.do Action Bean
InsertStudentAction.java
public class InsertStudentAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws DBException {
return mapping.findForward("success");
}
}
31
insertStudent.do ActionForward
students.jsp
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
...
The corresponding
<!-- in case form validation fails --> Ac1onForm bean
<html:errors /> will also be used to
... populate HTML form
<tr>
<html:form action="/insertStudent">
<td><html:text property="id" size="10" /></td>
<td><html:text property="first" size="15" /></td>
<td><html:text property="middle" size="15" /></td>
<td><html:text property="last" size="15" /></td>
<td><html:submit value="Insert" /></td>
<td><html:reset /></td>
</html:form>
</tr>
...
32
16
struts-config.xml Structure
<struts-config>
<!-- ========================= Form Bean Definitions -->
<form-beans>...</form-beans>
Global Exceptions
struts-config.xml
34
17
Global Exceptions
DBException.java
package dataentry.db;
public DBException() {
super();
}
35
Global Exceptions
StudentModel.java
try {
...
} catch (SQLException ex) {
throw new DBException(ex);
} catch (NamingException ex) {
throw new DBException(ex);
}
}
36
18
Global Exceptions
InsertStudentAction.java
...
StudentModel.insertStudent(...);
...
}
}
37
Global Exceptions
dbException.jsp
<html>
<body>
<h2>Database Exception</h2>
...
<h3>Here is the message generated by the thrown database
exception:</h3>
<p><html:errors /></p>
</body>
</html>
38
19
Global Forwards
struts-config.xml
menu.jsp
39
Message Resources
MessageResources.properties
# -- app --
app.title=Struts Data Entry Application
...
students.jsp
40
20