Hi All
I am facing a unique problen.I am using
Struts ValidationFrameWork.I have user Struts-config.xml file.I am not able to use validator.
Here i am giving my code.Pl. tell me where i am wrong.
Struts-config.xml
---------------------------
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="UserForm"
type="com.mkyong.user.form.UserForm" >
</form-bean>
</form-beans>
<action-mappings>
<action
path="/login"
type="org.apache.struts.actions.ForwardAction"
parameter="/src/main/webapp/pages/login.jsp"/>
<action
path="/RegisterUserPage"
type="org.apache.struts.actions.ForwardAction"
parameter="/src/main/webapp/pages/RegisterUser.jsp"/>
<action
path="/Register"
type="com.mkyong.user.action.UserAction"
name="UserForm"
scope="request"
validate="true"
input="/pages/Input.jsp">
<forward name="success" path="/pages/ThanksYou.jsp"/>
</action>
</action-mappings>
<message-resources
parameter="com.mkyong.user.properties.ApplicationResources.properties" />
<plug-in className="org.apache.struts.validator.ValidatorPlugIn" >
<set-property property="pathnames"
value="/src/main/webapp/WEB-INF/validator-rules.xml,/src/main/webapp/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
Validation.xml
-----------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
<form-validation>
<formset>
<form name="userForm">
<field property="username" depends="required">
<msg name="required" key="errors.username.required" />
</field>
<field property="pwd" depends="required">
<msg name="required" key="errors.pwd.required" />
</field>
</form>
</formset>
</form-validation>
Validator-rules.xml
--------------------------------
<form-validation>
<global>
<validator
name="required"
classname="org.apache.struts.util.StrutsValidator"
method="validateRequired"
methodparams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
depends="required"
msg="errors.required"/>
<validator name="minlength"
classname="org.apache.struts.util.StrutsValidator"
method="validateMinLength"
methodparams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
depends="required"
msg="errors.minlength"/>
</global>
</form-validation>
ApplicationResources.properties
--------------------------------------------
#Normal resource bundle messages
label.username=First Name
label.pwd=Last Name
#Error messages used by the Validator
errors.required={0} is required.
errors.invalid={0} is invalid.
RegisterUser.jsp
----------------------------------
<%@ taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
</head>
<body>
<h2>Struts - Validator Example</h2>
<b>User Registeration Form</b>
<br/><br/>
<font color="red">
<html:errors/>
</font>
<html:form action="/Register">
<br/>
<bean:message key="label.username" /> :
<html:text property="username" size="20"></html:text>
<br/>
<bean:message key="label.pwd" /> :
<html:text property="pwd" size="20"></html:text>
<br/>
<html:submit>
<bean:message key="label.button.submit" />
</html:submit>
</html:form>
</body>
</html>
UserForm.java
-----------------------------
package com.mkyong.user.form;
import org.apache.struts.validator.ValidatorForm;
public class UserForm extends ValidatorForm{
String username;
String pwd;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
UserAction.java
----------------------
package com.mkyong.user.action;
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.ActionForward;
import org.apache.struts.action.ActionMapping;
public class UserAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("success");
}
}
web.xml
-----------------
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Struts Hibernate Examples</display-name>
<
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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
I am new to ValidationFrameWork.When i type
http://loclahost:8080/StrutsExample/RegisterUserPage.do
it says "Servlet action is not available".
Pl. tell me where i doing wrong.
Thanls and regards to all.
Rajesh