Reputation: 125
I am new at Java Web. I follow a tutorial about struts 2 at: http://viralpatel.net
I success at create server-side validation, but the client-side do not work. When submit, I notice that a javascript method is not defined. Try viewing source, I see no script is generated. This is generated HTML source
http://pastebin.com/Lc49jnMs
There is no javascript 'validateForm_customer()' method.
In customer.jsp, I have added validate attrubute:
<s:form action="customer.action" method="post" theme="xhtml" validate="true">
and also added:
<s:actionerror/>
<s:fielderror />
and
<s:head/>
In struts.xml:
<action name="customer" class="mypackage.CustomerAction">
<result name="success">/success.jsp</result>
<result name="error">/customer.jsp</result>
</action>
In code, i have extended ActionSupport, and I have CustomerAction-validation.xml file. Only client-side validation do not work, the server-side validation do work perfectly.
I am using struts 2.1.6. I don't know but the newer vesion also do not work with me. It build success but have some error in dispatcher initialize, so when using taglib "struts-tags", it throw an error
The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location]
caused by
org.apache.struts2.views.gxp.inject.InjectedObjectContainer
class not found!
The struts 2.1.6 seems work good until i try using validation.
What did I do wrong?
Thanks and sorry for my bad English
Upvotes: 3
Views: 4161
Reputation: 1064
After a long trial i figured out that the validation doesn't works on welcome page set in the web.xml file, so some redirect must be carried out inside welcome page and redirect it internally to next page. And this redirect is not noticed by the user. Below is a sample code of my workings.
index.jsp
<!--importing jslt library to redirect the page (jar required jstl.jar and standard.jar) -->
<%@ taglib prefix="j" uri="http://java.sun.com/jsp/jstl/core" %>
<!--redirection the index page to some action named index see strut.xml file -->
<j:set var="baseUrl" scope="session" value="http://127.0.0.1:8080/strutsBasic/"/>
<j:redirect url="index" />
struts.xml
<struts>
<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources"
value="ApplicationResources" />
<package name="default" extends="struts-default" namespace="/">
<!-- at this point redirect is mapped to login page -->
<action name="index">
<result>/login.jsp</result>
</action>
<action name="login"
class="com.pkg.action.LoginAction" method="execute">
<result name="success">/regform.jsp</result>
<result name="error">/login.jsp</result>
</action>
<action name="register"
class="com.pkg.action.LoginAction" method="register">
<result name="success1">success.jsp</result>
<result name="input">regform.jsp</result>
</action>
</package>
and the rest of the code and validation are same as of @Viral Patel
Upvotes: 1
Reputation: 895
Firstly, I strongly recommend you to use the latest version (for obvious security reasons,), which currently is 2.3.1.1
Then remove struts2-gxp-plugin (it looks like you don't need it), after Then see what happens
Upvotes: 1
Reputation: 66637
Struts tags are only usable when the request has passed through its servlet filter It seems you missed filter mappings in web.xml for the tags you defined. Make sure they are there.
Upvotes: 0