Chapter 13. Struts
ITSS Java Programming
Ngo Hong Son, HUT
Outline
Describe the necessity and the advantage of
the framework
Develop the Web application by using the
framework
Processing of screen control framework
View
Control
Model
Request
Call
Refer
Transfer
Response
Typical screen control framework
Struts: The Apache Software Foundation
JSF (JavaServer Faces): The Java Community
Process(JCP)
Webcoordinator: FUJITSU Ltd.
When framework is not used (1)
public class ControllerServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException {
try {
req.setCharacterEncoding("JISAutoDetect");
} catch (UnsupportedEncodingException e) {e.printStackTrace();}
try{
String page = "";
String flag = req.getParameter("flag");
if (flag.equals("1")) {
page = "/jsp/member-registration-view.jsp";
}
getServletContext().getRequestDispatcher(page).forward(req,res);
}
}
When framework is not used (2)
1. Acquire the request parameter
2. Decide transition destination’s screen according to request parameter
3. Transfer to the transition destination‘s screen
When framework is used
Struts description
Framework to develop the Web app. by
using Servlet and JSP.
Starts according to the request from a
client
Offers the mechanism to process request,
storage of data, screen transition control,
etc.
Defines screen transition information in
XML file (struts-config.xml)
Functions of Struts
Screen control
Execute the screen transition based on “file of
the transition.
Validator function
Verify function of input data from the client
Internationalization
Change the language displayed on the client side
by the browser's language setting
Struts components
ActionServlet class
Receives the request of the “xxx.do” form
After referring to “struts-config.xml”, it calls
the form class and the action class
corresponding to the request
Executes the View‘s call
Configuration file (struts-config.xml)
Specifies the form Bean and the action class
according to the request
Specifies the transfer destination file which is
the object screen of processing result.
Form Bean (Action form class)
Succeeding to the ActionForm class
Takes request information sent from the
form and the link
Confirms whether the input value is correct.
JSP file for display
Combine the custom tag offered by HTML,
JSP, and Struts.
Display the screen
Action class
Succeeding to the Action class provided by
the Struts
Generate the object of the Model class and
call the method.
Store the object in the scope
Specify the View of the transition destination
Operation principle of Struts (1)
Operation principle of Struts (2)
Screen (loginForm.jsp) is displayed to the client.
Login: the ActionServlet will be called.
ActionServlet refers the configuration file (struts-
config.xml).
Execute the authentication processing by calling a
corresponding Action class (LoginAction).
Decides the JSP of the transition destination,
according to the result authenticated
The ActionServlet refers to the configuration file,
and selects the transition destination's JSP
Returns the client the screen of the processing
result.
Making class based on framework
Procedure of Struts application
Registration to struts-config.xml
Definition of form Bean class
Definition of Action class
Making JSP file for display
Registration to struts-config.xml
<?xml version="1.0" encoding="Shift_JIS"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"
<struts-config>
<! Definition of Form Bean >
<form-beans>
<form-bean name="loginFormBean" type="form.LoginForm" />
</form-beans>
<!– Definition of Action class mapping >
<action-mappings>
<action path="/login" name="loginFormBean" scope="request"
type="action.LoginAction">
<forward name="success" path="/jsp/loginSuccess.jsp" />
<forward name="error" path="/jsp/loginError.jsp" />
</action>
</action-mappings>
<!– Definition of Message resource >
<message-resources parameter="ApplicationResources" />
</struts-config>
Definition of form Bean class
Definition of Action class
Making JSP file for display
Custom Tag Content
Html tag library Tag that execute Struts own execution
Bean tag library Tag that acquire and display Bean property and
variable
Logic tag library Tag that execute processing such as repetition
or branching
TLD file Xml form file that defines use of custom tag, in
struts, struts-html.tld and struts-bean.tld and
so on are prepared
Login sample program
Explain based on lecturer computer
Outline
Hello struts world
LoginExample