When using
<s:token/>
with Struts 2 I got this warning message in the log
WARN [com.opensymphony.xwork2.ognl.OgnlValueStack] - <Error setting expression
'struts.token.name' with value '[Ljava.lang.String;#7023f'>
How can I avoid that?
Regards
It looks like you have multiple <s:token/> in your form.
[Ljava.lang.String
indicates that a String array is send, that happens if your form sends the same field multiple times.
I found that cannot avoid WARN's but we can setup a filter with Log4J,
check this...
https://cwiki.apache.org/S2WIKI/troubleshooting-guide-migrating-from-struts-20x-to-21x.html#TroubleshootingguidemigratingfromStruts2.0.xto2.1.x-MissingProperties
I had the same error. In my case, the following was missing in struts.xml:
<interceptor-ref name="token"/>
Inside my action.
Related
We're using Struts 2 in our web application. There are bunch of action mappings defined already, I want to implement a feature where any Urls starting with /buy and not mapped to any of the existing action mappings e.g. /buy/new or buy/old should be redirected to buy/index action. For example if someone is trying to go to /buy/bla1 or /buy/bla2 or buy/bla1/bla2/bla3 should go to buy/index.
Define the following action in your /buy package:
<action name="*">
<result type="redirectAction">index</result>
</action>
Alternatively you can just use:
<default-action-ref name="index" />
But then you will get no redirect. Instead the index page will show under the nonexisting address.
You also need to set the following parameter in your struts.properties:
struts.action.extension = action,,
You can add other extensions to the parameter, but as far as i know there is no wildcard for all extensions. The blank string covers directories like /buy/bla but /buy/bla.x won't be covered and will produce a 404 error unless you add x to the list of extensions.
If this is not good enough you might be better off solving this by using redirect rules in the webserver.
You can use default action reference and provide a JSP success path to it.
If any unmatched action is received then it will call the given action and forward it to the result JSP page.
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.
I'm using Struts 2.1.8.1. I have a requirement to embed some pages from another server on my own app, so the users will access to them through my application, without accessing directly the other server. My idea is to have a package definition for that, so any access to that package would be redirected to the internal server.
<package name="eco-marketing" namespace="/marketing" extends="eco-default">
<action name="*">
<result name="success" type="dispatcher">
<param name="location">http://myotherserver:8080/test/{1}</param>
</result>
</action>
</package>
But it does not work, I got a Error 404--Not Found, so I suppouse is not as easy as it sounds. Any ideas on how to do this?
TIA
I'll assume that you are accessing just html, then see: http://download.oracle.com/javase/tutorial/networking/urls/readingWriting.html
I would recommend creating an action in that package to do the work for you, and use the value of the parameter to get the required data.
You probably already know but an iframe in the consumer action will make this easier to use than trying to parse what you need out.
After you have that figured out, if you decide to create a custom result type, please post it back to us here it would be very interesting.
An example of a custom result type can be found here (4th code block from the top): http://siriwardana.blogspot.com/2008/12/creating-custom-result-type-struts-2.html
I have what I'd think is a common issue but an entire day of googling hasn't turned up anything useful.
I have a form with a checkbox and a textfield. I'd like to do a regex validation of the textfield, but only if the checkbox is selected. Regex validation is currently working for other non-conditional fields but I can't for the life of me figure out if there is a syntax that allows for this in the action-validation.xml file. ie. I have something like below for other fields. What I need is a way of making this conditionaly evaluated only if the checkbox is selected.
<validators>
<field name="sn">
<field-validator type="regex" >
<param name="expression">
[0-9]{12}
</param>
<message>Serial number format is invalid. Please try again</message>
</field-validator>
</field>
</validators>
Does anyone have a code example of how to do this?
Any help is much appreciated.
For Struts 1 there was a validation rule called "validwhen" that you could use to perform complex validations, but for Struts 2, as far as I know there is no such validation.
For Struts 2 you could go with the Expression Validator in which you can specify an OGNL expression to use, so I guess you could try something like this:
<validator type="expression">
<param name="expression">checkboxField eq "selected" and inputText eq "bla"</param>
<message>....</message>
</validator>
One thing I don't know is if there is a easy way of doing your regular expression check using OGNL (you'll have to look into it).
Additionally, if that does not work, you can always write your own custom validator.
The expression and fieldexpression validators take an OGNL expression. Evaluating to true passes false fails. You can use them quite easily for this requirement.
Personally I would just use the built in validators via XML like you're doing and then implement any custom validation logic in the validate() method (or methodname-validate() method). Just preference, no good reason really.
Every tutorial I have found on Struts2 Declarative validation explains how to make the fields validated which is nice and easy. But how do you enter the page with out it being validated?
I have the below Action mapped
<package name="admin" namespace="/admin" extends="struts-default">
<action name="display_*" class="action.admin.AdminAction" method="display">
<result name="input">/WEB-INF/pages/secure/admin/adminUsers.jsp</result>
<result name="success">/WEB-INF/pages/secure/admin/adminUsers.jsp</result>
</action>
Shouldn't I be able to call it with admin/display_input.action to skip validation?
You are using defaultStack (it's activated by default), see http://struts.apache.org/2.0.11/docs/interceptors.html (<default-interceptor-ref name="defaultStack"/>)
It contains validation interceptor which will ignore validation only on methods input,back,cancel,browse. So you can action via one of these methods, or you can use interceptor stack without validation interceptor (basicStack, for example).
You also can annotate your action method with #SkipValidation annotation.
Maybe this will help?
http://www.mail-archive.com/user#struts.apache.org/msg69320.html
Try setting the method attribute to method="{1}".