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.

user3678383
  • 105
  • 11

1 Answers1

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.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • 1
    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 – Andrea Ligios Oct 21 '15 at 17:01
  • Will do it and let u know – user3678383 Oct 21 '15 at 17:06
  • Andrea Ligios i have made the edits as suggested by you. Please go through it. – user3678383 Oct 23 '15 at 04:57
  • In JSP it is still name, not ename – Andrea Ligios Oct 23 '15 at 09:28