Implementation of Struts2.0 Result Interface

Implementation of Struts2.0 Result Interface

Previous Home Next

 

A result is a framework component i.e responsible for invoking JSP page , HTML page ,AJAX , Velocity etc . Functionality of result is described by Result interface.

A result is a framework component i.e responsible for invoking JSP page , HTML page ,AJAX , Velocity etc . Functionality of result is described by Result interface which contain following single method. 

public void execute(ActionInvocation ai);

This method is invoked by the ActionInvocation when String is return to it either by an interceptor by the action.

 
Directory Structure of Result Interceptor Example in Struts 2.0 Using MyEclipse IDE



index.jsp

<%@taglib uri="/struts-tags" prefix="s"%>

<s:form action="login">
<s:textfield name="name" label="Name"/>
<s:password name="password" label="Password"/>
<s:submit value="Login"/>
</s:form>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <filter>
  <filter-name>f1</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>f1</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

struts.xml

<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<package name="demo">
<result-types>
<result-type name="dispatcher" class="mypack.MyResult" default="true"/>
</result-types>
<interceptors>
<interceptor name="paramSetter" class="mypack.MyInterceptor"/>
</interceptors>
<action name="login" class="mypack.LoginAction">
<interceptor-ref name="paramSetter"/>
<result name="success">/hello.jsp</result>
<result name="failure">/relogin.jsp</result>
</action>
</package>
</struts>

MyResult.java

package mypack;
import javax.servlet.*;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;

public class MyResult implements Result {
private String page;
public static final String DEFAULT_PARAM="page";
	public void execute(ActionInvocation ai) throws Exception {
		ServletRequest request=ServletActionContext.getRequest();
		ServletResponse response=ServletActionContext.getResponse();
		RequestDispatcher rd=request.getRequestDispatcher(page);
		rd.forward(request,response);

	}
	public void setPage(String page) {
		this.page = page;
	}

}

MyInterceptor.java

package mypack;
import java.util.Enumeration;
import javax.servlet.ServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.util.ValueStack;

public class MyInterceptor implements Interceptor {
	public void destroy() {	}
	public void init() { }
	public String intercept(ActionInvocation ai) throws Exception {
		ServletRequest request=ServletActionContext.getRequest();
		ValueStack vs=ai.getStack();
		Enumeration<String> e=request.getParameterNames();
		while(e.hasMoreElements())
		{
			String pName=e.nextElement();
			String pValue=request.getParameter(pName);
			System.out.println(pName+"\t"+pValue);
			vs.setValue(pName,pValue );
		}String str=ai.invoke();
		return str;
	}
}

LoginAction.java

package mypack;
public class LoginAction {
	private String name,password;
	public String execute(){
		if(name.startsWith("a")&&password.startsWith("b"))
			return "success";
		else
			return "failure";		
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

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

hello.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<b>Welcome, <s:property value="name"/></b>


relogin.jsp

<b>Invalid UserName and Password !</b>
<jsp:include page="index.jsp"/>

Output



Previous Home Next