How to validate a form in action class and forward errors to the jsp in struts?

How to validate a form in action class and forward errors to the jsp in struts?

How to validate a form in action class and forward errors to the jsp in struts?

View Answers

April 1, 2011 at 4:11 PM

1)index.jsp:

<html>
<head>
<title>Loading..........</title>
<meta http-equiv="refresh" content="0;URL='formAction'" >
</head>
<body>
Loading.....
</body>
</html>

2)ValidationAction.java:

package net.roseindia.action;

import net.roseindia.model.UserModel;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class ValidationAction extends ActionSupport implements ModelDriven {
    UserModel obUserModel;

    @Override
    public String execute() throws Exception {

        return SUCCESS;

    }

    public void validate() {
        if (obUserModel.getName().length() == 0) {
            addFieldError("name", "Name is required.");
        }
        if (obUserModel.getAge().length() == 0) {
            addFieldError("age", "Age is required.");
        }
        if (obUserModel.getAddress().length() == 0) {
            addFieldError("address", "Address is required.");
        }
    }

    @Override
    public Object getModel() {
        // TODO Auto-generated method stub
        obUserModel = new UserModel();
        return obUserModel;
    }

}

3)UserModel.java:

package net.roseindia.model;

import java.io.Serializable;

public class UserModel implements Serializable {
    private String name;
    private String age;
    private String address;
    private int phone;

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getPhone() {
        return phone;
    }

    public void setPhone(int phone) {
        this.phone = phone;
    }
}

April 1, 2011 at 4:14 PM

4)userform.jsp:

<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>User form</title>
<s:head/>
</head>
<body>
<h1>User information form...</h1><hr/>
<s:form action="formvalidation">
    <s:textfield key="name" label="Name" />
    <s:textfield key="age" label="Age" />
    <s:textfield key="address" label="Address" />
    <s:submit />
</s:form>
</body>
</html>

5)struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.devMode" value="false" />
    <package name="roseindia" extends="struts-default">
        <action name="formAction" >
            <result name="success">/userform.jsp</result>
        </action>
        <action name="formvalidation" class="net.roseindia.action.ValidationAction">
            <result name="input">/userform.jsp</result>
        </action>
    </package>
</struts>









Related Tutorials/Questions & Answers:
How to validate a form in action class and forward errors to the jsp in struts?
how to display action errors in jsp which is in a form list
Advertisements
how to forward select query result to jsp page using struts action class
JSP Forward action
jsp forward action tag
How to disable browser back and forward button after logout [in struts2 or jsp]
how to validate duplicate records in struts1
how to validate duplicate records in struts1
How to validate a form - JSP-Servlet
form validate and perfom action immediatly
Understanding Struts Action Class
how to send my system.out.println in a class file to a jsp in Struts2
how to point my jsp form action to servlet? - JSP-Servlet
How can i pass the valus from a JSP to the action class???
JSP forward
how send nsnotification to forward class
download file Error in struts2 action class
How to pass Array of string from action class to jsp page
jsp request in struts1
jsp:forward tag ussage
How to validate form using Spring MVC?
how to pass string array from action class to jsp page? pls reply me.
struts2 how can we add an radio button to form using a class(doa)
struts2 how can we add an radio button to form using a class(doa)
linking tree heading in javasript into a Jsp file and then jsp to struts action form
How to forward the control from one jsp to another?
how to validate all form field values at once.
DTO & Action class - Struts
How to use struts2 grid with form submission
Jsp Action Tags
Struts1 vs Struts2
jsp forward not functioning
Action form
How to Assign struts2 property to jsp variable
How to Assign struts2 property to jsp variable
jsp errors - JSP-Servlet
how to implement ajax in struts2 to operate on selectbox in jsp
How to implement ajax in struts2 to operate on select box in jsp
jsp:forward - JSP-Servlet
how to validate e,ail id and phne number in student form
how to validate e,ail id and phne number in student form
how to display output on jsp from while loop of action class....actually i am retreiving the post from posts column from my sql.plz help.thnkss
Login form in Struts2 version 2.3.16
Create Action class
Action without a form.
Forward a Jsp Page
Calling Action on form load - Struts
What is Action Class?
Struts ForwardAction vs Forward tag in jsp - Struts
javascript call action class method instruts

Ads