Share on Google+Share on Google+

Upul Udukala
How to validate a form in action class and forward errors to the jsp in struts?
2 Answer(s)      6 years ago
Posted in : Struts

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

Ads

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:

<[email protected] 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:
Advertisements
Tutorials   
Java Spring Hibernate Struts Training Dynamic tabs in struts 1.2 Unable to upload a file to mysql database using struts1 Pagination in struts 2 struts2 filterdispatcher is deprecated please use the new filters viewing How to add struts2 dependency in Maven? Struts Exception Handling can you check below code if any erors multimedia how to display the value of termcell name in label class not found exception Where to learn Struts 2? dropdown cacheing Upload and Download Large files in jsp What is Struts 2 framework struts and hibernate based health care project source code struts and hibernate based health care project source code Struts 2 Integration with Struts 1 About captcha of strut2 About captcha of strut2 Which is the good website for struts 2 tutorials? advance java source code source code In Struts 2 how to pass the Struts struts internationalization for Korean language struts2 Struts2 and Hibernate Fetch Data Base Value Struts2 and Hibernate Fetch Data Base Value Struts2 and Hibernate i want to create an application with only a button which on click displays table from database using struts2 and hibernate on eclipse Struts2 and Hibernate Struts2 and Hibernate Struts2 validation Procedure Struts2 validation Procedure Struts2 validation Procedure Struts2 validation Procedure Struts 2 action-validation.xml not loading Struts-2 and Ajax integration STRUTS QUESTION conerting web project into .exe file About Select tag ScartApplication aplicacion ecommerce struts 1.3 struts 1.3 sms prob Need to know struts1 all jar file names struts with hibernate

Ads

 
Advertisement null

Ads