Learn how to use the HTML tag library in a login program.
BY Budi Kurniawan
In the first part of this series, you have learned how to configure the HTML tag library provided by Struts and the label used by independent use; in the second part you learn to be related to Form; in the third part, you will learn how you learn Use the HTML tag library in the Login program. You can click here to download the program. If you read the series of articles on Struts (including six parts), you will find that this Longin program is the enhancement version of the last Login program we discussed.
figure 1.
Application directory structure
This Login program is a simple struts program that will display how to use HTML tag libraries, especially with Form-related tags, and labels for errors. Its directory structure
The shown in Figure 1.
This article will introduce the relationship between each class in the program and the role they play. Note that you must copy all library files to the web-inf / lib directory; in order to save download time they are not included in the zip file.
There are three JSP pages as a view in the MVC paradigm: login.jsp, mainmenu.jsp and viewsecret.jsp. The login.jsp file contains two links (log out and viewing company secrets) for user login (log out), and viewsecret.jsp is used to display a confidential message.
The first page of this program is a login page, which is a Form containing two input boxes (UserName and Password) (see Figure 2). The user is required to enter the correct value in both input boxes. After logging in success, the program will display the mainmenu.jsp page, which contains two links: one is logging out, the other is to view the company confidential account (see Figure 3).
If you click on the "Logout" link, the program will exit and resume the login page. If you click the "View Confident" link, you will display the ViewSecret.jsp page. If the registration fails, the user will be reserved to the Login page to log in.
figure 2.
Login page
If the user enters the correct username and password, the program creates a session object and there will be a property named loggedin to be added to this session object. Only when the program can find this loggedin property in the user's session object, users can browse the Main Menu page and the Secret page. All SESSION objects that lack this attribute or invalid will force the user to log in. In this program, the correct user name is "john", the password is "123". These values are fixed-written in the program to avoid user authentication to the database or use other storage.
When the user logs out, his or her session object will be invalid, and the LONGIN page will be redisplayed. When the user clicks on the link to view the company secret, the ViewSecret.jsp page will pop up (see Figure 4).
image 3.
main page
Profile
Login programs have two profiles in a web-inflicity: as a web.xml file for deployment descriptors (see
Listing 1); and truts-config.xml file as a struts configuration file (see
Listing 2). in
In list 1, you need to pay special attention to the
param-value>
init-param>
A
The second place you need to pay attention to the deployment descriptor is the
Figure 4.
Confidential page
Pay attention
In list 2, the
The Form Bean has been introduced in the fifth part.
The last file in the web-inf directory is a struts-html.tld file, which is a TLD file in the HTML tag library. In order to save time, I will skip the introduction to it, but you can find it in the web-inf directory in the zip file.
The Form is displayed in the form of a com.javapro.struts.loginform class (see List 3). Pay attention to the part of the bold: It illustrates an ActionError object by incoming "Error.Missing.userName" and "Error.Missing.Password" string, which roles the keyword (key) in the message resource.
Action class Action class Two: LoginAction (see List 4) and LogoutAction (see List 5). It is also worth mentioning how to build this ActionError message through a keyword in the message resource (displayed as bold in the list 4).
The use of the HTML tag library uses a JSP file in the list 7, and list 8, respectively. The login.jsp page (see List 6) shows how these tags (bold parts) are used in the HTML library. I introduced these labels in the first part of this series, but you will have a more in-depth understanding of them by watching how it runs in a real program.
Message resources As I briefly introduced in the "Profile" section, in the ApplicationResources.properties file in the web-inf / class / com / javapro / struts directory, they are loaded to the default Message resources go. The
Errors.Header =
Error (s) !!! b>
Errors.footer =
ul>
Error.Missing.userName =
Error.login.failed =
If you check the web.xml file of the program, you will see how these objects are loaded. The message resource completes the loading process by incorporating the parameters of the program into the ActionServlet. When the ActionServlet is initialized, it analyzes the ApplicationResources.properties property in the package folder to the default message resource. If you change a message in the resource and reload the program, it will display it throughout the program. In your login program, this property file contains an error message used in the class of some programs.
This article describes how to use the HTML tag library in a simple login program. It describes the procedure, including views, configuration files, form beans, Action classes, and code for JSP pages.
About the Author:
Budi Kurniawan is an IT consultant that he specializes in Internet and object-oriented programming, teaches Java and Microsoft technology. He is very good
Java for the Web with servlets, JSP, And EJB; A Developer's Guide To Scalable Solutions (New Riders) Author, he also developed the most popular Java Upload Bean, you can
BrainySoftware.com gets it, many important companies have licensed and used it in the project. Budi contact information is
Budi@brainysoftware.com.