0

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>Add new product</title>
    </head>
    <body>
        <s:form action="emp1" method="post">
            <s:textfield label="Name" name="name" ></s:textfield>
            <s:textfield label="Age" name="age" ></s:textfield>     
            <s:submit value="Save" align="left"></s:submit>     
        </s:form>
    </body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" 
version="3.0">

    <display-name>DemoValidation</display-name>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

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="default" extends="struts-default">
        <action name="emp1" class="controller.Employee">
            <result name="success">/success.jsp</result>
            <result name="input">/index.jsp</result>            
        </action>
    </package>
</struts>

Employee.java

package controller;

import com.opensymphony.xwork2.ActionSupport;

public class Employee extends ActionSupport{

    private String ename;
    private int age;

    public String execute(){
        return SUCCESS;
    }

    public String getEname() {
        return ename;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

Employee-validation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC 
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"

<validators>

    <field name="ename">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>Name is required</message>
        </field-validator>
    </field>

    <field name="age">          
        <field-validator type="int">
            <param name="trim">true</param>
            <param name="min">21</param>
            <param name="max">40</param>
            <message>Age should be between 21 to 40</message>
        </field-validator>      
    </field>

 </validators>

I have used the above code in order to demonstrate the validation for struts2. The validation is not triggered , even if the validate fails , success page gets displayed. Kindly go through the code and suggest me the changes.

13
  • Where did you put your Employee-validation.xml file? Commented Oct 20, 2015 at 9:36
  • in the same folder where my Employee.java is present Commented Oct 20, 2015 at 9:42
  • 1
    Which S2 version ? You should use the new filter, FilterDispatcher is deprecated. Commented Oct 20, 2015 at 9:58
  • 1
    Make sure that it is deployed also in the same location. Commented Oct 20, 2015 at 9:59
  • 1
    @user3678383 If you want to thank me so much, why not upvoting the linked answer as suggested ? That's the way you say thanks on StackOverflow ;) P.S: I've just realized you've never voted anything. To vote up, simply press the arrow up in the top left corner of an answer (or a question). Commented Oct 20, 2015 at 13:05

1 Answer 1

2
  1. Apply the new filter because the FilterDispatcher is deprecated;

  2. Change your wrong definition from

    <!DOCTYPE validators PUBLIC
        "-//Apache Struts//XWork Validator Definition 1.0.3//EN"
        "http://struts.apache.org/dtds/xwork-validator-definition-1.0.3.dtd">
    

    to

    <!DOCTYPE validators PUBLIC 
        "-//Apache Struts//XWork Validator 1.0.3//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
    

As you can see in the Apache directory (http://struts.apache.org/dtds/) there are xwork-validator 1.0, 1.0.2, 1.0.3 but xwork-validator-definition is only 1.0, and is not the right one, according to the documentation.

Sign up to request clarification or add additional context in comments.

4 Comments

Very weird. Can you please edit your question by updating the code according to the modification made ? DTD, new Filter etc... even if minified and with name changed, the code must be the same of your real code, otherwise you could avoid posting something critical to the problem without noticing. Also pay attention to constructors (there must always be a no-args one, implicit or, when another is specified, explicit) and getters/setters for variables with one letter lowercase and another capitalized eg "fBar" that Eclipse creates in a wrong way
Will do it and let u know
Andrea Ligios i have made the edits as suggested by you. Please go through it.
In JSP it is still name, not ename

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.