Grails- groovy sql each row to be displayed in gsp view - grails

I want to display the result of sql each row from the service code to my gsp view.
My Service code is:
def health()
{
def schemaList = [:]
groovy.sql.Sql sql = new groovy.sql.Sql(dataSource);
sql.eachRow("SELECT SOURCE, count(1) as COUNT from fact group by SOURCE");
ArrayList returnResults = []
sqlStatement.eachRow(sqlString)
{
returnResults<<it.toRowResults()
}
sqlStatement.close()
return[returnMap:returnResults]
}
My Controller Code is:
def stats = {
def health = AccessLogService.heath()
render (template:'healthview', model:[health:health])
}
My gsp view is as follows:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="layout" content="admin" />
<title>Health</title>
</head>
<body>
<SCRIPT language="JavaScript">
</SCRIPT>
<br />
<br />
<font style='font-size:14px;font-weight:bold;'>Health Condition</font>
<div id='overall'>
<g:if test="${health.size() > 0}">
<table border="1">
<thead>
<tr>
<th>Source</th>
<th>Count</th>
</tr>
</thead>
<tbody>
<g:each in="${health}" status="i" var="thisRecord">
<tr>
<td>${thisRecord.SOURCE}</td>
<td>${thisRecord.COUNT}</td>
</tr>
</g:each>
</tbody>
</table>
</g:if>
</div>
</body>
</html>
I am not able to see the results of my query in gsp view? Where I am going wrong.

you are trying to get the wrong key of your model.
you service returns a hash [returnMap:returnResults] so your controller renders the model: [health:health] -> [health:[returnMap:returnResults]].
thus in your gsp you should refer to health.returnMap to see the list:
<g:if test="${health.returnMap}">
...
<g:each in="${health.returnMap}" status="i" var="thisRecord">
<tr>
<td>${thisRecord.SOURCE}</td>
<td>${thisRecord.COUNT}</td>
</tr>
</g:each>
...
</g:if>
UPDATE:
the code looks strange... this is how it should be:
ArrayList returnResults = []
sql.eachRow("SELECT SOURCE, count(1) as COUNT from fact group by SOURCE"){
returnResults << it.toRowResults()
}

Where is the sqlStatement variable declared in your service? I think that is the error.
And an advice you need to debug your program. for example, Test if the service returns result by:
running your app debug mode
using log.debug
using println
or if you are doing these and have seen any erorrs on your console post that here.

Related

How modify value of attributes passed into Grails GSP

I require to be able to change the value of a bean (attribute)
passed into a gsp. Am using grails 2.3.11.
see code below - this does not modify the bean - please can you inform how it can be done?
At line #1 this is an attempt to replace the attribute with a new value -
so that Venue -> Xenue.
At line #2 this shows that the value has not changed.
// locns.gsp
<!DOCTYPE html>
<html >
<head>
...
<script type="text/javascript">
<g:each in="${locns}" status="i" var="locn">
var x = '${locn.addr1}'.replace("V", "X");
locn.addr1 = x; #1
</g:each>
</script>
</head>
<body>
<g:each in="${locns}" status="i" var="locn">
${locn.addr1} #2
</g:each>
</body>
</html>
// Controller:
def locns() {
Locn[] locns = new Locns[2];
locns[0] = new Locn();
...
locns[0].addr1 = "Venue Point";
locns[0].addr2 = 'London';
...
[locns:locns]
}
// attribute / bean class
class Locn {
public String addr1;
public String addr2;
...
}
Thank you, Steve.
You should be able to do something like this (even though it is dicouraged and your controller should handle this)
at #1
<g:each in="${locns}" status="i" var="locn">
<% def locnChanged = new ArrayList<>() %>
<% def x = locn %>
<% x.locn = locn.addr.replace("V", "X") %>
<% locnChanged.add(x) %>
</g:each>
at #2
<g:each in="${locnChanged}" status="i" var="locn">
${locn.addr1}
</g:each>
No need for javascript there. But once again, you shouldn't take care of that in the GSP.
You're mixing up client and server side logic here. First, the GSP tags execute on the server side to generate HTML which is sent to the browser, and only then does the generated JavaScript execute in the client browser. The GSP will produce some HTML that looks something like
<!DOCTYPE html>
<html >
<head>
...
<script type="text/javascript">
var x = 'Venue Point'.replace("V", "X");
locn.addr1 = x; #1
var x = 'Venue 2'.replace("V", "X");
locn.addr1 = x; #1
</script>
</head>
<body>
Venue Point #2
Venue 2 #2
</body>
</html>
The JavaScript will then fail with an exception at the first line #1 because you're trying to set a property on an undefined value.

How can I send an email with a layout

I am using grails mail plugin. When I submit my form, it will send an email from aaa#example.com to textField name="email successfully but how can I send an email with a layout...not blank like this picture http://www.4shared.com/photo/uT2YUCfo/Capture__2_.html or maybe some CSS..
FORM
<g:form action="send">
<table style="width:500px">
<tbody>
<tr>
<td>Your Email Address </td>
<td><g:textField style="width:250px" name = "email"/></td>
</tr>
<tr>
<td>Your Name</td>
<td><g:textField style="width:250px" name = "user"/></td>
</tr>
<tr>
<td><input type="submit"/></td>
</tr>
</tbody>
</table>
</g:form>
MAIL CLOSURE
def send = {
sendMail {
to params.email
from "aaa#yahoo.com"
subject "Test Reset Password"
body(view:"/user/layoutmail",model:[name:params.user])
}
render "Email Terkirim"
}
Well you could actually use a layout for emails, similarly how you would use layouts for view pages. What you would want to do is create a new layout and a view file for your email body content.
Layout: eg. ../views/layouts/emailLayout.gsp
<%# page contentType="text/html" %>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
a {color: #348eda;}
</style>
</head>
<body>
<g:layoutBody/>
</body>
</html>
View eg. ../views/emails/welcomeEmail.gsp
<%# page contentType="text/html" %>
<g:applyLayout name="emailLayout">
<html>
<body
Your Welcome ${welcome.username}
</body>
</html>
</g:applyLayout>
And to send the mail heres an example
def sendWelcomeMail(User user, String url){
def rtn = [success:false]
if(user) {
def fooBar = [
username: user.username,
email: user.email,
url: url
]
sendMail {
async true
to fooBar.email.trim()
subject "Welcome Email"
body(view: '/emails/welcomeEmail', model: [welcome: fooBar])
}
rtn.success = true
}
return rtn
}
It isn't going to pick up a grails layout. And you don't really want it to. You should construct your email in a fashion that it could be a stand alone web page with no other dependencies. All the static resources used should be accessible via a public URL.

Grails params.max returns a map

I am new to grails and have just started developing applications in work. The first thing that i wanted to do is create a gsp with two tables, each table having pagination functionality. With a research I found that there is a plugin called remotePagination that uses ajax to update a tables pagination. The problem that i am having is that the 'params.max' and 'params.offset' value is a map of two strings rather than just a string value. On opening the page the 'list' closure is called and with the correct values set for the max and offset, lets say 10. On the second call, when the ajax closure is called the max and offset values are each held within a map as follows:
params.max = [10,10]
params.offset = [10,10]
The code I am using is as follows:
Controller:
def list = {
params.max = Math.min(params.int('max') ?: 10, 100)
[bookInstanceList: Book.list(params), bookInstanceTotal: Book.count()]
}
def ajaxListBooks = {
params.max = Math.min(params.int('max') ?: 10, 100)
render(template: "bookList", model:[bookInstanceList: Book.list(params), bookInstanceTotal: Book.count()])
}
list.gsp
<%# page import="com.intelligrape.Book" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="layout" content="main"/>
<g:set var="entityName" value="${message(code: 'book.label', default: 'Book')}"/>
<title><g:message code="default.list.label" args="[entityName]"/></title>
<g:javascript library="prototype"/>
</head>
<body>
<div class="nav">
<span class="menuButton"><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a>
</span>
<span class="menuButton"><g:link class="create" action="create"><g:message code="default.new.label" args="[entityName]"/></g:link></span>
</div>
<div class="body">
<h1><g:message code="default.list.label" args="[entityName]"/></h1>
<g:if test="${flash.message}">
<div class="message">${flash.message}</div>
</g:if>
<div id="repoList">
<g:render template="bookList"/>
</div>
</div>
</body>
</html>
_listBooks.gsp
<%# page import="com.nmi.uk.sw.subzero.Book" %>
<div>
<table>
<thead>
<tr>
<util:remoteSortableColumn property="author" title="${message(code: 'book.author.label', default: 'Author')}" update="repoList" action="ajaxListBooks"/>
<util:remoteSortableColumn property="name" title="${message(code: 'book.name.label', default: 'Name')}" update="repoList" action="ajaxListBooks"/>
<util:remoteSortableColumn property="price" title="${message(code: 'book.price.label', default: 'Price')}" update="repoList" action="ajaxListBooks"/>
</tr>
</thead>
<tbody>
<g:each in="${bookInstanceList}" status="i" var="bookInstance">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
<td><g:link action="show" id="${bookInstance.id}">${fieldValue(bean: bookInstance, field: "author")}</g:link></td>
<td>${fieldValue(bean: bookInstance, field: "name")}</td>
<td>${fieldValue(bean: bookInstance, field: "price")}</td>
</tr>
</g:each>
</tbody>
</table>
<div class="paginateButtons">
<util:remotePaginate total="${bookInstanceTotal}" update="repoList"
action="ajaxListBooks"
pageSizes="[10,20,30,40,50,60,70,80,90,100]" />
</div>
</div>
The above code is based on the sample application for the remotePagination tutorial. It isn't that different. I created it just to see if the plugin would work before I integrated it into my application.
I would like to know if any one else has come across this problem and if there is a solution to it. Many thanks.
In your _bookList.gsp, there is an error in <util:remotePaginate tag:
pageSizes="[10,20,30,40,50,60,70,80,90,100]"
pageSizes should be a map, not a list, like:
pageSizes="[10:'10 Per Page', 20: '20 Per Page', 50:'50 Per Page',100:'100 Per Page']"

user-defined form validation in grails

I am learning grails by doing some sample programs.
I want to know how to validate a user-defined form using grails.
My code is:
Domain class
class User
{
String property
static constraints =
{
property(blank:false,nullable:false)
}
}
Controller class
def addUser() {
}
def userPrint() {
def values = request.getParameterValues("property")
for(val in values){
println "values received :"+val
}
}
addUser.gsp
<html>
<head>
<title>Users</title>
</head>
<body>
<g:form name="useraddform" url="[controller:'user',action:'userPrint']">
<table>
<tr>
<td>Username :</td>
<td><g:textField name="property" value=""/></td>
</tr>
<tr>
<td>Password : </td>
<td><g:textField name="property" value=""/></td>
</tr>
</table>
<input type="submit" value="ADD"/>
</g:form>
</body>
</html>
I need to validate the username and password entered.
How to achieve this???
Please help....!
Please read the constraints manual section and whole web layer chapter.
Also, run grails generate-all on some other sample project and look at scaffolding GSPs contents, especially hasErrors tag/function.

Render Errors From A Service

I call a service that creates a parent and a child record. If an error happens, the service throws a RuntimeException. The RuntimeExceptionis is caught by the controller and then there there is a redirect back to the gsp. But the error is not being rendered.
In this case, I guess the controller and thus the gsp doesn't really no anything about the objects, since everything is done in the service. So how do I render the errors?
Simple Data Entry GSP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sample title</title>
</head>
<body>
<h1>Add A Record</h1>
<g:hasErrors bean="${parent}">
<div class="errors">
<g:renderErrors bean="${parent}" as="list" />
</div>
</g:hasErrors>
<g:hasErrors bean="${child}">
<div class="errors">
<g:renderErrors bean="${child}" as="list" />
</div>
</g:hasErrors>
<g:form action="add" name="doAdd">
<table>
<tr>
<td>
Parent Name
</td>
<td>
Child Name
</td>
</tr>
<tr>
<td>
<g:textField name="parentName" />
</td>
<td>
<g:textField name="childName" />
</td>
</tr>
<tr><td><g:submitButton name="update" value="Update" /></td></tr>
</table>
</g:form>
</body>
</html>
Controller
class AddrecordController {
def addRecordsService
def index = {
redirect action:"show", params:params
}
def add = {
println "do add"
try {
addRecordsService.addAll(params)
} catch (java.lang.RuntimeException re){
println re.message
flash.message = re.message
}
redirect action:"show", params:params
}
def show = {}
}
Service
class AddRecordsService {
static transactional = true
def addAll(params) {
def Parent theParent = addParent(params.parentName)
def Child theChild = addChild(params.childName,theParent)
}
def addParent(pName) {
def theParent = new Parent(name:pName)
if(!theParent.save()){
throw new RuntimeException('unable to save parent')
}
return theParent
}
def addChild(cName,Parent theParent) {
def theChild = new Child(name:cName,parent:theParent)
if(!theChild.save()){
throw new RuntimeException('unable to save child')
}
return theChild
}
}
You need to get a reference to the invalid object somehow and pass it to the view via the model so I would extend RuntimeException and add fields to contain the objects with validation errors e.g.
}catch(MyCustomException m){
render view:'show', model:[parent:m.getParent(), child:m.getChild()]
}
This whole exercise might be easier using Parent.withTransaction instead of the automatic rollback via RuntimeExceptions. Then you could manually rollback the transaction if there's validation errors and just return the objects instead of having to contain them in the exception.

Resources