struts 2におけるjspページ上の検証コードの生成


jspページに認証コードを表示する方法:
 

  
  
  
  
  1. :<img class="yzm_img" align="middle" id="validateImage" src="imageCode.action" width="150" height="40" onclick=""http://blog.51cto.com/viewpic.php?refimg=" + this.src='imageCode.action?time-'+(new Date()).getTime();"/> 

imageCode.actionリクエストを自動的に送信します.struts 2プロファイルは次のとおりです.
 

  
  
  
  
  1. <!--   --> 
  2.         <action name="imageCode" class="org.tarena.action.user.ImageAction"> 
  3.             <result name="success" type="stream"> 
  4.                 <param name="inputName">imageStream</param> 
  5.                 <param name="bufferSize">2048</param> 
  6.             </result> 
  7.         </action> 

対応するアクションは次のとおりです.
 

  
  
  
  
  1. public class ImageAction {  
  2.     private InputStream imageStream;  
  3.       
  4.     public InputStream getImageStream() {  
  5.         return imageStream;  
  6.     }  
  7.  
  8.     public void setImageStream(InputStream imageStream) {  
  9.         this.imageStream = imageStream;  
  10.     }  
  11.  
  12.     public String execute(){  
  13.         Map<String,BufferedImage> map = ImageUtil.getImage();  
  14.         // , session  
  15.         String key = map.keySet().iterator().next();  
  16.         Map<String,Object> session = ActionContext.getContext().getSession();  
  17.         session.put("code", key);  
  18.         // , stream  
  19.         BufferedImage image = map.get(key);  
  20.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  21.         JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(bos);  
  22.         try {  
  23.             jpeg.encode(image);  
  24.             byte[] bts = bos.toByteArray();  
  25.             imageStream = new ByteArrayInputStream(bts);  
  26.             return "success";  
  27.         } catch (IOException e) {  
  28.             e.printStackTrace();  
  29.             return "error";  
  30.         }  
  31.     }  

ImageUtilクラスは次のとおりです.
 

  
  
  
  
  1. public final class ImageUtil {  
  2.     private static final String[] chars = { "2""3""4""5""6",  
  3.             "7""8""9""a""b""c""d""e""f""g""h" };  
  4.     private static final int SIZE = 5;  
  5.     private static final int LINES = 20;  
  6.     private static final int WIDTH = 200;  
  7.     private static final int HEIGHT = 100;  
  8.     private static final int FONT_SIZE = 60;  
  9.       
  10.     public static Map<String,BufferedImage> getImage() {  
  11.         StringBuffer sb = new StringBuffer();  
  12.         BufferedImage image = new BufferedImage(WIDTH, HEIGHT,  
  13.                 BufferedImage.TYPE_INT_RGB);  
  14.         Graphics graphic = image.getGraphics();  
  15.         graphic.setColor(Color.LIGHT_GRAY);  
  16.         graphic.fillRect(00, WIDTH, HEIGHT);  
  17.         Random ran = new Random();  
  18.         //������ַ�  
  19.         for(int i=1;i<=SIZE;i++){  
  20.               
  21.             int r = ran.nextInt(chars.length);  
  22.             graphic.setColor(getRandomColor());  
  23.             graphic.setFont(new Font(null,Font.BOLD+Font.ITALIC,FONT_SIZE));  
  24.             graphic.drawString(chars[r],(i-1)*WIDTH/SIZE , HEIGHT/2);  
  25.             sb.append(chars[r]);//���ַ�������Session  
  26.         }  
  27.         //��������  
  28.         for(int i=1;i<=LINES;i++){  
  29.             graphic.setColor(getRandomColor());  
  30.             graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT), ran.nextInt(WIDTH),ran.nextInt(HEIGHT));  
  31.         }  
  32.         Map<String,BufferedImage> map = new HashMap<String,BufferedImage>();  
  33.         map.put(sb.toString(), image);  
  34.         return map;  
  35.     }  
  36.       
  37.     private static Color getRandomColor(){  
  38.         Random ran = new Random();  
  39.         Color color = new Color(ran.nextInt(256),ran.nextInt(256),ran.nextInt(256));  
  40.         return color;  
  41.     }  
  42. }  

これによりactionは、imgラベルがある場所にストリームファイルを返し、直接表示します.
「交換がはっきり見えない機能」を追加したい場合は、actionをもう一度やり直し、生成された検証コードピクチャを歩くたびにランダムにします.
記入した検証コードが正しいことをどのように検証しますか?次のようになります.
ページでのjqueryの使用
 

  
  
  
  
  1. $(function(){  
  2.    //  
  3.         $("#validateCode").blur(function(){  
  4.             flag.validateCode=false;  
  5.             var txt = $(this).val();  
  6.             if(txt==""){  
  7.                 $("#msg").html("<img src='../images/right.gif'/> !");//  
  8.             }else{  
  9.                 $.post(  
  10.                     "valid.action?dt="+new Date().getTime(),  
  11.                     {"code":txt},  
  12.                     function(data){  
  13.                         if(data.ok){  
  14.                             $("#numberInfo").html("<img src='../images/right.gif'/> !");  
  15.                             flag.validateCode=true;  
  16.                         }else{  
  17.                             $("#numberInfo").html("<img src='../images/wrong.gif'/> !");  
  18.                         }  
  19.                     },  
  20.                     "json" 
  21.                 );  
  22.             }  
  23.         });  
  24.  
  25. }); 

ajaxローカルリフレッシュを使用し、中の#msg、#numberInfoはdivブロックのidです.これは言うまでもなく、valid.actionが指すactionを次に書きます.
struts 2プロファイルには次のように書かれています.
 

  
  
  
  
  1. <!-- Ajax  --> 
  2.     <action name="valid" class="org.tarena.action.user.ValidImageAction"> 
  3.         <result name="success" type="json"></result> 
  4.     </action> 

は、ValidImageActionを指します.
 

  
  
  
  
  1. public class ValidImageAction {  
  2.     private String code = "";  
  3.     private boolean ok = false;  
  4.       
  5.     public boolean isOk() {  
  6.         return ok;  
  7.     }  
  8.  
  9.     public void setOk(boolean ok) {  
  10.         this.ok = ok;  
  11.     }  
  12.  
  13.     public String execute(){  
  14.         Map<String,Object> session = ActionContext.getContext().getSession();  
  15.         //HttpServletRequest request=ServletActionContext.getRequest();  
  16.         String scode = (String)session.get("code");  
  17.         if(code.equals(scode)){  
  18.             ok = true;  
  19.         }else{  
  20.             ok = false;  
  21.         }  
  22.         return "success";  
  23.     }  
  24.  
  25.     @JSON(serialize=false)// code json  
  26.     public String getCode() {  
  27.         return code;  
  28.     }  
  29.  
  30.     public void setCode(String code) {  
  31.         this.code = code;  
  32.     }  
  33.       

以上struts 2を使用してコントローラを作りましたが、servletやstrutsであれば少し変更します