本文介绍了在Grails中动态显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个问题.我一直在根据朋友的建议使用Grails进行项目.我仍然是使用Grails的新手,因此欢迎您进行任何脚踏实地的解释.

I have this problem I'm facing. I have been working on a project using Grails based on the advice from a friend. I'm still a novice in using Grails, so any down to earth explanation would be highly welcomed.

我的项目是一个Web应用程序,可以扫描断开或断开的链接并将其显示在屏幕上.主应用程序是用Java编写的,随着扫描的进行,它会在系统控制台上连续显示输出(正常链接,不良链接,扫描的页面).我已经使用Grails实现了UI,控制器,视图和数据库.现在,我想在我的GSP页面的一部分中积极显示forager.gsp当前正在扫描的链接,当前发现的不良链接的数量以及正在扫描的当前页面.

My project is a web application which scans broken or dead links and displays them on a screen. The main application is written in Java, and it displays the output (good links, bad links, pages scanned) continuously on the system console as the scan goes on. I've finished implementing my UI, controllers, views, database using Grails. Now, I will like to display actively in a section of my GSP page say forager.gsp the current link being scanned, the current number of bad links found and the current page being scanned.

我尝试实现此活动显示的尝试包括将应用程序在控制台上显示的输出存储在数据库中的表中.该表具有一行,该行随着当前分页的扫描更改,发现的良好链接的数量更改和发现的不良链接的数量而不断更新.由于此特定表正在不断更新,因此我在控制器中编写了一个动作,该动作读取了这一行并将结果呈现到我的UI中.我现在面临的问题是,我需要一种在一段时间后在UI中不断更新显示结果的方法.我希望最终输出看起来像

The attempts I have tried in implementing this active display include storing the output my application displays on the console in a table in my database. This table has a single row which is constantly updated as the current paged scanned changes, number of good links found changes and number of bad links found changes. As this particular table is being updated constantly, I've written an action in my controller which reads this single line and renders the result to my UI. The problem I'm now facing is that I need a way of constantly updating the result being displayed after an interval of time in my UI. I want the final output to look like

scanning: This page,  Bad links: 8, good links: 200

所以基本上这是我的控制器操作,该操作从数据库中读取表

So basically here is my controller action which reads the table from the database

import groovy.sql.Sql

class PHPController {

   def index() {}

   def dataSource

   def ajax = {

      def sql = new Sql(dataSource)

      def errors = sql.rows("SELECT *from links")

      render (view: 'index', template:'test', model:[errors:errors])
   }
}

这是我渲染test.gsp的模板

Here is the template I render test.gsp

<table border="0">
<g:each in="${ errors }" var="error">
   <tr><td>${ error.address }</td><td>${ error.error}</td><td>${ error.pageLink}</td></tr>
</g:each>
</table>

目前,我正在使用一个测试UI,这意味着它不是我的UI,而是我用于测试的UI,例如index.gsp

For now I'm working with a test UI, which means this is not my UI but one I use for testing purposes, say index.gsp

<html>
<body>

<div><p>Pleaseeee, update only the ones below</p></div>

<script type="text/javascript">
   function ClickMe(){
      setInterval('document.getElementById("auto").click()',5000);
      alert("Function works");
   }
</script>

<div id="dont't touch">
   <g:formRemote url="[controller:'PHP', action:'ajax']"  update="ajaxDiv"
                 asynchronous="true" name="Form" onComplete="ClickMe()" after="ClickMe()">
   <div>
      <input id="auto" type="button" value="Click"  />
   </div>
</g:formRemote>

<div id="ajaxDiv">
   <g:render template="/PHP/test"/>
</div>

</body>
</html>

我要更新的div是"ajaxDiv".任何试图回答这个问题的人都可以假设我没有index.gsp,可以从头开始提出解决方案.到目前为止,这是我一生中第一次使用Grails,也是我第一次使用任何形式的Ajax.目的是从我的数据库中动态获取数据并显示结果.或者,如果有人知道如何将系统控制台的输出直接镜像到UI,那也很棒.

The div I'm trying to update is "ajaxDiv". Anyone trying to answer this question can just assume that I dont have an index.gsp and can propose a solution from scratch. This is the first time I'm using Grails in my life so far, and also the first time I'm ever dealing with ajax in any form. The aim is to dynamically fetch data from my database and display the result. Or if someone knows how to directly mirror output from the system console unto the UI, that will also be great.

推荐答案

听起来形式很适合您的需求.在表单上查看 Grails文档.您应该能够使用所需的值来呈现表单,而不会带来太多麻烦.请务必注意您的映射,并在设置index.gsp以便为您的值呈现表单之后遇到任何问题.

It sounds like a form would be appropriate for your needs. Check out the Grails documentation on forms. You should be able to render a form with the values you would like without too much trouble. Be sure to pay attention to your mapping and let me know if you have any questions after you have set index.gsp up to render a form for your values.

这篇关于在Grails中动态显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:05
查看更多