12:56 pm - Thursday July 27, 2017

struts 1x Validation with example program In Eclipse

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

Step by Step tutorial on running  your  StrutsValidation Struts application in Tomcat5.5 with Eclipse IDE

Step – 1 Creating Dynamic web Project

Step – 2 StrutsValidation   folder structure

Step – 3 Creating your StrutsValidation  Program(Controller &Model&Properties)

Step – 4 Creating web.xml for StrutsValidation application

Step – 5 Creating Struts-config.xml

Step – 6 StrutsValidation  view page

Step – 7 How to Add server in  StrutsValidation   application

Step – 8 OutPut  for  Running  StrutsValidation   application

 

Start Eclipse and goto File -> New -> Project -> Dynamic Web Project

 

Step – 1 Creating Dynamic web Project

Following is the list of required JAR files to be added in Java Class Path of your project. Download displaytag JAR files from

JAR FILES + TLD FILES

Step – 2 StrutsValidation   folder structure

 Lib Folder

 

Download The Following TLD files  and paste into WEB-INF

 

Step – 3 Creating your StrutsValidation  Program(Controller &Model&Properties)

LoginAction.java

package Com.Candidjava;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

public class LoginAction extends org.apache.struts.action.Action {

	/* forward name="success" path="" */
	private final static String SUCCESS = "success";

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		return mapping.findForward(SUCCESS);
	}
}

LoginForm.java

package Com.Candidjava;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.validator.ValidatorForm;

public class LoginForm extends ValidatorForm {

	private String userName;
	private String password;

	public LoginForm() {
		super();
	}

	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		if (getUserName() == null || getUserName().length() < 1) {
			errors
					.add("userName", new ActionMessage(
							"error.userName.required"));
		}
		if (getPassword() == null || getPassword().length() < 1) {
			errors
					.add("password", new ActionMessage(
							"error.password.required"));
		} else if (getPassword().length() < 6) {
			errors.add("password",
					new ActionMessage("error.password.minlength"));
		}
		return errors;
	}

	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;
	}
}

ApplicationResource.properties

# -- standard errors --
errors.header=</pre>
<div style="color: red;">errors.prefix=
errors.suffix=
errors.footer=</div>
<pre>
# -- validator --
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.

error.userName.required = User Name is required.
error.password.required = Password is required.
error.password.minlength = Password can not be less than 6 characters.

Step – 4 Creating web.xml for StrutsValidation application


<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>

	<display-name>Struts Blank Application</display-name>

	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>/WEB-INF/struts-config.xml</param-value>
		</init-param>
		<init-param>
			<param-name>debug</param-name>
			<param-value>2</param-value>
		</init-param>
		<init-param>
			<param-name>detail</param-name>
			<param-value>2</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
	</welcome-file-list>
	<taglib>
		<taglib-uri>/tags/struts-html</taglib-uri>
		<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
	</taglib>
	<taglib>
		<taglib-uri>/tags/struts-bean</taglib-uri>
		<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
	</taglib>

</web-app>

Step – 5 Creating Struts-config.xml

<?xml version="1.0" encoding="UTF-8" ?>

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

<struts-config>
	<form-beans>
		<form-bean name="LoginForm" type="Com.Candidjava.LoginForm" />

	</form-beans>

	<action-mappings>
		<action input="/login.jsp" name="LoginForm" path="/login"
			scope="session" type="Com.Candidjava.LoginAction">
			<forward name="success" path="/success.jsp" />
		</action>
	</action-mappings>

	<message-resources parameter="Com.Candidjava.ApplicationResource" />

	<!--
		========= Validator plugiN==================
	-->
	<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
		<set-property property="pathnames"
			value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
	</plug-in>

</struts-config>

validation.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
          "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">

<form-validation>

	<!--
		This is a minimal Validator form file with a couple of examples.
	-->

	<global>

		<!--
			An example global constant <constant>
			<constant-name>postalCode</constant-name>
			<constant-value>^\d{5}\d*$</constant-value> </constant> end example
		-->

	</global>

	<formset>

		<!-- An example form -->
		<form name="logonForm">
			<field property="username" depends="required">
				<arg key="logonForm.username" />
			</field>
			<field property="password" depends="required,mask">
				<arg key="logonForm.password" />
				<var>
					<var-name>mask</var-name>
					<var-value>^[0-9a-zA-Z]*$</var-value>
				</var>
			</field>
		</form>

	</formset>

	<!-- An example formset for another locale -->
	<formset language="fr">

		<constant>
			<constant-name>postalCode</constant-name>
			<constant-value>^[0-9a-zA-Z]*$</constant-value>
		</constant>

		<!-- An example form -->
		<form name="logonForm">
			<field property="username" depends="required">
				<arg key="logonForm.username" />
			</field>
			<field property="password" depends="required,mask">
				<arg key="logonForm.password" />
				<var>
					<var-name>mask</var-name>
					<var-value>^[0-9a-zA-Z]*$</var-value>
				</var>
			</field>
		</form>

	</formset>

</form-validation>

 

Step – 6 StrutsValidation  Struts view page 

login.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>

<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Struts Validation</title>

</head>
<body bgcolor="pink">
<html:form action="/login.do">
	<table>
		<tr>
			<td>User Name</td>
			<td><html:text property="userId"
				errorKey="org.apache.struts.action.ERROR" /></td>
			<td><html:errors property="userId" /></td>
		</tr>
		<tr>
			<td>Password</td>
			<td><html:password property="password"
				errorKey="org.apache.struts.action.ERROR" /></td>
			<td><html:errors property="password" /></td>
		</tr>
		<tr>
			<td></td>
			<td><html:submit value="Login" /></td>
			<td></td>
		</tr>
	</table>
</html:form>
</body>
</html>

success.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="pink">
<h1>Login Success!</h1>
</body>
</html>

Step – 7 How to Add server in  StrutsValidation   application

Window –>show view—->servers—>add new–>          

Select ther version which you installed in your System

How to run the Project:

Right click on project name———–>click Run As —> Run On Server

Step – 8 OutPut  for  Running  StrutsValidation   application

   DOWNLOAD                +  SOURCE CODE

 BACK

Filed in: Z-AStruts Codes

No comments yet.

Leave a Reply