Java Reference
In-Depth Information
25.public void setPassword(String password) {
26.this.password = password;
27.}
28.}
In lines 7 to 9 of Listing 4-30, the username and password are hard-coded. Later you will see how
to authenticate against a database. If the username or password is invalid, the
addActonError
method in line 10 gets the
error.login
message mapped to the
ApplicationResources.properties
,
illustrated in Listing 4-31, and returns the String
error
.
Listing 4-31. ApplicationResources.properties
label.username= Username
label.password= Password
error.login= Invalid Username/Password
Listing 4-32 illustrates
home.jsp
, which is rendered when the
LoginAction
class returns the String
success
.
Listing 4-32. home.jsp
1.<%@ page contentType="text/html; charset=UTF-8"%>
2.<%@ taglib prefix="s" uri="/struts-tags"%>
3.<html>
4.<head>
5.<title>Home</title>
6.</head>
7.<body>
8.<s:property value="username" />
9.</body>
10.</html>
Listing 4-32 displays the username using the
s:property
tag when
LoginAction
returns the
String
success
.
Developing Templates
In this section, you will see how to integrate the Tiles framework with Struts 2. We will add Tiles support
to the HelloWorld Struts application created in the previous section. Tiles is a templating system that
reduces code duplication and maintains a consistent look and feel across all the pages of a web
application. With Tiles, you define a common layout in a configuration file, and this layout is extended
across all the webpages of the web application. This means you can change the look and feel of all the
pages of a web application by changing just the template file, instead of changing all the pages. For the
bookstore application, you will add a header and menu, as illustrated in Figure
4-18
.