(Quick Reference)

6.2.2.5 表单和字段 - Reference Documentation

Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith

Version: null

6.2.2.5 表单和字段

Form Basics

GSP supports many different tags for working with HTML forms and fields, the most basic of which is the form tag. This is a controller/action aware version of the regular HTML form tag. The url attribute lets you specify which controller and action to map to:

<g:form name="myForm" url="[controller:'book',action:'list']">...</g:form>

In this case we create a form called myForm that submits to the BookController's list action. Beyond that all of the usual HTML attributes apply.

表单基础

GSP有很多不同的标签来支持HTML表单和字段,不过最基础的还是form标签。常规的HTML表单标签支持controller/action属性,而url属性让你以映射(map)的方式来指定controller和action:

<g:form name="myForm" url="[controller:'book',action:'list']">...</g:form>

在这个示例中,我们创建了一个myForm表单,它将会提交到BookController控制器的list操作。此外HTML的所有通用属性都可以使用。

Form Fields

In addition to easy construction of forms, GSP supports custom tags for dealing with different types of fields, including:

  • textField - For input fields of type 'text'
  • passwordField - For input fields of type 'password'
  • checkBox - For input fields of type 'checkbox'
  • radio - For input fields of type 'radio'
  • hiddenField - For input fields of type 'hidden'
  • select - For dealing with HTML select boxes

Each of these allows GSP expressions for the value:

<g:textField name="myField" value="${myValue}" />

GSP also contains extended helper versions of the above tags such as radioGroup (for creating groups of radio tags), localeSelect, currencySelect and timeZoneSelect (for selecting locales, currencies and time zones respectively).

表单字段

除了轻松地构造表单之外,GSP自定义的标签支持不同的字段类型,包括:

  • textField - 针对类型是'text'的输入字段
  • passwordField - 针对类型是'password'的输入字段
  • checkBox - 针对类型是'checkbox'的输入字段
  • radio - 针对类型是'radio'的输入字段
  • hiddenField - 针对类型是'hidden'的输入字段
  • select - 针对HTML的下拉框(select boxes)

这些标签的value属性都允许使用GSP表达式:

<g:textField name="myField" value="${myValue}" />

GSP还包含上述标签扩展的助手版本,比如radioGroup(用于创建一组radio标签)、localeSelectcurrencySelecttimeZoneSelect(用于选择区域、货币和时区)。

Multiple Submit Buttons

The age old problem of dealing with multiple submit buttons is also handled elegantly with Grails using the actionSubmit tag. It is just like a regular submit, but lets you specify an alternative action to submit to:

<g:actionSubmit value="Some update label" action="update" />

多个提交按钮

处理多个提交按钮这一个古老的问题,也得到优雅的解决,那就是使用Grails的actionSubmit标签。跟常规的提交类似,只不过你可以指定另外一个操作来提交:

<g:actionSubmit value="Some update label" action="update" />