Dynamic Results in Struts 2

In Struts 2, we can decide next action to be executed at runtime with complete set of its own interceptors.  here i will assume the example and source code of the previous article.

For this demo, change the existing Action class code:

package com.G2.Actions;

import com.G2.Model.LoginValidate;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	String uName,pwd;
	String nextAction;

	@Override
	public String execute() throws Exception {
		boolean res = LoginValidate.isLoginValid(uName, pwd);

		if(res)
			nextAction = "LoginSuccess";
		else
			nextAction = "LoginInput";

		return  "next";
	}

	public String getNextAction() {
		return nextAction;
	}

	public void setNextAction(String nextAction) {
		this.nextAction = nextAction;
	}

	public String getuName() {
		return uName;
	}

	public void setuName(String uName) {
		this.uName = uName;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
}

As you can see, I have added one more filed with setters and getters named “nextAction”, which will be accessed in struts configuration file.

Change in Configuration file :

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

	<package name="Login" extends="struts-default">
		<action name="LoginInput">
			<result>/pages/Login.jsp</result>
		</action>

		<action name="LoginSuccess">
			<result >/pages/LoginSuccess.jsp</result>
		</action>

		<action name="LoginAction" class="com.G2.Actions.LoginAction">
			<result name="next" type="redirectAction">${nextAction}</result>
		</action>

	</package>
</struts>


Here we are redirecting, on the next action if the result name is “next”. The next action is decided by the variable “nextAction” of the Action class. In above configuration if we change the result type to “chain” instead of “redirectAction”, then still application will run.

Posted

in

by

Tags:


Related Posts

Comments

One response to “Dynamic Results in Struts 2”

  1. prabakaran Avatar
    prabakaran

    Hi please send me the code for integrated struts 2 jdbc application

Leave a Reply to prabakaranCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: