Creating a controller / action href - grails

While Grails has several tags to generate either resource links or anchor tags, what about a plain href? Say I'm coding some AJAX functions on my own and I want to say something like
$.post("<g:href controller='mycontroller' action='update' />');

I think createLink is what you're looking for:
<a href="${createLink(controller:'mycontroller', action:'update')}" />

Related

Direct BeginUmbracoForm action to another page

I am trying to set up a global search box that posts to a search page in umbraco 7. This template uses a shared partial containing Html.BeginUmbracoForm() that points to a SurfaceController.
Since this is a global control, I want the form to be posted to the /search page instead of the current page. BeginUmbracoForm only seems to be able to post to the current page unless I'm mistaken.
I want something like RedirectToUmbracoPage(id), but clears this post values on redirect.
Is there a way to get an ActionResult like CurrentUmbracoPage which keeps the post values?
I would not try using surfacecontrollers for that purpose. Surfacecontrollers are auto routed. They are created to (e.g.) make #Html.Action(...) work on every umbraco page. Including postbacks on these actions or macros.
If you only need a "redirect" to the /search page, you don't need a surface controller. A simple partial will do the job.
<form action="/search" method="get">
Search <input type="search" name="q" />
</form>
Of course you can replace the url using some Umbraco logic like #Umbraco.TypedContentSingleAtXPath("//Search") or other Umbraco magic.
In the umbraco documentation project is an example where Html.Action is used.

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>

creating a string for a link in grails without g:link

In a tag like below:
<a data-url="/new/request/showText/${requestInstance.id}"> view text</a>
Is there a way to not hardcode the url like I have?
I tried using <g:link action="showText" id="${requestInstance.id}"> but that renders to an anchor tag.
What I'm doing might be find but I'm just curious if there is a better way to do this in grails?
You may use
${createLink(action:'myaction',params:[datasetId:dataset.id])}
for full control. It just returns something like http://myapp/myaction/123 and supportsall the params which g:link supports.
To be more specific:
<a data-url="${createLink(action:'showText',id: requestInstance.id)}"> view text</a>
should work.
You can use the createLink function inside of ${..}.
So in your case this would be:
<a data-url="${createLink(controller: 'yourController', action: 'yourAction', params:[param1: 'value1'])}"> view text</a>

Struts2 : Using form action

I am working with a struts2 based application
Inside my JSP page for form submission is it mandatory to use s:form (Predefined struts component )
Because when i tried this way it worked (calling the Action class under struts.xml )
<s:form action="HelloWorld" >
<s:submit />
</s:form>
But When I tried to use normal form submission as shown
<form action="HelloWorld">
<input type="Submit"/>
</form>
It isn't working , it gave me 404 error .
please tell me is it mandatory to use and for data submission ??
A struts form action and an HTML tag form action are different. You can use a standard HTML form tag with struts if you create a struts specific URL for example (off the top of my head):
if using in multiple places, generate the url in and call like this -
<s:url id="myActionUrl" action="HelloWorld" />
<form action="<s:property value="%{myActionUrl}" />">
<input type="Submit"/>
</form>
or using in a single instance -
<form action="<s:url id="myActionUrl" action="HelloWorld" />">
<input type="Submit"/>
</form>
You can often look at the page source in your browser to see what Struts generates and recreate it manually like this. You will often end up using additional struts tags such as property to retrieve values from your value stack, but it is useful at times, for instance when generating JavaScript code dynamically.
No, it's not mandatory to use any of the S2 tags, but as Russell says, you need to duplicate the correct action URL.
You also need to be a little careful when mixing-and-matching S2 form tags with non-S2 HTML form tags, because the default S2 theme adds additional HTML markup to the page; they don't just create form tags--the default theme uses table tags to lay out the form.
You can use s:form for form and
<input type="Submit"/>
can be replaced by
<button type="submit"/>
It's not about which to use,it is what you want from it.Struts2 tags provides additional capabilities to the form.
Please go through below two links to get the diffrence
1] http://struts.apache.org/release/2.1.x/docs/form-tags.html
2] http://www.w3schools.com/tags/tag_form.asp
some facilities such as namespace,tooltip,tooltipIconPath and many are provided by struts2 tags.

How can I show img inside anchor tag?

I want to do something like:
<img src="abc.jpg"/>
But when I see Html.ActionLink I can find any overload that allows me to do that. How can I achieve the above using Action.HtmlLink?
The easiest way is not to use Html.Action link and write plain old HTML.
<a href="<%=Url.Action("Action", "Controller")%>">
<img src="<%=Url.Content("~/Images/abc.jpg")%>" />
</a>
To achive the result your asking with ActionLink you've to write your own extension.
Look at this article

Resources