struts- Execute the authentication processing by calling a corresponding Action class LoginAction.. Making JSP file for displayHtml tag library Tag that execute Struts own execution Bea
Trang 1Chapter 13 StrutsITSS Java Programming Ngo Hong Son, HUT
Trang 3Processing of screen control framework
Response
Trang 4Typical screen control framework
Process(JCP)
Trang 5When framework is not used (1)
Trang 6public class ControllerServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException { try {
}
}
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
Trang 7When framework is used
Trang 8Struts description
using Servlet and JSP
storage of data, screen transition control, etc
file (struts-config.xml)
Trang 9 Change the language displayed on the client side
by the browser's language setting
Trang 10Struts components
Trang 11ActionServlet class
the form class and the action class
corresponding to the request
Trang 12Configuration file (struts-config.xml)
according to the request
the object screen of processing result.
Trang 13Form Bean (Action form class)
form and the link
Trang 14JSP file for display
JSP, and Struts.
Trang 15Action class
the Struts
call the method.
Trang 16Operation principle of Struts (1)
Trang 17Operation principle of Struts (2)
Screen (loginForm.jsp) is displayed to the client
Login: the ActionServlet will be called
ActionServlet refers the configuration file config.xml)
(struts- 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
Trang 18Making class based on framework
Trang 19Procedure of Struts application
Trang 20Registration to struts-config.xml
<?xml version="1.0" encoding="Shift_JIS"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
<forward name="success" path="/jsp/loginSuccess.jsp" />
<forward name="error" path="/jsp/loginError.jsp" />
Trang 21Definition of form Bean class
Trang 22Definition of Action class
Trang 23Making JSP file for display
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
Trang 24Login sample program
Trang 25Outline
Trang 26Hello Struts
Trang 27Select [File] – [New] – [Project]
Trang 28Hello Struts
Select [Tomcat project] in [New project] window, and click on [Next] buttonName project blank ->
Finish
Trang 29Libraries to import
Find them in the lib directory of Struts-2
package
Trang 30How to import jars?
Trang 31Hello Struts
Import or edit files:
struts.xmlweb.xmlHelloWorld.javaHelloWorld.jsp
Find them on tutorial pages of struts 2 packages
Trang 32struts.xml
Trang 33web.xml
Trang 34HelloWorld.java
Trang 35HelloWorld.jsp
Trang 36You got it!
Trang 37How it works?
Trang 38Login example
Trang 39Sample program for Struts login processing
Select [File] – [New] – [Project]
Trang 40Sample program for Struts login processing
Select [Tomcat project] in [New project] window, and click on [Next] button
Trang 41Sample program for Struts login processing
Give the following project name in [New Tomcat project] window,
and click the [Finish] button Project name: LoginExample
Trang 42Sample program for Struts login processing
Confirm that the “LoginExample” project is
created in the “Package Explorer” view
Trang 43Sample program for Struts login processing
Right-click [LoginExample project],
and select [Import]
Trang 44Sample program for Struts login processing
Select [File System] on [Import] screen,
and click on [Next] button
Trang 46Sample program for Struts login processing
Click on [Tomcat run] button
Start Tomcat Stop Tomcat Restart Tomcat
Trang 47Sample program for Struts login processing
Verify with the "Console" view that Tomcat
is started
Trang 48Run it now
Trang 49How it work
Logon.jsp
(1)
Action Servlet
Success.jsp Logon.jsp
Trang 50<package name="tutorial" extends="struts-default">
<action name="Logon" class="tutorial.Logon">
<result name="input"> /Logon.jsp</result>
<result type="redirectAction">Success </result>
Trang 52<html>
<head><title>Dang nhap thanh cong</title></head>
<body>
<h3> Chuc mung ban da thanh cong </h3>
<a href="Logon"> Quay tro lai trang ban dau </a>
</body>
</html>
Trang 53Logon.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;
Trang 54Logon.java (2)
private String username;
public String getUsername() {
private String password;
public String getPassword() {
Trang 55Validator functions
Trang 56User doing validation
Trang 57Struts does it for you
Input to be
validated Defined by struts
Validation registration by
xml files
Trang 58Client-side validation
Trang 59Basic steps for validation
Create the form
Create the Action class
Create the validation.xml
Trang 60Example: Step 1
Trang 63Results
Trang 64Tips: To avoid validation fire at the beginning
<struts>
<package name="" extends="struts-default">
<action name="Basic_*" method="{1}" class="QuizAction">
Trang 65Hand-on exercices
defined by students)
Trang 66Hand-on exercises