ColdBox - 如何使用setView()调用设置视图和传递参数?

Kin*_* G. 3 coldfusion coldbox

我正在使用ColdBoxColdFusion 10.我想用setView()传递一个参数说id = 1000 .我找不到任何传递param的例子.

这是代码:

component {
// Dependency Injection
property name="requestService" inject="RequestService";

function index(event, rc, prc) {        
    var response = requestService.save(rc);

    if(response.Success EQ true) {
        event.setView(view="requests/success"); //Want to pass a param(int)
    } else {
        event.setView("requests/failure");
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Bra*_*ood 5

将值从处理程序传递到视图有两种主要方法.

第一种是将值放在私有请求集合中,该集合在处理程序中作为名为"prc"的结构提供.该视图具有相同的"prc"结构.此请求集合可用于整个请求以及为该请求执行的所有布局或视图.

在你的处理程序中

prc.id = 1000;
event.setView( view="requests/success" );
Run Code Online (Sandbox Code Playgroud)

在你看来

<cfoutput>#prc.id#</cfoutput>
Run Code Online (Sandbox Code Playgroud)

如果你想要一个更加封装的方法,只使该值专门用于该视图,你可以使用event.setView()的"args"参数,并传递一个结构的值,该结构将在结构的视图中可用被称为"args".

在你的处理程序中

event.setView( view="requests/success", args={ id = 1000 } );
Run Code Online (Sandbox Code Playgroud)

在你看来

<cfoutput>#args.id#</cfoutput>
Run Code Online (Sandbox Code Playgroud)