ModelAndViewの使用方法-検証コード生成に関するbbossmvc restful例1


ModelAndViewの使用方法-検証コード生成に関するbbossmvc restful例1
 
bboss mvcはbbossgroups 2.1とともにリリースされます.ここではbboss-mvcのrestfulスタイルの例を見てみましょう.
 
  1.グラフィックコードチェックコントローラ-ImageValidatorController
 
/*
 *  Copyright 2008 biaoping.yin
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.frameworkset.web.spi.validator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.frameworkset.util.annotations.Attribute;
import org.frameworkset.util.annotations.AttributeScope;
import org.frameworkset.util.annotations.HandlerMapping;
import org.frameworkset.util.annotations.PathVariable;
import org.frameworkset.util.annotations.RequestMethod;
import org.frameworkset.util.annotations.RequestParam;
import org.frameworkset.web.servlet.ModelAndView;

/**
 * <p>Title: ImageValidatorController.java</p> 
 * <p>Description: </p>
 * <p>bboss workgroup</p>
 * <p>Copyright (c) 2008</p>
 * @Date 2010-11-13
 * @author biaoping.yin
 * @version 1.0
 */
@HandlerMapping("/rest/imagevalidator")
public class ImageValidatorController  {
	//http://localhost:8080/bboss-mvc/rest/imagevalidator/abcefg1234/6
	@HandlerMapping(value="/{codelist}/{codenum}",method={RequestMethod.GET,RequestMethod.POST})
	public void generateImageCode(
								  @PathVariable("codelist") String codelist,
								  @PathVariable("codenum") int codenum,
								  HttpServletRequest request,HttpServletResponse response)
	{
		String codekey  = "imagecodekey";
		HttpSession session = request.getSession(true);
		RandImgCreater rc = new RandImgCreater(response,codenum,codelist);

		String rand = rc.createRandImage();
		session.setAttribute(codekey,rand);

	}
//	http://localhost:8080/bboss-mvc/rest/imagevalidator
	@HandlerMapping(method={RequestMethod.POST,RequestMethod.GET})
	public ModelAndView checkImageCode(@RequestParam(name="imagecode") String imagecode,
			@Attribute(name="imagecodekey",scope=AttributeScope.SESSION_ATTRIBUTE) String oldcode)
	{
		String message = "";
		if(imagecode == null || imagecode.equals(""))
			message = "        。";
		else if(oldcode == null)
			message = "   ,       。";
		else if(!imagecode.equals(oldcode))
		{
			message = "     ,     。";
		}
		else
		{
			message = "ok。";
		}	
			
		ModelAndView view = new ModelAndView("/validate/imagevalidate","message",message);
		return view;

	}
	
	
}

 
2.グラフィックコードを生成するコンポーネント
/*
 *  Copyright 2008 biaoping.yin
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.frameworkset.web.spi.validator;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * Title: RandImgCreater.java
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * bboss workgroup
 * </p>
 * <p>
 * Copyright (c) 2008
 * </p>
 * 
 * @Date 2010-11-13
 * @author biaoping.yin
 * @version 1.0
 */
public class RandImgCreater {
	private static final String CODE_LIST = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
	private HttpServletResponse response = null;
	private static final int HEIGHT = 20;
	private static final int FONT_NUM = 4;
	private int width = 0;
	private int iNum = 0;
	private String codeList = "";
	private boolean drawBgFlag = false;

	private int rBg = 0;
	private int gBg = 0;
	private int bBg = 0;

	public RandImgCreater(HttpServletResponse response) {
		this.response = response;
		this.width = 13 * FONT_NUM + 12;
		this.iNum = FONT_NUM;
		this.codeList = CODE_LIST;
	}

	public RandImgCreater(HttpServletResponse response, int iNum,
			String codeList) {
		this.response = response;
		this.width = 13 * iNum + 12;
		if(iNum > 0)
			this.iNum = iNum;
		if(codeList != null)
			this.codeList = codeList;
	}

