So I have my JSTL tags like following
<a href="${urlHeader}hfv/${curRow.postTitle}">
</a>
If the curRow.postTitle is "TEST TEST" and when I click the link, the postTitle segment of the URL becomes "TEST%20TEST". What I want is "TEST_TEST" instead.
Does it have to be done before the data has been passed to the view or can you simply do it with an available JSTL or Spring tags?
Thanks.
There is a JSTL tag in "functions" called replace that you can use to do this. It works similarly to String.replace. As the example shows, you can do something like this:
${fn:replace(url, " ", "_")}
Related
I use Thymeleaf as a templating engine and I usually output variable value like this:
in Java I set:
ctx.setVariable("tester", "hello");
and in html template I output:
<span th:text="${tester}"></span>
This works great, but I would like to output a variable without the need of a tag. Something following would be great:
${tester}
Unfortunately it does not work. My goal is to avoid unnecessary tag to output the variable value. Is this possible to do with Thymeleaf?
My goal is to avoid unnecessary tag to output the variable value. Is this possible to do with Thymeleaf?
Yes this is possible. You can use the Thymeleaf synthetic th:block tag (see here).
Example template excerpt:
<body>
<th:block th:text="${tester}"></th:block>
</body>
This renders the following HTML:
<body>
hello
</body>
Only the variable is displayed.
Use Thymeleaf expression inlining (docs) using either [[...]] or [(...)]. With expression inlining, you do not need to use synthetic tags.
Example:
<body>
The value of tester is [[${tester}]].
</body>
Thymeleaf triggers on the "th:" tag and as far as I know thats the only way.
The behaviour you describe works with JSF.
Best regards
Ben
I also managed to figure out some workaround:
<span th:text="${tester}" th:remove="tag"></span>
th:remove removes span tag, but preserves content.
On my HTML-page I have a link for saving recipe, example
addrecipe?id=607691&name=Soft-Bread-Salami-Rolls
It looks like the correct URL when I hold the mouse over the link,
but the browser(?) converts it to something like this
addrecipe?id=607691%26name%3DSoft-Bread-Salami-Rolls
I managed to put atleast id= in the html but I get error when trying to add name= also.
<a th:href="#{'/addrecipe'(id=${recipesinfo.link})}" th:text=Save></a>
Looking at the docs and assuming recipesinfo.link contains your 607691 ID (and nothing else), I think you should be using
<a th:href="#{/addrecipe(id=${recipesinfo.link},name='Soft-Bread-Salami-Rolls')}">Save</a>
If the name value comes from a variable (eg recipesinfo.name), then you would use name=${recipesinfo.name} instead of the string literal.
I'm trying to add an action to a link inside a list item
It's inside a form:
<form name="searchForm" method="post" action="searchThis" id="querySubmiter">
Inside a list:
<li><img src="img/Logo/myLogo.png"/>
I'm a bit clueless as to how to make the connection.
Is it possible?
Use Struts 2 tags like
<s:a action="searchThis"><img src="<s:url value="/img/Logo/myLogo.png"/>"/></s:a>
There would be generated an anchor tag with the image. Note to use <s:url> tag to correctly define the path to the static resource. Also combined with <s:param> tag you could construct any link. See the documentation with example of the <s:a> tag.
How can I check for a particular error on a JSP page and only show it when it is present.
For example I would like to check whether the following error exists using <c:if> tag and only then render it as HTML.
<form:errors path="transactionType" cssClass="error"></form:errors>
Use the <spring:hasBindErrors name="myFormBean"> tag, and inspect the page-scope errors bean.
Reference doc link
You already get this functionality with the <form:errors> tag. It only renders its contents when there is a corresponding error (based on the path attribute), so it is analogous to using a <c:if> to first check for an error.
is there a way to use groovy builders to build JSP files in a Grails application keeping things enough integrated?
To explain better: by default Grails uses gsp files that are nice but quite verbose..
<div class="clear">
<ul id="nav">
<li><g:link controller="snippets" action="list">Snippets</g:link></li>
<li><g:link controller="users" action="list">Users</g:link></li>
<li><g:link controller="problems" action="list">Problems</g:link></li>
<li><g:link controller="messages" action="list">Messages</g:link></li>
</div>
<div id="content">
is there a way to use groovy.xml.MarkupBuilder tha would turn the previous piece into
div(class:'clear') {
ul(id:'nav') {
li { g_link(controller:'snippets', action:'list', 'Snippets') }
// and so on
Of course g_link is invented just to give the idea..
Do a search for builder under the web layer section of the grails user guide. There is an example in there that shows you exactly how to do this using the xml builder.
I don't have a complete answer for you, but I suspect the key will be gaining access to the "view resolvers". In a normal SpringMVC app, these are configured in views.properties (or views.xml) as follows:
csv=com.example.MyCSVResolver
xml=com.example.MyXMLResolver
audio=com.example.MySpeechResolver
In a regular SpringMVC app, you return something like new ModelAndView(myModel, 'csv') from a controller action.
This would cause the CSVResolver class to be invoked passing it the data in myModel. In addition to containing the data to be rendered, myModel would likely also contain some formatting options (e.g. column widths).
Spring searches the views file for a key matching the view name. If it doesn't find a match, by default it just renders a JSP with the view name and passes it the model data.
Now back to Grails....remember that Grails is really just a Groovy API over SpringMVC and most of the features of SpringMVC can be accessed from Grails. So if you can figure out how to modify the views file, just change your controller actions to return an appropriate ModelAndView instance, and it should work as described above.
GSP allows you to run arbitrary Groovy code inside <% %> brackets. So you can have something like this (borrowing example from page linked to by BlackTiger):
<% StringWriter w = new StringWriter()
def builder = new groovy.xml.MarkupBuilder(w)
builder.html{
head{
title 'Log in'
}
body{
h1 'Hello'
builder.form{ }
}
}
out << w.toString()
%>
Note that the above calls g:form tag, and you can pass additional stuff to it.
So what you are asking for is certainly possible, though I am not sure if it will end up being a win. I'd suggest you perhaps look more at TagLibs in combination with Templates and SiteMesh Layouts - can definitely simplify things tremendously.