This question already has an answer here:
disabling the Validation in struts2 and use my own customized validation
(1 answer)
Closed 6 years ago.
I'm trying to disable the validation of a method of one class.
I've seen the annotation #SkipValidation but it doesn't work. I tried to put the following interceptor-ref to my action but it doesn't work either.
<interceptor-ref name="validation">
<param name="excludeMethods">ricerca,execute</param>
</interceptor-ref>
I need to remove the validation of a Date field of this class.
What #MohanaRaoSV said is probably the solution:
AnnotationValidationInterceptor is the Interceptor that reads the #SkipValidation Annotation.
It is NOT part of the DefaultStack, so you have to include it manually;
From your code, it seems you are working with the ValidationInterceptor, that knows nothing about those annotations.
Be sure to include it in your stack.
EDIT: Looking in struts-default.xml inside Struts2-core-2.3.15.x.jar, "validation" is now mapped to AnnotationValidationInterceptor, instead of the old ValidationInterceptor; check in your struts-default.xml, if you are running an older version of Struts2;
Related
In struts2, I took advantage of built-in OGNL in struts2, naming my inputs like <input name='bag["item"].property'>
Which went to getters/setters getBag().get("item").setProperty(value)
I've upgraded to struts 2.2.1, and suddently those no longer work: the getter never gets called.
The internet is utterly silent on using OGNL in parameters, as if nobody ever made complex forms.
How do I get my map-parameters back?
It turns out that they hardened restrictions on parameter names to boost security.
So I had to add to my struts.xml:
<interceptor-stack name="defaultStack">
<interceptor-ref name="params">
<!-- For maps to work -->
<param name="acceptParamNames">
[a-zA-Z0-9\.\]\[\(\)_'\s"/]+
</param>
</interceptor-ref>
</interceptor-stack>
(I had "s and /s in my parameter names)
File upload ceased working after that (interceptor stacks are madness), so I had to add it explicity either.
Update: These days I strongly suggest using JSON to pass complex structures instead of rich OGNL forms. Of course you would need some JS.
My configuration: JSF 2.2 - mojara version 2.2.8 Glassfish server 4.0 Primefaces 5.1.7
I have a problem when I try to pass f:param with h:commandLink inside p:dialog inside p:panel. Here is the code for h:commandLink
<h:commandLink action="#{creditApplicationAttachmentBean.uploadFile()}" styleClass="mystyle-btn" id="uploadFileButton"><i/> #{msg['mymessage.upload.button']}
<f:ajax execute="ids of elemets to execute" render="ids of elements to update"/>
<f:param name="showMessageInGlobal" value="false"/>
</h:commandLink>
So when action is fired the param is never set(it is null). I have checked for nested forms and do not have them. No validation errors. So all seems to be good a part from parameter is not set.
Please any help would be much appreciated.
f:param must work with command link but try to use also and for test remove ajax, and the first try to pass parameter. Also check param handle code from JSF bean, if you use f:param, you should get it from parameters map,
here is a link for help
PropertyActionListener
After some searching and asking people around the answer is simple there is a feature with jsf 2.2 of majara implementation f:param does not work with f:ajax. It also allows special parameters defined but nothing else. there has been request made to include it in jsf 2.3 version. So we will wait and see.
I'm doing a project with struts2, Hibernate.
I want struts to validate my form. I have added a MyAction-validation.xml and it works fairly well. (I say fairly well because it doesn't validate on the client side. I have set the validate attribute of the <s:form/> tag to true)
First it provided me some errors and googling it I got that I should add a result with input name. So now I have a result with input name in my action without understanding well how it works and why.
My action returns a plain form when it is called by myAction.action url and when the form is submitted the data goes directly to the action parameters and saved in database. Then a filled form will be shown with a success message. The form fields should be validated upon the submission. But they are validated whenever the action is invoked. I tried #SkipValidation annotation but it cancels the validation completely. Even when I call the validate method in the execute method it doesn't run. I tested it by some System.out.println lines. My action definition in the struts.xml is the following:
<action name="ShowAddItemPage" class="action.clerk.ShowAddItemPage">
<result name="success" type="tiles">addItem</result>
<result name="generalError" type="tiles">clerkGeneralError</result>
<result name="input" type="tiles">addItem</result>
</action>
How can I make the validation work on the client side?
How can I make the validation run only upon the form submission and disable it when there form fields are provided by the application?
What is input result name for and why did I have to add it to the action results?
By setting the validate attribute to true, just like you said.
By having a different action for displaying the form, or by creatong an interceptor that skips validation on a GET (that's what I used to do), etc.
"input" is the result used when validation fails, although you can change it. If validation fails (and by default, type conversion failures as well) it has to go somewhere, and the "input" result is where.
Is there a way to dynamically add an attribute to a struts 2, tag UI tag such as a textfield?
The reason is that I want to add a readOnly form field attribute to an <s:textfield/>, depending on an action's method result. I cannot use readOnly="%{isReadOnly()}" since once the attribute is defined, the form element is read-only, no matter what value it has. And wrapping each form field into an <s:if/> tag is pretty cumbersome and results in a lot of code duplication.
I would also like to avoid JavaScript for interoperability reasons and for not relying on the browser's scripting settings.
If the issue is to use the built in struts2 functionality then one easy option is to render your view with freemarker, which readily supports the dynamic addition of attributes.
If you are using conventions, it is VERY trivial you just need to create a file with a ".ftl" extension, if you are using xml it is also very easy just use the freemarker result type (see here for greater description):
<action name="test" class="package.Test">
<result name="success" type="freemarker">/WEB-INF/content/testView.ftl</result>
</action>
Here is example view using a map to dynamically add attributes (example also taken from liked page):
<#s.textfield name="test" dynamicAttributes={"placeholder":"input","foo":"bar"}/>
The dynamicAttributes would be extremely useful in all JSP UI tags but alas it is not currently implemented.
NOTE: There is one error/omission in the above link. It tells you to add the following line which causes an error in my environment (simply the line is not needed).
<#assign s=JspTaglibs["/WEB-INF/struts.tld"] />
That is, this line in a file all by it self is sufficient for rendering a text element, no explicit tag library declaration needed!
<#s.textfield name="test" dynamicAttributes={"placeholder":"input","foo":"bar"}/>
There are a number of advantages to using freemarker over plain JSPs, so taking a moment to explore the syntax and using it for this one case may prove useful later.
I have a misunderstanding with the use of flash scope in jsf 2.
I want to put an object in flash map during my action and show this object in the next page.
Here is my action (call by h:commandLink):
public String showDetail(PersonneDTO personne) {
MessageFlashBean message = new MessageFlashBean();
message.addMessage("test");
FacesContext.getCurrentInstance().getExternalContext().getFlash()
.put("test", message);
return "showDetail";
}
It's just a test, but the real object I want to use is more complex.
Here is the second page with the flash use:
<ui:fragment rendered="#{flash.test != null and flash.test.hasMessage()}" >
<ui:repeat var="message" value="#{flash.test.messages}">
#{message}
</ui:repeat>
</ui:fragment>
Here is the navigation (and here is my problem :) ):
<navigation-rule>
<from-view-id>/index.xhtml</from-view-id>
<navigation-case>
<from-outcome>showDetail</from-outcome>
<to-view-id>/private/showDetail.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
In this case, the flash scope works fine and my message appears.
But if I add a redirect in the navigation case, my object in the flash map has disappeared.
I don't understand why I can't use redirect in the navigation rule (I believed it was the goal of flash scope).
May be something is wrong in my code.
Someone can help me ?
Thanks
The Mojarra's implementation of the JSF2 Flash scope had a lot of problems. Most of them were resolved with every new Mojarra release. Currently, with the latest Mojarra version, the 2.1.6, there's as far as I know still only one major open problem: it won't work if you redirect to a different base path. See also the (currently) last comment in the long discussion in issue 1751.
Right now, you have basically the following options:
Forget using Flash when you need to redirect to a different base path. Use a request parameter instead or try to simulate the flash scope with a custom cookie.
If possible, reorganize the folder structure so that you don't need to redirect to a different base path when you need the flash scope to survive.
Try MyFaces instead of Mojarra. Maybe it does it better with regard to flash scope.
Update: as per issue 2136, the last open problem of Flash scope not being available after a redirect on a different path is solved in the shortly upcoming Mojarra 2.1.14. So with this version, all Flash scope related problems discovered and open so far
should be solved and closed.
jsf 2.1 with netbeans has this problem with flash. using /somepage?redirect-faces=true or /somedirectory/somepage?faces-redirect=true does not work. it has to be somepage?redirect-faces=true.
tested mojarra 2.1.7, but it didn't have the above problem.