0

我有一个启动服务器并停止服务器的代码。但是,当我按下按钮时,服务器会启动,但它也会将我重定向到新视图。我拥有的控制器中的代码是

def test= amazonWebService.ec2.startInstances(new StartInstancesRequest([InstanceToStart]))

我想在按下按钮而不进入新视图时执行测试。我在 gsp 页面中的代码是

 <g:link action="test">
        <input type="button" value="Start Server" class="button" id="startServer1" />
    </g:link>
4

3 回答 3

1

如果您不介意页面刷新,请在您的测试操作中重定向到您来自的同一视图。因此,假设您通过索引进入当前视图:

class SomeController {

   def index() {
     // index.gsp rendered via convention
   }

   def test() {
      // execute your code then
      redirect action: index, params: params
   }
}

您的另一个选择是在单击链接时提交 Ajax 请求,而根本不必刷新页面。

于 2013-05-14T15:18:55.767 回答
0

使用远程功能,请参见此处http://grails.org/doc/2.1.0/ref/Tags/remoteFunction.html

于 2013-05-14T17:32:48.980 回答
0

以下是该问题的替代答案。

默认情况下,Grails 视图解析器尝试查找类似于操作名称的视图。在您的情况下,Grails 正在尝试查找 test.gsp,如果它在您的存储库中,它会呈现它。您可以使用 Grails URL 映射功能覆盖此行为。在那里,您将指定应为某个操作呈现哪个视图。

class UrlMappings {

    static mappings = { 
        "/$controller/$action?/$id?"{ 
            constraints { 
            // apply constraints here 
            } 
        }

    "/"(view:"/index") 
    "500"(view:'/error') 

    } 
}

欲了解更多信息,请访问: http: //grails.org/doc/2.2.x/ref/Plug-ins/URL%20mappings.html

于 2013-05-14T17:52:10.923 回答