【问题标题】:Grails/Gorn flush messagesGrails/Gorn 刷新消息
【发布时间】:2010-12-19 02:33:13
【问题描述】:

我在控制器中的保存方法:

def save = {

        def userInstance = new User(params)
        boolean captcha = jcaptchaService.validateResponse("image", session.id, params.response)

        if ( (captcha) && (userInstance.save(flush: true))) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'user.label', default: 'User'), userInstance.id])}"
            redirect(url:'http://localhost:8080/TrafficManFinal/index.gsp?page=registThx')
        }
        else {
            if (captcha == false)
                flash.message2 = "Wrong Captcha!"
            render(view: "create", model: [userInstance: userInstance])
        }
    }

我的用户/create.gsp 视图

     <tr class="prop">
                                    <td valign="top" class="name">
                                        <label for="secretAnswer"><g:message code="user.secretAnswer.label" default="Secret Answer" /></label>
                                    </td>
                                    <td valign="top" class="value ${hasErrors(bean: userInstance, field: 'secretAnswer', 'errors')}">
                                        <g:textField name="secretAnswer" value="${userInstance?.secretAnswer}" />

                                        <g:form name="challenge" action="image">
                                          <jcaptcha:jpeg name="image" /><br>
                                          <br>
                                          <g:textField name="response" value="" /><br>
                                          <br>
                                        </g:form>
                                    </td>
                                </tr

>

服务:

import com.octo.captcha.service.CaptchaService;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

/**
 * Provides access to the captchas as well as provides some util
 * type methods to convert captchas to usable data.
 * 
 * @author LD <ld@ldaley.com>
 */
class JcaptchaService 
{
 /**
  * Used to access the captchas defined as part of the app config.
  */
 def grailsApplication

 /**
  * Retrieves a captcha by name.
  * 
  * @param captchaName The 'key' of the captcha defined in config.
  * @throws IllegalArgumentException If captchaName is null.
  * @throws IllegalStateException If there is no captcha by that name.
  * @returns The captcha service keyed by 'captchaName'
  */
 CaptchaService getCaptchaService(String captchaName) {

  if (captchaName == null) throw IllegalArgumentException("'captchaName' cannot be null")
  def c = grailsApplication.config.jcaptchas[captchaName]
  if (c == null) throw new IllegalStateException("There is no jcaptcha defined with name '${captchaName}'")
  c
 }

 /**
  * Used to verify the response to a challenge.
  * 
  * @param captchaName The key of the captcha
  * @param id The identifier used when retrieving the challenge (often session.id)
  * @param response What the user 'entered' to meet the challenge
  * @return True if the response meets the challenge
  * @see getCaptchaService()
  */
 boolean validateResponse(captchaName, id, response)
 {
  def c = getCaptchaService(captchaName)
                c.validateResponseForID(id, response)
 }

 /**
  * Utility routine to turn an image challenge into a JPEG stream.
  * 
  * @param challenge The image data
  * @return A raw bunch of bytes which come together to be a JPEG.
  */
 byte[] challengeAsJpeg(BufferedImage challenge)
 {
  def jpegOutputStream = new ByteArrayOutputStream()
  def jpegEncoder = JPEGCodec.createJPEGEncoder(jpegOutputStream)
  jpegEncoder.encode(challenge)
  jpegOutputStream.toByteArray()
 } 

 /**
  * Utility routine to turn a sound challenge into a WAV stream.
  * 
  * @param challenge The sound data
  * @return A raw bunch of bytes which come together to be a WAV.
  */ 
 byte[] challengeAsWav(AudioInputStream challenge)
 {
  ByteArrayOutputStream soundOutputStream = new ByteArrayOutputStream()
  AudioSystem.write(challenge, AudioFileFormat.Type.WAVE, soundOutputStream)
  soundOutputStream.flush()
  soundOutputStream.close()
  soundOutputStream.toByteArray()
 }
}

它的实现方式,只有当我引入正确的验证码一次,我才能看到来自约束的flush.messages。我想要一种方法来合并两个刷新消息或同时显示两个不同的刷新。

【问题讨论】:

  • 你想用验证码做什么,你为什么要保存它?
  • 而不是尝试保存验证码哈哈,我认为我应该在用户/保存 .gsp 文件中从脚手架中使用该服务。对吗=?我在用户/创建中创建了一个不会存储任何信息的文本字段。我使用默认的脚手架东西

标签: class grails dns service grails-orm


【解决方案1】:

现在你走在了正确的轨道上。 ;)(来自阅读 cmets)

通常控制器调用服务,服务在域模型上运行。

在这种情况下,如果您的情况很典型,则验证码用于验证是否有人输入了数据,而不是某种程序。

所以这是在控制器领域。在您的控制器操作中,您将使用 CaptchaService 来验证用户输入的值。如果它是错误的,您可能会重新呈现验证码页面。如果正确,则继续。

【讨论】:

  • 我重新制定了 que 问题。请再看一下:p,谢谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-30
  • 2012-06-25
  • 1970-01-01
相关资源
最近更新 更多