• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Struts ValidationFrameWork

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you implemented validate() method in your form bean?

----
Peter
 
Sharma Rajesh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Peter

Thanks a lot for reply.
No i haven't declared validate() method in UserForm class.
Pl. could you tell me what to declare in validate method.

Thanks & Regards
Rajesh
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajesh,

By the concept of validation framework ,it is not needed to write validate() in form bean.

And you have mentioned <msg name="required" key="errors.username.required" /> , include this key with a value in properties file.

Check whether the path value mentioned in parameter or the page(RegisterUser.jsp) is available or not. (parameter="/src/main/webapp/pages/RegisterUser.jsp")
 
Sharma Rajesh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Keerthi
Thanks for reply.
Every thing is right but still it is giving the same error(Servlet action in unavailable).

Thanks
Rajesh
 
keerthi vineela
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you deployed struts1 jars or if the application is not deployed correctly so that it not able to parse the struts-config file and initialize the servlet properly or the type and name values in the struts-config file are not given properly.Mostly ,these are the reasons to produce that error.

Please modify set-property value in PlugIn to value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" .Please refer below sample code

<plug-in className="org.apache.struts.validator.ValidatorPlugIn>
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dear frnds
if you found warning Resource your properties file name_en_US something then you have
mistake something like
1) we did not keep properties file in classes folder.(so keep in classes folder and extension should be properties )
2)check index.jsp action attribute values are same as struts-config.xml action tag path attribute value.
3)tag message-resources here s are must
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic