Implementation of ActionSupport Class in Struts2.0 Framework

Implementation of ActionSupport Class in Struts2.0 Framework

Previous Home Next

 

The ActionSupport class implements Action interface which exposes the execute() method.

The following constants are declared in the Action interface which can be used as return values in the execute() method.
  • public static final String ERROR = "error"
  • public static final String INPUT = "input"
  • public static final String LOGIN = "login"
  • public static final String NONE = "none"
  • public static final String SUCCESS = "success"
  • ERROR is returned when the action execution fails.
  • INPUT is returned when the action requires more input from the user.
  • LOGIN is returned when the user is not logged into the system.
  • NONE is returned when the action execution is successfully and there are no views to display.
  • SUCCESS is returned when the action executed successfully and the corresponding result is displayed to the user

 

Directory Structure of ActionSupport Example in Struts 2.0 Using MyEclipse IDE




index.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<s:form action="register">
<s:textfield name="name" label="Name"/>
<s:password name="password" label="Password"/>
<s:textfield name="mailId" label="MailId"/>
<s:submit value="Register"/>
</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" extends="struts-default">
    <action name="register" class="mypack.RegisterAction">
    <result name="success">/hello.jsp</result>
    <result name="input">/index.jsp</result>
    </action>
    </package>
    </struts>

RegisterAction.java

package mypack;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport {
private String name,password,mailId;
	
@Override
	public void validate(){
	if(name.length()==0)
		this.addFieldError("name",”Name is Required"));
	if(password.length()==0)
		this.addFieldError("password",” Password is Required"));
	if(password.length()<5)
	this.addFieldError("password",”Password length more than five digit"));
}
public String execute(){
	return "success";
}
	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;
	}
	public String getMailId() {
		return mailId;
	}
	public void setMailId(String mailId) {
		this.mailId = mailId;
	}
}

hello.jsp

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

<b>You are successfully registered

Output



Previous Home Next