【问题标题】:How to execute an action without going to another view in grails?如何在不转到 grails 中的另一个视图的情况下执行操作?
【发布时间】:2013-05-14 14:44:38
【问题描述】:

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

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>

【问题讨论】:

    标签: ajax grails controller action


    【解决方案1】:

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

    class SomeController {
    
       def index() {
         // index.gsp rendered via convention
       }
    
       def test() {
          // execute your code then
          redirect action: index, params: params
       }
    }
    

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

    【讨论】:

      【解决方案2】:

      【讨论】:

        【解决方案3】:

        以下是问题的替代答案。

        默认情况下,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

        【讨论】:

          猜你喜欢
          • 2014-12-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-04
          • 2020-03-13
          相关资源
          最近更新 更多