how put a html args in a message of grails? - html

I wish to insert into message a line like "view.mail.warning". It contains a html code, but using encodeAs="html", the message shows a href ... too and does not show a link. What is the correct way for do these things?. I don't want to insert the message in parts :(.
In a gsp:
<"p style='color:#ddd; font-size:12px; text-align:center;'> <"g:message code="view.mail.warning" encodeAs="html>
In message.properties:
<"view.mail.warning= textPart1 <"a href=['http://page.com']> <"g:message code="view.pageName"/> textPart2 <"a href='mailto:support#page.com'">'support#page.com' textPart3

The correct way /is/ to insert it in parts. However, if you /must/ include html in your properties file you can. What you can not include there however, is GSP/taglibs. Your call to g:message will never be parsed/executed. Messages from the resources are just strings. The following are perfectly valid:
messages.proprties
view.mail.warning=<p>I {0} do html in here!</p><em>But I {1} shouldn't.</em>
GSP
<g:message code="view.mail.warning" args="['can', 'really']" />
Output
<p>I can do html in here!</p><em>But I really shouldn't.</em>
A few things to note about this. As you can see you can pass arguments to your message codes. They are ordinal, and start a zero. This way you can tokenize your message and even pass values from your GSP to your messages.
This is the correct way to use message resource bundles in Grails.

For example:
message.properties
view.mail.warning= >p>I can do html in here!>/p> >p>But I really shouldn't.>/p>
GSP
Output
I can do html in here!>/p> >p>But I really shouldn't.
i want insert into message properties, message with strings+html

Related

Thymeleaf - Insert HTML into <p>

I have the following Java variables:
context.setVariable("content", "test <b>test</b>");
context.setVariable("condition", condition);
and I want to place this into the following template:
<p th:text="${condition}?${content}:''"></p>
The expected result is: "test test" if the condition is true. However,
the result is
"test <b>test</b>"
if I open my page and the condition is true. Changing th:text=... to th:utext=... results in
"test <b>test</b>"
displayed on my page, which is still not what I want.
Is there a way to do this?
If you want the actual HTML, then you need to pass actual (not escaped) HTML and use th:utext. (It doesn't make sense to pass in escaped HTML and think Thymeleaf will unescape it for you.)
// Controller
context.setVariable("content", "test <br>test</br>");
<!-- HTML -->
<p th:utext="${condition}?${content}:''"></p>
If you really do want to pass in "test <br>test</br>", then you're going to have to use string search/replace to change those characters back into < and > (probably using the #strings utility object).

How do I check the Html.ValidationSummary for errors?

I am writing an mvc web app and I'm trying to check to see if there are any errors in the Html.ValidationSummary to determine if I need to display the div that handles the messages.
#if(Html.ValidationSummary() != null)
{
<div class="registration-val-summary">
<label class="registration-val-label">
Please correct the following issues with your registration:
</label>
#Html.ValidationSummary()
</div>
}
This is the error that I receive:
Unexpected "if" keyword after "#" character. Once inside code, you do
not need to prefix constructs like "if" with "#".
From what I read in this post: Unexpected "foreach" keyword after "#" character It is due to the "Html" that I have inside my code. I understand the error but I do not know how to get around it. I want to avoid displaying the div that wraps the ValidationSummary unless there are actually errors to display otherwise the message about correcting your error shows up and displaces my form.
How do I hide this if ValidationSummary has no messages to display?
You are recieving that error because you are rendering the partial view rasor code from Html.ValidationSummary() into your if statement. The compiler then sees any # symbols the validationSummary renders and fails.
A simple check to see if there are validation errors is to do #if(!ViewData.ModelState.IsValid)
#if(!ViewData.ModelState.IsValid)
{
<div class="registration-val-summary">
<label class="registration-val-label">
Please correct the following issues with your registration:
</label>
#Html.ValidationSummary()
</div>
}
Html.ValidationSummary() is displayed IF and ONLY IF there validation errors on the page.
What you are trying to do is already achieved by the Html.ValidationSummary() helper and it returns an HTML div element that contains an unordered list of all validation error messages from the model-state dictionary [Source]
Html.ValidationSummary() renders a partial view, but you can customize/extend the view by following this answer
In your case, you might want to put the following code inside the customized view that you will create
<div class="registration-val-summary">
<label class="registration-val-label">
Please correct the following issues with your registration:
</label>
</div>

Explain the strange code html

I want to do a change in my site web (html+css+php)
I have this code html:
<h4>#_live_support#</h4>
In your opinion, what is the result of code??
No!No! result is not:
#_live_support#
a result CODE is Name Only:
Direct support
I want to explain this code please**
Photo caption
[code html]
http://goo.gl/vZQNXD
[Result]
http://goo.gl/4OaIvB
This is a template HTML file you are looking at. When the PHP script is called, #_LIVE_SUPPORT# will be dynamically replaced by whatever is appropriate at the time.

set var value from input field value

I started short time ago with JSP, JSTL, HTML and JavaScript so here is my problem:
I need to set the value of a var the value of an input hidden. Other option is if it possible to compare using
<c:if test="....">
the value of a variable that I sent with the request with the value of the hidden input.
Thanks.
Update
I've been trying but can't make it work.
I have this field that contains the id of and object. I also have the list with the objects so what I have to do is find the object related to that ID.
<input type="text" name="id1" />
but if I do this:
<c:set var="dd" value="${param.id1}" />
<input type="text" value="${dd}" />
The input text is empty but the text related to id1 displays 850 (i.e. the value is dinamic)
Any suggestion why is not working?
Update 2
I need the "multipart/form-data" because in the form I need to upload a picture. I understand how to get the parameters from Java, but since I'm not using the server but the JSP pages, there's any way to do it? Just need to read that input element and save it in a variable.
You can access request parameters by implicit ${param} variable.
E.g. http://example.com/context/page.jsp?foo=bar in combination with
<c:if test="${param.foo == 'bar'}">
The foo's param value is bar!
</c:if>
<c:if test="${param.foo != 'bar'}">
The foo's param value is not bar, it is: ${param.foo}
</c:if>
would show the first condition.
If you actually want to retain some hidden input element in subsequent requests (which wasn't really made clear in your question), then all you basically need to do is:
<input type="hidden" name="foo" value="${param.foo}">
Update: as per your update: you need to give the input element a name as well. Thus, e.g.
<input type="text" name="id1" value="${param.id1}" />
This way it's available by request.getParameter("id1") and inherently also ${param.id1}. Do you see it now?
Update 2: as per your comment here: certainly this is related to enctype="multipart/form-data". With this encoding, the request parameters aren't in the parameter map anymore, but instead in the request body, because of the mixup with binary data (file uploads). It's going to be a long story to explain it all, but basically you need to parse the request yourself. If you're on Servlet 2.5 or older, then the Apache Commons FileUpload is very helpful here. Read especially "User Guide" and "Frequently Asked Questions" over there to see code examples and to learn how to use it the right way (also in MSIE!). You can even decide to abstract the FileUpload away so that you can stick using HttpServletRequest#getParameter() and ${param} the usual way, also see this article.
If you're already on Servlet 3.0, then you can just make use of HttpServletRequest#getParts(). You can even abstract it away so that you can stick using HttpServletRequest#getParameter() and ${param} the usual way, also see this article.
Update 3: Oh, you really don't want to use JSP to do all the processing. There it is not for. It's high time to learn Servlet. Besides, when using a Filter which puts all parameters from the request body back in the request parameter map (as described in the both articles), you also don't necessarily need a Servlet after all.

HTML Form works with GET but not with POST

I'm running Firefox 2.0.0.14.
I have a form on a webpage which is working fine with the GET method.
I'm using a plugin to view my browser's HTTP request when submitting the form, and here it is :
GET /postComment.php?review=2&comment=Testing HTTP/1.1
...
However, if I make the simple change from method=GET to method=POST on the form:
GET /postComment.php HTTP/1.1
...
It isn't even attempting to POST.
Any possible reasons for this, under any circumstances?
EDIT: Here is the form:
<form method=POST action="postComment.php"><input type=hidden name=review value="2"><input type=submit value="Postit">
</form>
Is the action parameter of the form tag set? Could Javascript be intercepting the post? Some HTML from your form would be helpful, or an example link :)
I'm guessing your plugin is not capturing the POST variables. Since the output of your plugin is:
GET /postComment.php HTTP/1.1
How are you catpuring your POST varables? $_POST['key'] or $_REQUEST['key'] should contain your value if the form action and method are set properly.
POST will not be found in the query string.
EDIT:
if you are trying to capture the post value, you can check it with something like this:
if (isset($_REQUEST['submit'])) {
echo $_REQUEST['review'];
}
or
if (isset($_POST['submit'])) {
echo $_POST['review'];
}
Acorn
I would start by making sure your HTML is valid XHTML. Wrap attribute values in quotations and end the input elements with />. Use a valid DOCTYPE.
Also, try changing the value of the submit button to "submit" (as that is the default).
Try it out in different browsers, including the latest version of Firefox.
Firstly, your <form> tag needs to be adjusted:
<form method="post" ... >
Secondly, I have a function called debugArray that I use to spit out misbehaving arrays. It's very handy:
function debugArray($array){
echo("<pre>");
print_r($array);
echo("</pre>");
}
Then, call it in your code like this:
debugArray($_POST);
By looking at the entire contents of the $_POST array, you can see exactly what is being sent, what is not, and how it is being sent.
I'm willing to wager that one of the following is true:
You have a spelling mistake in a field name, remembering that names are case sensetive.
Your form field is outside your <form> tags.
You have a value that is not being escaped correctly, or otherwise being dropped from the $_POST for whatever reason.
Edit: And I would also be inclined to update your copy of Firefox.
I was having the same problem, till I remembered that my .htaccess file hides my PHP extension, and for reasons that someone else can explain (tech stuff) All I did was remove the .php extensión in the action property and it worked.
So, I went from:
action="folder/folder/file.php"
To:
action="folder/folder/file"
And the print_r($_POST) displayed the full array
I really Hope this helps someone else with the same problem.
And if anyone can technically explain why this is happening, it would be very educational 🙃