validation Archives - Ebhor.com Read The Latest Post Java, Css, Html, Php learning articles Wed, 01 Feb 2023 11:38:39 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 https://www.ebhor.com/wp-content/uploads/2021/05/ebhor_logo_100x.jpg validation Archives - Ebhor.com 32 32 Struts1 Validation https://www.ebhor.com/struts1-validation/ Wed, 01 Feb 2023 11:10:00 +0000 http://ebhor.com/?p=769 Process of verifing the format & correctness of the html/jsp form data like required field are typed or not. There are two types of validation in any application. client side validation.(using javascript .js) server side validation.(using server side technology like servlet,jsp) Struts based server side validation using programmatic approach using validate(—-) of formBean class. using ... Read more

The post Struts1 Validation appeared first on Ebhor.com.

]]>
Process of verifing the format & correctness of the html/jsp form data like required field are typed or not.
There are two types of validation in any application.

  1. client side validation.(using javascript .js)
  2. server side validation.(using server side technology like servlet,jsp)

Struts based server side validation

  1. using programmatic approach using validate(—-) of formBean class.
  2. using Declerative approach using validator f/w plugin.

Server side validation example

When ever we deploy this program in any server this program we used programmatic validation logic those validation logic written in SubmitForm.java files under of Validate methods.with the help of Application.resources.properties file this generate any errors messages again with the help of tags in jsp pages it display errors messages.
Example deploy the application and we input the given field in browser if forget to fill any singles field and click submit button then result display enter those field.

WelcomeStruts.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>

<html:html locale="true">
    
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>submit</title>
        <html:base>
    
    <center>
<h1>Example Submit Page</h1>
</center>
        <html:errors>
            <html:form action="submit.do">
                </html:form></html:errors>
<table align="center" width="500" cellspacing="10">

<tbody>
<tr>
<td align="right">Last Name:</td>
<td> <html:text property="lastName"></html:text></td>
</tr>


<tr>
<td align="right">Address:</td>
<td> <html:textarea property="address"></html:textarea></td>
</tr>


<tr>
<td align="right">Sex:</td>
<td><html:radio property="sex" value="M">Male 
                                      <html:radio property="sex" value="F">Female
</html:radio></html:radio></td>
</tr>


<tr>
<td align="right">Married:</td>
<td> <html:checkbox property="married"></html:checkbox></td>
</tr>


<tr>
<td align="right">Age:</td>
<td><html:select property="age">
                            <html:option value="a">0-19</html:option>
                            <html:option value="b">20-49</html:option>
                            <html:option value="c">50-</html:option>
                </html:select></td>
</tr>


<tr>
<td colspan="2" align="center"><html:submit></html:submit></td>
</tr>

</tbody>
</table>


            <center>
<h3>
                    <logic:present name="lastName" scope="request">
                        Hello
                        <logic:equal name="submitForm" property="age" value="a">
                            young
                        </logic:equal>
                        <logic:equal name="submitForm" property="age" value="c">
                            old
                        </logic:equal>
                        <bean:write name="lastName" scope="request">
                    </bean:write></logic:present>
</h3>

        </center>
    
</html:base></html:html>

web.xml

<!--?xml version="1.0" encoding="UTF-8"?-->
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <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>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>welcomeStruts.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

struts-config.xml

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




<struts-config>
    <form-beans>
        <form-bean name="SubmitForm" type="com.myapp.struts.SubmitForm">
    
    </form-bean></form-beans>
    
    <global-exceptions>
    
    </global-exceptions>

    <global-forwards>
        <forward name="welcome" path="/Welcome.do">
    </forward></global-forwards>

    <action-mappings>
        <action input="/welcomeStruts.jsp" name="SubmitForm" path="/submit" scope="request" type="com.myapp.struts.SubmitAction">
        <forward name="success" path="/welcomeStruts.jsp">
			<forward name="failure" path="/welcomeStruts.jsp">
            </forward></forward></action>
    </action-mappings>
    
    <controller processorclass="org.apache.struts.tiles.TilesRequestProcessor">

    <message-resources parameter="com/myapp/struts/ApplicationResource">    
    
</message-resources></controller></struts-config>

ApplicationResources.property

errors.header=
<h4>Validation Error(s)</h4>
<ul>
errors.footer=</ul>

<hr>


error.lastName=
 	<li>Enter your last name
error.address=</li>
 	<li>Enter your address
error.sex=</li>
 	<li>Enter your sex
error.age=</li>
 	<li>Enter your age

</li>

SubmitForm.java

package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class SubmitForm extends org.apache.struts.action.ActionForm {
    
    private String lastName = null; // default value
	  public String getLastName() {
		return (this.lastName);
	  }
	  public void setLastName(String lastName) {
		this.lastName = lastName;
	  }

	  /* Address */
	  private String address = null;
	  public String getAddress() {
		return (this.address);
	  }
	  public void setAddress(String address) {
		this.address = address;
	  }

	  /* Sex */
	  private String sex = null;
	  public String getSex() {
		return (this.sex);
	  }
	  public void setSex(String sex) {
		this.sex = sex;
	  }

	  /* Married status */
	  private String married = null;
	  public String getMarried() {
		return (this.married);
	  }
	  public void setMarried(String married) {
		this.married = married;
	  }

	  /* Age */
	  private String age = null;
	  public String getAge() {
		return (this.age);
	  }
	  public void setAge(String age) {
		this.age = age;
	  }

	  public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
   	  {
		  // Check for mandatory data
		  ActionErrors errors = new ActionErrors();

		  if (lastName == null || lastName.equals("")) {
			errors.add("Last Name", new ActionError("error.lastName"));
		  }
		  if (address == null || address.equals("")) {
			errors.add("Address", new ActionError("error.address"));
		  }
		  if (sex == null || sex.equals("")) {
			errors.add("Sex", new ActionError("error.sex"));
		  }

		  if (age == null || age.equals("")) {
			errors.add("Age", new ActionError("error.age"));
		  }

		  return errors;
 	 }
}

SubmitAction.java

package com.myapp.struts;

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 SubmitAction extends org.apache.struts.action.Action {
    
    private final static String SUCCESS = "success";
    
    
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        SubmitForm f = (SubmitForm) form; // get the form bean

			// and take the last name value
			String lastName = f.getLastName();

			// Translate the name to upper case
			//and save it in the request object
			request.setAttribute("lastName", lastName.toUpperCase());

			// Forward control to the specified success target
			return (mapping.findForward("success"));
    }
}

The post Struts1 Validation appeared first on Ebhor.com.

]]>