• No results found

Action - LoginAction

In document Struts2 Black Book (Page 178-182)

The action class created here is LoginAction. The LoginAction action class has two input properties, i.e. username and password, with setter/getter methods. The LoginAction action class does not have any validate method implemented here as its properties are to be validated using the Validation Framework, taken care by the validate Interceptor.

Here’s the code, given in Listing 4.48, for LoginAction action class (you can find LoginAction.java file in Code\Chapter 4\struts2_i\WEB-INF\src\com\kogent\action folder in CD):

Listing 4.48: LoginAction.java

package com.kogent.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

private String username;

private String password;

public String getUsername() {

return username;

}

public void setUsername(String username) { this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) { this.password = password;

}

public String execute() throws Exception { if(username.equals(password))

return SUCCESS;

The LoginAction returns SUCCESS if the username matches with password, otherwise an action error is added and an ERROR is returned. The input properties, like username and password, are validated for their values through the Validation Framework. This means the validation rules for these fields are to be defined an ActionClass_validation.xml file where ActionClass is the name of the action class to be validated.

Here’s the content of LoginAction-validation.xml, given in Listing 4.49, which defines the validation rules for the input fields of LoginAction action class (you can find LoginAction-validation.xml file in Code\Chapter 4\struts2_i\WEB-INF\src\com\kogent\action folder in CD):

Listing 4.49: LoginAction-validation.xml file

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"

"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>

<field name="username">

<field-validator type="requiredstring">

<message>Enter User Name</message>

</field-validator>

</field>

<field name="password">

<field-validator type="requiredstring">

<message>Enter Password</message>

</field-validator></field>

</validators>

The action class and associated ActionClass_validation.xml file must be at the same location.

Configuring Action and Interceptors

Now, we need to configure this action class with a set of Interceptors and results. The basic Interceptors, which need discussion here, are validation and scope. The Interceptor stack, defined here for this action, includes these Interceptors. The Validation Interceptor works in conjunction with the Workflow Interceptor. Add a new action mapping for LoginAction, which is shown in Listing 4.50, with all Interceptors and results:

Listing 4.50: Configuring Interceptors for LoginAction

<action name="login" class="com.kogent.action.LoginAction">

<interceptor-ref name="basicStack"/>

<interceptor-ref name="validation"/>

<interceptor-ref name="workflow"/>

<interceptor-ref name="scope">

<param name="session">username, password</param>

<param name="key">ACTION</param>

<param name="type">start</param>

<param name="autoCreateSession">true</param>

</interceptor-ref>

<result name="success">/login_success.jsp</result>

<result name="error">/login.jsp</result>

<result name="input">/login.jsp</result>

</action>

The Scope Interceptor is used to set some action properties in application or session scope to make it consistent throughout the application. The session and application attributes are keyed after the action’s class, action’s name or any other given key. The LoginAction class properties to be set in session is provided using <param name="session">username, password</param>.

Similarly, parameter key is set to ACTION, which creates a unique key prefix for the attribute to be set in session scope here with actions namespace and action name.

Working of Example

We can access the login.jsp page by clicking over the ‘Interceptor Example 5’ hyperlink, as shown in Figure 4.3 and created in Listing 4.26. The output of the login.jsp page is already shown in Figure 4.15.

Click over ‘Login’ button, as shown in Figure 4.15, leaving both fields blank to see the field errors as shown in Figure 4.16. These field errors have been added by the Validation Interceptor according to the validation rules defined in LoginAction_validation.xml for different properties of LoginAction.

Figure 4.16: The login.jsp page showing field errors set by validation Interceptor.

The error text displayed here is defined in LoginAction_validation.xml file. Enter a same string as username and password here to see the output of login_success.jsp, which displays a message for successful login and prints the username and password from the session scope. We have not used any session object throughout this example, but the two properties of the LoginAction action class are put into session scope by the Scope Interceptor. The output of the login_success.jsp is shown in Figure 4.17.

Figure 4.17: The login_success.jsp showing data from session scope.

This chapter described the important and frequently-used Interceptors through five different examples.

Each example emphasized a different set of Interceptors in their working. The configuration and execution flow of different Interceptors are fully discussed in these examples. Now, the different Interceptors can be seen for their importance. The chapter fully describes where these Interceptors are required and how they should be implemented. Different modifications in the action class in conjunction have also been described, like implementing different interfaces and defining some methods to make Interceptors active.

In the next chapter, we’ll discuss the OGNL expression language and its different capabilities and how it is supported in Struts 2 Framework for interaction with ValueStack.

5

Manipulating Data with

In document Struts2 Black Book (Page 178-182)