简体   繁体   English

如何避免GSP中的常规代码?

[英]How to avoid groovy code in GSP?

I've to display check box checked or not based on some logic which is more than 4 lines of groovy code ... I don't like to write it in GSP page..is there any taglib or some way I can extract the logic out of GSP page. 我必须基于多于4行的Groovy代码的某些逻辑来显示是否选中复选框...我不喜欢在GSP页面中编写它。是否有任何taglib或某种方式可以提取逻辑超出GSP页面。 where I could access model boject and request objects. 在这里我可以访问模型对象并请求对象。

A TagLib is a good palce to put your logic, you can pass what you need as attributes and do your test: TagLib是放置逻辑的好地方,您可以将需要的内容作为属性传递并进行测试:

class MyTagLib {
  static namespace = "my"

  def somecheckbox= { attrs ->
      def model = attrs.remove('model')
      if() { //tests goes here
          //you can also test if you need to mark the checkbox as checked
          if() {
              attrs.checked = "checked"
          }
          out << g.checkbox(attrs: attrs) //remaining attrs will be applied to the checkbox
      }
  }

}

myview.gsp myview.gsp

<my:somecheckbox model="${model}" name="checkboxname" value="${checkboxValue}" />

You have (at least) 3 options: 您至少有3个选项:

Model attribute 模型属性

If you're only doing the complex logic for a single page or a single controller, do the logic in the controller method and pass the boolean result to the view through a boolean: 如果仅对单个页面或单个控制器执行复杂的逻辑,请在controller方法中执行逻辑,然后通过布尔将布尔结果传递给视图:

// in your controller
def myAction() {
    [shouldDrawCheckbox: shouldDrawCheckBox(...)]
}

private boolean shouldDrawCheckBox(/* info for decision making */) {
    // decision making
}

Service method 服务方式

If you're going to access this identical logic from several controllers, you can extract the shouldDrawCheckBox method into a service and again pass the result through the models. 如果要从多个控制器访问相同的逻辑,则可以将shouldDrawCheckBox方法提取到服务中,然后再次将结果传递给模型。

class MyController {
    def myService

    def myAction() {
        [shouldDrawCheckbox: myService.shouldDrawCheckbox(...)]
    }
}

class MyService {
    boolean shouldDrawCheckBox(...) {
        // logic!
    }
}

Custom Taglib 自定义Taglib

If you want to avoid passing the decision through the model, or if the logic is more generally applicable, you can create a custom taglib. 如果要避免通过模型传递决策,或者如果逻辑更通用,则可以创建自定义标记库。

class MyTaglib {
    static namespace = "my"

    def myCheckbox = { attrs ->
        // extract decision info from the attrs
        // perform logic with info
        if (shouldDrawCheckbox)
            out << g.checkbox(attrs: attrs)
        }
    }
}

In your view: 您认为:

<my:myCheckbox whateverYourAttribsAre="value" name="..." value="..."/>

Ideally the logic should go to the controller which renders the gsp and sets a flag in the model object. 理想情况下,逻辑应该传递给控制器​​,控制器renders gsp并在model对象中设置标志。 If the gsp is a child of a template, then the flag has to pass through. 如果gsp是模板的子级,则该标志必须通过。 DOM manipulations in view layer is not ideal when we have appropriate binding framework available in grails. 当我们在grails中有合适的绑定框架时,​​视图层中的DOM操作并不理想。

Use the g: namespace. 使用g:名称空间。 http://grails.org/doc/2.2.x/ref/Tags/checkBox.html http://grails.org/doc/2.2.x/ref/Tags/checkBox.html

<g:checkBox name="myCheckbox" value="${condition}" />

It doesn't get any simpler then this. 没有比这更简单的了。 All the logic should be done inside the controller. 所有逻辑都应在控制器内部完成。

All the data you need on a page can be passed by the controller. 您在页面上需要的所有数据都可以由控制器传递。 Just return a Map . 只需返回一个Map

class MyController {
    def index() {
        def someCondition = true
        [request:request, condition:someCondition]
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM