使用jcaptcha4struts2插件生成的验证码问题

环境说明:struts2

一、使用jar包

struts2必备jar包,另外还有使用jcaptcha4struts2插件所需jar包,

jcaptcha4struts2-2.0.2.jar 

jcaptcha-1.0-all.jar

imaging-01012005.jar 

同事要引入commons-collections-3.2.1.jar包。

二、web配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- 过滤器配置 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <!-- 设置web起始页类型 -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    
</web-app>

 

三、在应用中添加JCaptcha支持

jcaptcha4struts2可有多种配置方式,如自定义Action继承 JCaptcha4Struts2 提供的基础 Action 类,或者通过在struts.xml 中配置拦截器来使JCaptcha 生效。

1.继承 JcaptchaBaseAction 类

这种方式非常直接。只需要是让自己的Action 继承下面这个类:

com.google.code.jcaptcha4struts2.common.actions.JcaptchaBaseAction 。

下面就是一个这样做的简单例子。注意,除了继承 JCaptcha 提供的类之外不需要再与 JCaptcha 建立其它特殊的联系。这个类准确的处理了所有要被验证的 验证码并且会生成对应的错误信息。实际上 JCaptcha 提供的验证码基础Action 继承了ActionSupport类(因此我们的Action 也就和继承了ActionSupport一样方便的使用)。

public class MyFormActionWithCaptcha extends JCaptchaBaseAction {

    private static final long serialVersionUID = 6643871470200594031L;

    private String message;

    @Override
    public String execute() throws Exception {
        // Your action code here
        return super.execute();
    }


    @Override
    public void validateInput() {
        // Validation Logic Here

        // Note that you cannot override validate() method (it's final).
        // Instead, the logic should be provided in this method.
    }


    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

}                

2. 使用 JCaptchaValidationIntercepter 拦截器

二选一,你需要在自己的 java 类中不使用任何 JCaptcha 相关代码,或者,你需要继承除JCaptchaBaseAction 之外的其他类。在这种情况下,你可以通过配置 struts2 的JCaptchaValidationIntercepter 拦截器来拦截 Action 调用,来完成验证码的验证。

下面 struts.xml 的代码片段,展示了如何完成JCaptcha 拦截器的配置。

<package name="mypackage" extends="jcaptcha4struts2-default">

    <action name="myformaction" class="com.my.app.FormAction">

  <!-- Interceptor Stack -->
  <interceptor-ref name="jcaptchaDefaultStack" />

  <result name="input">/form.jsp</result>
  <result>/done.jsp</result>
  </action>
</package>

就像上面代码展现的那样,Action package 应该继承jcaptcha4struts2-default 包(这个包继承了 struts-default 包)。如果这样做不可用,就必须要自己显式配置下面这个拦截器类:

com.google.code.jcaptcha4struts2.core.interceptors.JCaptchaValidationIntercepter。

如果使用了上面给出的包作为你的默认包,就可以通过jcaptchaDefaultStack来简单的使用拦截器。这个拦截器栈由struts2的默认拦截器和JCaptcha提供的      JCaptchaValidationIntercepter 拦截器构成。

四、页面显示

编写jsp页面,并且添加下面的标记库声明,注意要与struts2的 标记库一起声明

<%@ taglib uri="http://code.google.com/p/jcaptcha4struts2/taglib/2.0" prefix="jcaptcha" %>

代码格式如下图,可调整大小查看:

接下来,在你的表单内,只需要包含 <jcaptcha:image label="Label Name Here"/> 标记即可。

示例:

<s:form action="myformaction">
  <s:textfield name="text" label="Text"/>
  <!-- Rest of Controls -->

  <!-- The JCaptcha Control -->
  <jcaptcha:image label="Type the text "/>

  <s:submit />
</s:form>

五、总结

验证码是可以出来了,但是有较多的问题:

1.生成之后发现图片太大,把其他的元素都挤到一边去了。jcaptcha:image里面有属性,高度跟宽度。。觉得设置相应的高度和宽度就可以了 ,但是设置高度跟宽度不管用。看下面的代码(这个可以项目的源代码,没想到会出现这样的bug)

public Component getBean(ValueStack stack, HttpServletRequest request,
            HttpServletResponse response) {

        JCaptchaImage bean = new JCaptchaImage(stack, request, response);

        // Set attribute values
        bean.setWidth(width);
        bean.setWidth(height);//这里我想作者本来的意思是设置高度的,没想到设置了两次宽度 
        bean.setTextSize(textSize);

        return bean;
    }

2.生成的图片不好辨认。。

总结:这个插件还有不少的问题有待改进。比如高度不能调整。生成的图片不好辨认等问题。另外拦截器没有得到调用,暂时还不知道是什么原因,有待进一步研究。 

 

 

 

posted @ 2012-12-12 16:23  終囿┅兲  阅读(937)  评论(0编辑  收藏  举报