是否可以在GSP View 中输入数据并在程序内部的 Controller 中使用此数据来执行某些操作,而无需将该数据存储在域中。例如,我有一个g:textField,然后输入我的名字。我希望能够使用在 Controller 中输入的名称进行操作。

请您参考如下方法:

从 View 传递到 Controller 的任何数据都不必与任何特定域对齐。您可以通过两种方法来执行此操作。

风景:

<g:textField name="name" /> 

Controller :
class SomeController { 
  def someAction() { 
    def name = params.name 
    // do something with name 
  } 
} 

您也可以使用 Command Object

命令对象:
@Validateable 
class SomeCommand { 
  String name 
 
  static constraints = { 
     name nullable: false 
  } 
} 

Controller :
class SomeController { 
  def someAction(SomeCommand someCommand) { 
    if (!someCommand.hasErrors()) { 
      def name = someCommand.name 
      // do something with name 
    } 
  } 
} 


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!