Learn Java for Web Development

(Tina Meador) #1
CHAPTER 4: Building a Web Application Using Struts 2 187

Listing 4-29. struts.xml


1.<?xml version="1.0" encoding="UTF-8" ?>
2.<!DOCTYPE struts PUBLIC
3."-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
4."http://struts.apache.org/dtds/struts-2.3.dtd">
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.view/home.jsp
15.login.jsp
16.

17.

18.


In Listing 4-29, line 13 maps the LoginAction class of the login name to the URL, and line 14
renders home.jsp when the String success is returned by the LoginAction class. If the LoginAction
class returns the String error, then login.jsp is rendered again with the error message.


Listing 4-30 illustrates LoginAction.


Listing 4-30. LoginAction.java


1.package com.apress.bookstore.action;
2.import com.opensymphony.xwork2.ActionSupport;
3.public class LoginAction extends ActionSupport {
4.private String username;
5.private String password;
6.public String execute() {
7.if (this.username.equals("vishal") && this.password.equals("password")) {
8.return "success";
9.} else {
10.addActionError(getText("error.login"));
11.return "error";
12.}
13.}
14.public String getUsername() {
15.return username;
16.}
17.public void setUsername(String username) {
18.this.username = username;
19.}
20.
21.public String getPassword() {
22.return password;
23.}
24.

Free download pdf