	public String createRandImage() {
		BufferedImage image = new BufferedImage(width, HEIGHT,
				BufferedImage.TYPE_INT_RGB);

		Graphics g = image.getGraphics();

		Random random = new Random();

		if (drawBgFlag) {
			g.setColor(new Color(rBg, gBg, bBg));
			g.fillRect(0, 0, width, HEIGHT);
		} else {
			g.setColor(getRandColor(200, 250));
			g.fillRect(0, 0, width, HEIGHT);

			for (int i = 0; i < 155; i++) {
				g.setColor(getRandColor(140, 200));
				int x = random.nextInt(width);
				int y = random.nextInt(HEIGHT);
				int xl = random.nextInt(12);
				int yl = random.nextInt(12);
				g.drawLine(x, y, x + xl, y + yl);
			}
		}

		g.setFont(new Font("Times New Roman", Font.PLAIN, 18));

		String sRand = "";
		for (int i = 0; i < iNum; i++) {
			int rand = random.nextInt(codeList.length());
			String strRand = codeList.substring(rand, rand + 1);
			sRand += strRand;
			g.setColor(new Color(20 + random.nextInt(110), 20 + random
					.nextInt(110), 20 + random.nextInt(110)));
			g.drawString(strRand, 13 * i + 6, 16);
		}
		g.dispose();
		try {
			ImageIO.write(image, "JPEG", response.getOutputStream());
		} catch (IOException e) {

		}

		return sRand;
	}

	public void setBgColor(int r, int g, int b) {
		drawBgFlag = true;
		this.rBg = r;
		this.gBg = g;
		this.bBg = b;
	}

	private Color getRandColor(int fc, int bc) {
		Random random = new Random();
		if (fc > 255)
			fc = 255;
		if (bc > 255)
			bc = 255;
		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);
		return new Color(r, g, b);
	}

}
 
3.表示されるjspページWebRoot/jsp/validate/imagevalidate.jsp
<%@ page contentType="text/html;charset=UTF-8"  %>
<%@ taglib uri="/WEB-INF/pager-taglib.tld" prefix="pg"%>
<form name="frm" method="post" action="/bboss-mvc/rest/imagevalidator">
Hello Image Test<br/>
checkCode:<img src="/bboss-mvc/rest/imagevalidator/abcdefghijk123456/5"><br/>
please input the checkCode:<input type="text" name="imagecode" value=""><br/>
<input type="submit" name="btn1" value="check">
</form> 

${message}


 
4.コントローラ構成
 
5.使用方法と説明
ImageValidatorControllerマッピングのルートパスは/rest/imagevalidatorです.
@HandlerMapping("/rest/imagevalidator")
public class ImageValidatorController 

メソッドgenerateImageCodeメソッドは、主にグラフィックコードを生成するために使用される次のアドレスにマッピングできます.
http://localhost:8080/bboss-mvc/rest/imagevalidator/abcefg1234/6
	@HandlerMapping(value="/{codelist}/{codenum}",method={RequestMethod.GET,RequestMethod.POST})
	public void generateImageCode(
								  @PathVariable("codelist") String codelist,
								  @PathVariable("codenum") int codenum,
								  HttpServletRequest request,HttpServletResponse response)
 
 
    abcefg1234   generateImageCode   codelist  ,          
     /6  generateImageCode   codenum  ,         
  generateImageCode                 ,         imagecodekey  key session   。
        。
 
 
メソッドcheckImageCodeユーザーが入力したグラフィックコードとgenerateImageCodeメソッドがセッションに格納されている画像コードを検証し、比較結果に基づいて対応する結果情報をフロントエンドに出力し、比較が完了したらアドレスにジャンプします.
/validate/imagevalidate

bboss-mvcは、このアドレスをマッピングルールに従ってページWebRoot/jsp/validate/imagevalidateにマッピングする.jsp
http://localhost:8080/bboss-mvc/rest/imagevalidator
	@HandlerMapping(method={RequestMethod.POST,RequestMethod.GET})
	public ModelAndView checkImageCode(@RequestParam(name="imagecode") String imagecode,
			@Attribute(name="imagecodekey",scope=AttributeScope.SESSION_ATTRIBUTE) String oldcode)
      
imagecode    ,
oldcode       session   key 
imagecodekey    

 
6.マッピング規則構成bboss-mvc.xml
 
 
<property name="viewResolver" class="org.frameworkset.web.servlet.view.InternalResourceViewResolver" singlable="true">
        <property name="viewClass" value="org.frameworkset.web.servlet.view.JstlView"/>
        <property name="prefix" value="/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </property>