CHAPTER 4: Building a Web Application Using Struts 2 197
- public String searchByKeyword() {}
- public String home() {}
- public String selectedBooks(){}
- public String logout() {}
- // getters and setters
- }
Line 5: responsible for displaying the login form.
Line 7: authenticates the user against the database.
Line 9: displays the error message, for example if the user is invalid.
Line 11: lists all the books in the bookstore.
Line 13: lists the books by category.
Line 15: allows user to search the books by keyword: book title or
author’s name.
Line 17: displays the home page when clicked on home link.
Line 19: displays the list of selected books.
Line 21: allows the user to log out.
Login Using Database
Listing 4-40 illustrates the executelogin() method responsible for authenticating user against the
database. For this you need to add the USER table to the data model you developed in chapter 1
using the following DDL:
CREATE TABLE USER(
ID INT NOT NULL AUTO_INCREMENT,
FIRST_NAME VARCHAR(60) NOT NULL,
LAST_NAME VARCHAR(60) NOT NULL,
USERNAME VARCHAR(60) NOT NULL,
PASSWORD VARCHAR(60) NOT NULL,
PRIMARY KEY (ID)
);
Listing 4-40. executelogin( )method in the BookController
- public String executelogin() {
- String executelogin = "failed";
- session = ActionContext.getContext().getSession();
- dao = new BookDAOImpl();
- user = new User();
- user.setUserName(getUsername());