I am writing a web application with struts2 + Jquery plugin.
I am using AJAX form submission with sj:submit tag from jquery plugin.
I am facing an issue while validating the form (AJAX) with "validate= true" tag in sj:submit.
Following is the sj:submit tag:
<sj:submit value="ADDUSERNOW" onBeforeTopics="before" onSuccessTopics="success"
onErrorTopics="errorState" onCompleteTopics="complete" targets="userMgmntDiv" validate="true"/>
My html head section also contains references to two js files required for validation as below.
<script language="JavaScript" src="${pageContext.request.contextPath}/struts/utils.js" type="text/javascript"></script>
<script language="JavaScript" src="${pageContext.request.contextPath}/struts/xhtml/validation.js" type="text/javascript"></script>
Validation xml is given below which is named as ActionClass-validation.xml and is placed in the same package as that of a class on which validation is invoked:
<validators>
<field name="userModel.userName">
<field-validator type="requiredstring">
<message>UserName Is Required</message>
</field-validator>
</field>
</validators>
After form is submitted , no validation is triggered , and execution proceeds and struts2 action class is called.
Is there anything I am missing here ? I have searched a lot but couldn't find any answer .Please help
struts2-core version : 2.3.4
struts2-jquery-plugin :3.3.3
Action configuration looks loke below :
<action class="addUserAction" method="addUser">
<interceptor-ref name="jsonValidationWorkflowStack"/>
<result>addUser.jsp</result>
</action>
In addition to above,I am using spring + struts2 + hibernate for achieving above task.When I tried the example as shown in struts2-jason wiki page : ,I am getting error as : cannot find action with class named AddUserAction(which is a bean in spring-beans.xml)
It seems that error is thrown when validator inteceptor is invoked.
When I comment out the annotations used for validation on the action class - AddUserAction, this error vanishes and page loads successfully.
Following are my questions :
1)How can I use spring alongwith struts2 convention plugin ? This is because , convention plugin uses its own mechanism for mapping urls and action (using annotations or default behaviour).
2)Previuosly , I was expecting struts.xml file can be used for url to action mapping , and annotations can be used for validations. (using convention plugin)But this approach doesn't seem to work.(convention plugin + struts.xml doesn't work)
struts2 ajax validation is a client side validation, which provided natively by dojo, here is the doc for Struts2 Ajax validation. struts2-jquery plugin provides this feature using jsonValidationWorkflowStack, you can use it like this: Example
I've tried. It's work very well. But if your form has an input file for upload, the ajax validation don't work.
Related
I have a project based in Spring Web model-view-controller (MVC) framework. The version of the Spring Web model-view-controller (MVC) framework is 3.2.8.
I want to use a message inside a form:select, but I got an error:
org.springframework.beans.NotReadablePropertyException: Invalid property `'<fmt:message key=' of bean class`
The JSP
<form:select path="cancelledSubStatus"
id="cancelledSubStatusId"
items="${cancelledSubStates}"
itemValue="key"
itemLabel="<fmt:message key="${key}" />"
cssClass="c-select"
/>
outside of the form you can do:
<fmt:message key="your.message" var="ItemLabelMessage" />
so you are saving the message in that variable and then inside of the form select you access that variable by doing:
itemLabel="${ItemLabelMessage}"
There it should work
Hope it works! Good luck
I wrote an interceptor for struts2 application and configured in struts configuration file. Here i am using this interceptor for only some actions , not for globally.So here my problem is when validation errors are coming from ActionName-validation.xml at that time my custom interceptor is not working.Please give me suggestion/solution.
First of all, read how the validation (and conversion) error are handled by Struts2, then move your Interceptor BEFORE the Validation/Parameters/ConversionError Interceptors
The easier way is to define a new stack with your interceptor at first (or in the middle, "exploding" the defaultStack) like follows:
<interceptor-stack name="customStack">
<interceptor-ref name="customInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
And then apply customStack or defaultStack action by action or package by package, according to your needs.
I am developing a myCustomTag.tag file to implement my custom tag in spring MVC supported project. I know that the developer using the myCustomTag would use the myCustomTag within the spring provided tag. For E.g. :
<form:form action="..." commandName="...>
<myprefix:mycustomTag ............ />
</form:form>
Is it a good design to use a spring's MVC tag ( let's say form:input tag) within a custom myCustomTag.tag file implementation ?
Absolutely, I don't see a reason why you would not want to use spring tags within a custom tag.
For example, for a large form, you might want to separate sections into their own tags and spring tags are perfectly ok to be used there (ie, if it is syntactically allowed).
I am working for first time with Struts 2 + Spring + Hibernate architecture. I took this project as reference for building it and it works. I can list DB tables in my index.jsp using struts tags, but this does not work from nested JSP loaded into DIV containers inside index.jsp.
index.jsp has a <div class="art-nav"></div> and loads there another jsp using js:
$(".art-nav").load("./menu.jsp");
The same struts tags that work in index.jsp to list DB tables do not work in menu.jsp. I am not sure if the problem is the way I am loading this JSP or if it is necessary to execute some action from Struts 2 before loading menu.jsp...
Basically the JSPs use AJAX and I am adapting them to this architecture and there is where I am facing the problems because of my lack of experience.
Thank you in advance for your help!
You should include the second jsp as follows:
<div class="art-nav">
<%# include file="/WEB-INF/jsp/menu.jsp"%>
</div>
If you load your jsp via javascript the response will not get forwarded to that jsp.
If you really want to load a jsp via javascript you should create a new action (menu.action) that returns a parsed jsp and include it in the existing html. (Though I personally do not really like that technique.)
In the Spring docs, the section on multipart support has an example of specifying a multipart form as this:
<form method="post" action="upload.form" enctype="multipart/form-data">
This doesn't use the Spring form tags which I am familiar with and instead submits the form to the upload.form action.
How does Spring know what upload.form means? Do you have to create a mapping for it and, if so, where is this done?
upload.form is the URL of the HTTP POST request that is created by this form. It will be relative to the URL of the view that generated the form in the first place, so if the form shown above is rendered by /mysite/myapp/myform.html, it will submit to /mysite/myapp/upload.form
It then becomes a matter of configuring Spring to handle "/upload.form" request accordingly, such as through #RequestMapping if you're using annotation-based Spring MVC. See here for more information.