阅读:7281回复:0
关于$P、=:、$S的使用$P:可用于获取网页地址search里面的属性,和获取核格平台页面传的值 如当前网址为http://localhost:8888/ChengDu/Main/main.w.xhtml 这时,$P.id就不存在 如当前网址为http://localhost:8888/ChengDu/Main/main.w.xhtml?id=e43d 这时,$P.id为'e43d' 如果这时要写一个数据查询,要求是如果地址没有id或则id没有值,则执行A查询;如果地址有id且有值,则执行B查询,其中B查询有个条件是数据库表的id=地址中id的值。 则应该这么写: #if($P.id or $P.id == 'null') select id,name,age from student #else select id,name,age from student where id = '$P.id' #end =:可用于获取js查询方法传过来的值。 用法和$P类似,不过在两则都可以使用的情况下,推荐使用=:,因为使用$P可以直接在后台看到查询语句中$P的值,而使用=:只能在后台看到"?",可以防止sql注入(类似java连接数据库的statement和 prepareStatement ) 如这里要使用js传值给数据查询并返回一个list集合,则可以这么写 数据查询:(SelectStu.selectName) select id,name,age from student where name =:name js代码:function getList(){ // 调用查询方法 SERVER.getList( { sqlid:"SelectStu.selectName", data:{ name:"David" }, callBack:function(ret1){ // 弹出Json的内容(仅显示Key) MSG.alertJson( ret1 ); } }); } 这样就可以把name="David"传给数据查询,然后返回查询后的ret1集合 后台sql语句显示: 上面这种用=:的方式,select id,name,age from student where name =:name 执行后在后台显示的是 select id,name,age from student where name=? 而如果使用$P的方式,select id,name,age from student where name = '$P.name' 执行后在后台显示的是 select id,name,age from student where name='David' 所以使用=:更好,类似于prepareStatement,可以防止sql注入 $S:可获取当前页面session里面的内容 例如:select id,x,x from xxx where id = '$S.id'($S.id即是当前session里面的id) servlet里面给session添加id: req.getSession().setAttribute("id", "e4393c"); 数据查询: select id,name,age from student where id = '$S.id' 这样即可查询id为session里面id的学生的信息 |
|
|