Several important methods of grails filter and data query

Source: Internet
Author: User
Tags grails

A simple way to query

Use of 1.findAllWhere

In Grails, the difference between find and FindAll is that the find defaults out the first record of the query, and FindAll is the one that queries all the records.

DefUsefindallwhere() {  Defcity=cityinfo. Findallwhere (code:ID: 1)  iterator<cityinfo> it=city.iterator ();  def buf= new stringbuffer ();  while (It.hasnext ()) { cityinfo C=it.next ();  Buf.append (C.getid () +} render buf.tostring ();         
If there are multiple conditions behind the where, the and

Use of 2.getAll

Def usegetall () {  def c=cityinfo. Getall ([1,2,3])  iterator<cityinfo> it=c.iterator (); Span class= "indent" > def buf= new stringbuffer ();  while (It.hasnext ()) { cityinfo C1=it.next ();  Buf.append (C1.getid () +} render buf.tostring ();         
Get is a single data query based on ID, GetAll is querying multiple data

Use of 3.useFindAllBy

This method is particularly powerful, by following these conditions can be added

  • LessThan
  • LessThanEquals
  • GreaterThan
  • GreaterThanEquals
  • Between
  • Like
  • Ilike(i.e. ignorecase like)
  • IsNotNull
  • IsNull
  • Not
  • Equal
  • NotEqual
  • And
  • Or
The code is as follows
DefUsefindallby(){  def c=cityinfo. Findallbyidbetween (5,35);  span class= "constant" >iterator<cityinfo> it=c.iterator ();  def buf=new stringbuffer ();  while (It.hasnext ()) {  Cityinfo C1=it. next ();  Buf.append (C1.getid () +} render buf.tostring ();         
The results are as follows:

4. Use the HQL when using Find or FindAll

DefUsefindall(){  def hql= "from cityinfo where Id>:id and code in:code  def c=cityinfo.findall (hql,["Id": 2,code:["nb< Span class= "string", "nj"])  iterator< Cityinfo> It=c.iterator ();  def buf=new StringBuffer ();  while (It.hasnext ()) { Cityinfo C1=it.next (); " +c1.getname () + "\n}  render buf.tostring ();}           
There is no way to list all the methods that are not written to view the API documentation

Use of two filters

Previous Web development filters are typically used to restrict access to certain files or folders, and Grails filters are typically used to restrict the way that certain controllers or controllers cannot be accessed directly. Grails can control access to one or all of the controllers

For this piece of knowledge can look at the document, the document does not seem to say very carefully. Below I wrote a filter to restrict access to all controllers, as long as the session is empty and access is not the specified method to jump to the login interface, one thing to note is that the filter to filters end, generally speaking, the filter should be placed under grails-app/conf

Package FilterClassloginfilters {  DefFilters = { AllController‘*‘,Action"*") {  Before = {    if (session.user| | actionname=="Logincheck" | | actionname=="Login") {     ReturnTrue   }else{    redirectUri:"/login/login")     return false;   }  }   after = { Map model->  }   Afterview = {exception e->  } }}}    
The following code is posted to the login controller:
Logincontroller {  logincheck() { session.user="Cry""impersonated login" }Login (){ }}
As for the login page is relatively simple, I wrote a sentence, the following to access just one of the action, such as Other/usefindallwhere, you can see the following interface

The next way to access the login

The controller before the visit will not jump to the login interface.

  likes a Collection

Several important methods of grails filter and data query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.