Result Interface Example in Struts 2.0
Previous | Home | Next |
Result Interface Example using Struts 2.0 the following tool are required for run this example
- JDK 1.5
- MyEclipse IDE
- Server Tomcat 6.0
- Struts 2.0 jar file
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.javapackage 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.javapackage 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.javapackage 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 |