Custom fields displayed by Grails Fields plugin with <f:display> - grails

I want to display some fields using the <f:display> tag of the Grails Fields plugin. I tried the following:
<f:display bean="user" property="authorities" label="roles"/>
<f:display bean="user" property="username"/>
However, this only displays the field values, not the labels. I tried adding a custom template grails-app/views/_fields/default/_displayWrapper.gsp
<span id="${property}-label" class="property-label">${label}</span>
<div class="property-value" aria-labelledby="${property}-label">${value}</div>
This custom template is used with
<f:table collection="${userList}" except="password"/>
But it is ignored when I use <f:display>

In your _displayWrapper you should use ${widget} instead of ${value} as you want the rendering of _displayWidget to get the specific types of rendering.
The <f:table> is IMO a bit broken, but I'll look into that as soon as I have time. I'm doing a presentation on the Fields plugin at Greach, so I need to do something :-)

Related

Select dropdown with f:field (fields plugin) in Grails 3

I'm trying to generate a select drop down with an option preselected (based on params) using the fields plugin. The code below generates a select box with the proper elements and everything works except it doesn't add "selected" to the option tag. It seems to be ignoring my widget- attributes.
From what I understand the fields plugin will pick g:select in this case by default if not overridden, and you can use widget- to pass arguments to it.
What am I doing wrong?
<f:field bean="specialUser" property="user" label="user.label" widget-optionKey="id" widget-value="${params.user?.id?:value?.id}"/>
Link to the fields documentation:http://grails3-plugins.github.io/fields/snapshot/guide/single.html#
This seems to have done what I wanted, computed specialUserList from the controller. Though it would've been nice to utilize the "widget-" to do this form me.
<f:field bean="specialUser" property="user" label="user.label">
<g:select id="user" name="user.id" optionKey="id" class="span-8 margin-bottom-none" from="${specialUserList}" noSelection="${['null': message(code: 'common.list.select')]}" value="${params.user?.id}"/>
</f:field>

Use placeholders with <f:all bean />

In Grails, you can generate a form using <f:all bean="beanName" />. And while the generated form looks great, I was wondering if it would be possible to have a placeholder in the field, so that the result becomes something like:
<input type="text" name="question" placeholder="type your question here" />
I tried using the attributes validation in Grails, like this:
class Question {
static constraints = {
question(size:5..100, attributes:[placeholder:"type your question here"])
}
}
But it doesn't seem to have any effect on the generated HTML.
Just in case there's any confusion, the f:all tag is provided by the fields plugin. I don't think there's any way you can specify the placeholder attribute via the domain class constraints, but there are a few other options.
One option is to define a custom (GSP) template for this property and specify the placeholder attribute therein. The path to this template will depend on which version of the plugin you're using, but you can find the details here.
Alternatively, if you render each field individually with f:field, rather than using f:all you can pass additional attributes to the input field, e.g.
<f:field bean="person" property="gender"
widget-placeholder="type your question here"/>
In earlier versions of the plugin (before 1.5), the attribute should be named input-placeholder instead, e.g.
<f:field bean="person" property="gender"
input-placeholder="type your question here"/>

How do I conditionally parse formatting with Thymeleaf?

Is there an alternate way I can get this to process with Thymeleaf? Thymeleaf doesn't like the open tag, but I want to only render it for ROLE_A.
<span sec:authorize="hasRole('ROLE_A')">
<div class="col-xs-10">
</span>
bunch of text not specific to ROLE_A
<span sec:authorize="hasRole('ROLE_A')">
custom text specific to ROLE_A
</div>
</span>
I tried using
<sec:authorize="hasRole('ROLE_A')">
and
<div sec:authorize="hasRole('ROLE_A')">.
The former also doesn't run due to the same open tag issue and the latter is mixing up the closed div tags.
I have numerous blocks like this, so duplicating sections for different roles is not a great solution.
Not in the style you want. You need to close those elements.
My advice is then consider using fragments and load them as required with the needed data/
That way you reuse existing code thus making it cleaner and smaller.
For more examples and way you can do the above check out the Thymeleaf page http://www.thymeleaf.org/doc/springsecurity.html.

Create custom GSP tag without writing Groovy code

Is it possible to create a custom GSP tag without writing Groovy code and embedding my HTML in the code (i.e. more of a JSP style way of creating a custom tag)?
I have a menu that consists of a bunch of items something like:
<li class="menu-item">
<g:link controller="someController" action="someAction" id="123">
My Item Text
</g:link>
</li>
I would like to create a new GSP tag to simplify my pages since it will be repeated multiple times. So, I'd like to create something like:
<my:menuitem controller="someController" action="someAction" id="123" text="My Item Text"/>
I know that I can create a custom taglib and create the tag using Groovy code. However I really don't like the idea of embedding HTML into a Groovy file. In the past I have created JSP taglibs in essentially a JSP file without writing Java code. So far looking at the documentation for Grails I haven't seen a similar style.
As a side note, can custom JSP tags be used within GSP?
You can do this with a template via the render tag, as explained in the "Views and Templates" section of the docs. Its worth noting you name the template file with a leading underscore but you refer to it in the render tag without the underscore.
The other alternative is to use a custom taglib as you described but to create your HTML with the Groovy MarkupBuilder. It takes a bit of getting used to (syntax is a bit strange) but once you've done a few times it becomes second nature.
The only way I can see to do what you want to do without a Taglib is to use g:render and pass in your values into the model attribute. Like this:
<g:render template="myTemplate" model="[controller: 'someController', action: 'someAction', id: 123, text: 'My Text Item']" />
Then in your actual template you will have the following:
<li class="menu-item">
<g:link controller="${controller}" action="${action}" id="${id}">
${text}
</g:link>
</li>

Rendering html using Struts2

Using Struts2, my goal is to present a simple blog to a user using Struts2 iterators, such as:
Most Recent Topic
response 1
response 2
...
Previous Topic
response 1
response 2
...
Users generate and submit each Topic/Response using a separate form, but, once submitted, I don't want them to edit the blog.
To generate either a Topic or a Response, I provide an editor (like the stackoverflow editor I'm using now) that produces html-formatted text, including whatever styling (bold, underlines, lists, etc.) that the user chooses. The text of the Topic/Response created by the user, including the html tags, is stored in a database.
However, I cannot find a way to render the Topic/Response as html in the blog. For example, text bolded in the editor shows up as <strong>text</strong> in a struts2 s:textarea tag.
I know that the s:property tag has an 'escapeHtml' attribute that will prevent this, but the s:property tag can't layout the text properly, and it seems that only the s:property tag has this attribute.
I've tried using <input value="%{#topic.content}" /> within the iterator instead of s:textarea, but it doesn't seem to recognize the #topic iteration reference.
Is there a way to do this?
use text instated of tax area .Let me know if you still facing this issue.
Use escapeHtml="false". I just tried it myself and it works as intended.
For example, with:
<s:set var="var1"><p>some stuff</p><p>other stuff</p></s:set>
<s:property value="var1" escapeHtml="false" />
renders the paragraph tags as you would expect.
How about using <pre> with <s:property>.
About html <pre> tag:
http://www.w3schools.com/tags/tag_pre.asp

Resources