I am using struts-2.3.16.3 for my application. My action implements the “ModelDriven” interface. Using interceptor reference as defaultStack.
The problem that i am facing is, all the special characters (non keyboard characters like ®, ℗) are disappearing from my model property by the time they reach to my action class. Other special characters like # # $ etc able to see those in my action.
Do i need to configure any other interceptors in the stack?. Help needed if i am missing in configuration.
Example: If i enter Piracy℗symbol in my text field, in action class when i print property value it shows Piracysymbol.
Thanks,
Ramesh
I think the problem is that you have not mentioned the content-type. Try putting
<%# page contentType=”text/html;charset=UTF-8″ %> tag in your code if you haven't already. If the problem still persists write your code like this
<s:text name="username"/> <s:property value="getText('username')"/>
Hope this helps.
Related
I have one domain class which has one numeric field. Default scaffolding generated it. When I enter a number with more than 3 digits, ex:- 1234, then in show.gsp and list.gsp it shows as 1,234. My requirement is to show the number without comma. I can't use g:formatNumber because I have more than 50 domains and not able to use this tag for every gsp page. Is there anything I can do to change the number pattern globally?
thanks
maybe solution suggested here can be useful
Get the field directly like:
<td valign="top" class="value">${formulationTypeInstance?.id}</td>
Of course you may lose some of the tag's features with it, like highlighting field when a validation error occurs. But you're in show.gsp and it shouldn't have text fields or something to submit, so you lose nothing! Simple is the way to go here. =)
I'm struggling with a Struts2 date formatting issue. If I understand correctly, type conversion in Struts2 is locale aware, and any form fields/parameters that map to Date objects should be strings formatted in their Locale specific SHORT format; the default output for a Date object on the value stack is also output as the Locale specific SHORT format (unless overridden with custom formatting).
Although form fields have worked fine with dates, when using the <s:url> tag I can't seem to get the <s:param> tag to encode date parameters correctly. When I try something such as this
<s:url action="foo" >
<s:param name="endDateParam" value="#endDate"/>
</s:url>
the result is pretty obviously not the SHORT format:
/foo.action?endDateParam=Sat+Jan+14+00%3A00%3A00+EST+2012
I re-read the Struts2 documentation but they mostly discuss creating custom date formats in the i18n'ized properties files, which doesn't seem like the right solution.
Any help with this problem would be greatly appreciated.
You can send it like that :
<s:param name="dateFrom">
<s:date name="dateFrom" format="dd.MM.yyyy"/>
</s:param>
You've probably already solved this by formatting a string in the action the way you need it. This I would advise first, unless you are supper stickler for model/view separation or there isn't a one to one mapping between the action and the view in which case this zealousness my be justified.
Lets say you felt the formatting wasn't the business of the action in that case you could use OGNL to it's full effect:
Here is an example that displays the current date (it uses new to construct a new date but you can very easily just replace "new java.util.Date()" with "endDate". It was constructed this way so anyone can just paste it into their JSP without any action dependencies.
<p>
<s:property value="#java.text.DateFormat#getDateInstance(#java.text.DateFormat#SHORT, #java.util.Locale#CANADA).format(new java.util.Date())"/>
</p>
NOTE: requires OGNL static method access to be true. Easiest way to do that is to add the following to struts.xml:
<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
Using OGNL to this level is a bit suspicious but it is easy to read and the intention is clearly view/presentation related. Although it isn't that easy to construct... The easiest way is to write everything as one line of java and then apply ognl syntax rules which you would find here:
http://commons.apache.org/ognl/language-guide.html
Also for quick reference:
http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
http://docs.oracle.com/javase/7/docs/api/java/util/Locale.html
In my scenario I'd like to display a text with URL link within validation message attached to form element on MVC view. I'm using ValidationExtensions.ValidationMessage extension method, like this:
<%=Html.ValidationMessage(Model.Name) %>
The behavior I'm seeing is that validation message is HTML escaped, what effectively prevents me from adding a link to message. Is there a way to circumvent this behavior? My error messages aren't user-supplied, so I don't think I have to worry about output sanitization here...
I'm guessing that since Html.ValidationMessage is built in you're going to either create your own version, or if you're feeling creative, since it returns a string, assign that and then unescape the characters you want to change back.
string validation = Html.ValidationMessage(Model.Name);
validation = Regex.Replace(validation, ">", "<");
//etc...
You could use the HttpUtility.HtmlDecode(...) method along with your Html.ValidationMessage(...) method to get 'err done :D
I am using FckEditor in Create.aspx page in asp.net mvc application.
Since I need to show rich text in web pages, I used ValidateInput(false) attribute top of action method in controller class.
And I used Html.Encode(Model.Message) in Details.aspx to protect user's attack.
But, I had result what I did not want as following :
<p> Hello </p>
I wanted following result not above :
Hello
How can I show the text what user input?
Thanks in advance
The short answer is that HTMLEncode is making your markup show like that. If you don't HTMLEncode, it will do what you want.
You need to think about whether or not you need full control of markup, who is entering the markup, and if an alternative like BBCode is an option.
If your users using the editor are all sure to be 'safe' users, then XSS isn't likely to be as much a concern. However, if you are using this on a comment field, then BBCode, or something like SO itself uses is more appropriate.
You wont be able to use a WYSIWYG editor and do HTMLEncode though... (without BBCode, or some other token system)
It seems the user entered "<p> Hello </p>" (due to pressing Enter?) into the edit control, and it is displaying correct in the HTML as you have done an Html.Encode. E.g. the paragrahs are not rendered, they are outputted as "<p>..</p>" as the string is HTML encoded into something like "<p> Hello <p>".
If you do not want tags, I would suggest searching the text string for tags (things with <...>) and removing them from the inputted text. Do this before HTML.Encode.
...or am I missing something?
You can use HttpServerUtility.HtmlEncode(String)
Is it mandatory in struts2 to use struts-tags. I mean can't we use following:
<td bgcolor="DEDBC6"><div align="left"><font color="#000000"><strong>
Employee Name * :</strong></font></div></td>
<td><input name="empname" type="text" id="empname" size="32"></td>
instead of following:
<s:textfield label="Employee Salary " name="empsal" required="true"/>
I had tried both but when i used 1st i didn't get validation message i.e "empname is required" that i wrote in action-validation.xml although validation is working well.
please comment ?
No, it is not mandatory. In fact a lot of struts2 users are sufficiently dissatisfied with OGNL and choose to use regular HTML instead.
But using the standard HTML tags has the drawback of loosing some functionality, after all that's why the custom tags are there in the first place. It is possible to get validation through validate methods on the controller class even with standard HTML.
If you are just starting out with the framework I suggest you learn the struts tags properly before going off the beaten path.
Is it mandatory in struts2 to use struts-tags.
No, but if you aren't using tags at all then you're not really getting very much out of Struts as a framework. Might as well do it yourself.
i didn't get validation message
If you're using your own markup you'll have to tell it to display the error message. eg.:
<s:fielderror><s:param>empcode</s:param></s:fielderror>
please comment ?
Please stop asking the same questions over and over again.
I don't see why you need any tags at all. Couldn't you just use Struts to generate JSON result types and then use calls to those in your otherwise static jsp pages using JavaScript and JQuery?