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
Request Call
Control
Model Transfer
View
Response Refer
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)
When framework is not used (2)
public class ControllerServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException {
try {
req.setCharacterEncoding("JISAutoDetect");
} catch (UnsupportedEncodingException e) {e.printStackTrace();}
try{
1. Acquire the request parameter
String page = ""; String flag = req.getParameter("flag");
2. Decide transition destination’s screen according to request parameter
if (flag.equals("1")) {
page = "/jsp/memberregistrationview.jsp";
}
getServletContext().getRequestDispatcher(page).forward(req,res);
}
}
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 (strutsconfig.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 “strutsconfig.xml”, it calls
the form class and the action class corresponding to the request
Executes the View‘s call
Configuration file (strutsconfig.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 strutsconfig.xml Definition of form Bean class Definition of Action class Making JSP file for display
Registration to strutsconfig.xml
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, strutshtml.tld and strutsbean.tld and so on are prepared
Login sample program
Explain based on lecturer computer
Outline
Hello struts world LoginExample
Hello Struts
Hello Struts
Select [File] – [New] – [Project].
Hello Struts
Select [Tomcat project] in [New project] window, and click on [Next] button
Name project blank > Finish
Libraries to import
Find them in the lib directory of Struts2 package
How to import jars?
Hello Struts
Import or edit files:
struts.xml
web.xml
HelloWorld.java
HelloWorld.jsp
Find them on tutorial pages of struts 2 packages
struts.xml
web.xml
HelloWorld.java
HelloWorld.jsp
You got it!
Discussion
How it works?
Login example
Sample program for Struts login processing
Select [File] – [New] – [Project].
Sample program for Struts login processing
Select [Tomcat project] in [New project] window, and click on [Next] button
Sample program for Struts login processing
Give the following project name in [New Tomcat project] window, and click the [Finish] button. Project name: LoginExample
Sample program for Struts login processing
Confirm that the “LoginExample” project is created in the “Package Explorer” view.
Sample program for Struts login processing
Rightclick [LoginExample project], and select [Import].
Sample program for Struts login processing
Select [File System] on [Import] screen, and click on [Next] button.
Files to be imported
struts.xml web.xml Logon.java Logon.jsp Success.jsp
Sample program for Struts login processing
Click on [Tomcat run] button. Start Tomcat Stop Tomcat Restart Tomcat
Sample program for Struts login processing
Verify with the "Console" view that Tomcat is started.
Run it now
http://localhost:8080/LoginExample/Logon
M
How it work
LogonForm
V
Logon.jsp (1) (4)
(2)
(4)
Action Servlet LogonAction (5)
(3)
(6) C struts.xml
Success.jsp
(7) (redirect)
Logon.jsp
Struts.xml
Logon.jsp
<%@ taglib prefix="s" uri="/strutstags" %>
Chuong trinh demo logon
Success.jsp
Chuc mung ban da thanh cong
Quay tro lai trang ban dauLogon.java (1)
package tutorial; import com.opensymphony.xwork2.ActionSupport;
public class Logon extends ActionSupport {
public String execute() throws Exception { if (isInvalid(getUsername())) return INPUT; if (isInvalid(getPassword())) return INPUT; return SUCCESS; }
private boolean isInvalid(String value) { return (value == null || value.length() == 0); }
Logon.java (2)
private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; }
private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }
}
Validator functions
User doing validation
By action class
By javascript
private boolean isInvalid(String value) { return (value == null || value.length() == 0); }
Struts does it for you
Validation registration by xml files
Defined by struts
Input to be validated
Clientside validation
Basic steps for validation
Create the form
Create the Action class
Create the validation.xml
Example: Step 1
Example: Step 2
Example: Step 3
Results
Tips: To avoid validation fire at the beginning
No validation fired
http://localhost:8080/BasicValidation/Basic_input
Validation fired
http://localhost:8080/BasicValidation/Basic
Handon exercices
Build the logon program Modify program to use error.jsp (to be
defined by students)
Import validation's features