您的位置:首页 > 大数据 > 人工智能

Grails开源框架Note

2015-09-17 17:10 411 查看

redirect(action:"hasOrder",params:[stuNo:myStudentNo,
order:unDone.id.toString()])
1、身份验证springSecurityService
case :取得用户 Id(教师 || 学生 ObjectId) def myStudentId = springSecurityService.currentUser.teacherstudentId
case :取得用户 身份权限 def roles = SpringSecurityUtils.authoritiesToRoles(springSecurityService.authentication.authorities)
验证用户身份 if(roles.contains("ROLE_CHECKER")){……}
Role 包括 ROLE_USER ROLE_CHECKER ROLE_ADMIN ROLE_TEACHER ROLE_STUDENT
ROLE_MANAGER ROLE_SUPER ROLE_SWITCH_USER ROLE_EDITOR
case :取得用户 userName def username = springSecurityService.currentUser.username

取得教师信息 userManagerService.getTeacherInstance

2、Ajax请求,返回页面

def retAsJson = [:]
retAsJson = ["ret":0,
"msg":message(code:'vippay.createOid.error')

]
render retAsJson as JSON
※ 如果是函数中间需跳转返回,后面需要加上 return

3、check出现错误后,return
flash.message = message(code: 'vippay.roles.error')
// messages_zh_CN.properties 文件中写明错误提示消息
case A :需跳转至错误画面 redirect(controller:"message",
action:"error") return
case B :需跳至某个gsp页面,gsp<body>内需写明:



4、日期格式化
导包 : import java.text.SimpleDateFormat;
定义 : defdf = new SimpleDateFormat("yyyy-MM-dd")
: defdf = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss")
EG.. x = df.format(日期格式)

x = df.parse(字符串)
计算前一天
import org.apache.commons.lang.time.DateUtils;
def date
= DateUtils.addDays( params.createDate, -1 )

5、String 与 Integer、int 的转换
1 int i
= Integer.valueOf(str).intValue();
int i = Integer.parseInt(str);
2 Integer integer=Integer.valueOf(str);
3 String s = String.valueOf(i);

String s = Integer.toString(i);

String s = "" + i;

6、A函数调用B函数,需要返回>1个返回值

B函数

def ret
= [:]

ret << [param1:"Success"]
ret << [param2:param2, flg :2]
return ret

A函数
def result
= ……
result.param1 ……

7、后台往前台传List

List<Product> list = paymentService.getVIPProductList(type)
List<Product> proList = new ArrayList<Product>()
list?.each{
proList << [pid:it.pid,
name:it.name,
price:it.price,
intro:it.intro]
}
result << [stuNo:myStudentNo, proList:proList.sort{it.pid} as JSON]
8、数字合法性验证

Pattern pattern =Pattern.compile("[0-9]*")
if(!pattern.matcher(pid).matches())

if(!params.studentNo ||
!params.studentNo.matches("[\\d]+"))
9、sort方法
opList.sort{a,b
->
b.opDate <=> a.opDate
}
opList.sort{it.opDate}
10、后台组合combobox
case A:传递固定值

def contrastList
= [];

contrastList << ['id':"eq",'text':"等于"];

contrastList << ['id':"neq",'text':"不等于"];

render contrastList as JSON



case B :



11、前后台传List

Js:
var rows = [{key1:1},{key1:2}]

var para= JSON.stringify(rows)
Controller:
def x
= params.para

def x1
= new JsonSlurper().parseText(x)
12、页面超时避免出错【身份验证】
import grails.plugins.springsecurity.Secured;
@Secured(['ROLE_TEACHER','ROLE_ADMIN','ROLE_CHECKER'])
class BOT900503Controller
{}

13、Map中Key为变量时操作
def mList
=[:]

mList << [(it.name2+it.name)
: it.score]
取值时
courseTestId.get(course)
mList[it.name2 + it.name]

转载于:/article/1900252.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: