struts 2におけるjspページ上の検証コードの生成
22193 ワード
jspページに認証コードを表示する方法:
imageCode.actionリクエストを自動的に送信します.struts 2プロファイルは次のとおりです.
対応するアクションは次のとおりです.
ImageUtilクラスは次のとおりです.
これによりactionは、imgラベルがある場所にストリームファイルを返し、直接表示します.
「交換がはっきり見えない機能」を追加したい場合は、actionをもう一度やり直し、生成された検証コードピクチャを歩くたびにランダムにします.
記入した検証コードが正しいことをどのように検証しますか?次のようになります.
ページでのjqueryの使用
ajaxローカルリフレッシュを使用し、中の#msg、#numberInfoはdivブロックのidです.これは言うまでもなく、valid.actionが指すactionを次に書きます.
struts 2プロファイルには次のように書かれています.
は、ValidImageActionを指します.
以上struts 2を使用してコントローラを作りましたが、servletやstrutsであれば少し変更します
- :<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プロファイルは次のとおりです.
- <!-- -->
- <action name="imageCode" class="org.tarena.action.user.ImageAction">
- <result name="success" type="stream">
- <param name="inputName">imageStream</param>
- <param name="bufferSize">2048</param>
- </result>
- </action>
対応するアクションは次のとおりです.
- public class ImageAction {
- private InputStream imageStream;
-
- public InputStream getImageStream() {
- return imageStream;
- }
-
- public void setImageStream(InputStream imageStream) {
- this.imageStream = imageStream;
- }
-
- public String execute(){
- Map<String,BufferedImage> map = ImageUtil.getImage();
- // , session
- String key = map.keySet().iterator().next();
- Map<String,Object> session = ActionContext.getContext().getSession();
- session.put("code", key);
- // , stream
- BufferedImage image = map.get(key);
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(bos);
- try {
- jpeg.encode(image);
- byte[] bts = bos.toByteArray();
- imageStream = new ByteArrayInputStream(bts);
- return "success";
- } catch (IOException e) {
- e.printStackTrace();
- return "error";
- }
- }
- }
ImageUtilクラスは次のとおりです.
- public final class ImageUtil {
- private static final String[] chars = { "2", "3", "4", "5", "6",
- "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h" };
- private static final int SIZE = 5;
- private static final int LINES = 20;
- private static final int WIDTH = 200;
- private static final int HEIGHT = 100;
- private static final int FONT_SIZE = 60;
-
- public static Map<String,BufferedImage> getImage() {
- StringBuffer sb = new StringBuffer();
- BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
- BufferedImage.TYPE_INT_RGB);
- Graphics graphic = image.getGraphics();
- graphic.setColor(Color.LIGHT_GRAY);
- graphic.fillRect(0, 0, WIDTH, HEIGHT);
- Random ran = new Random();
- //������ַ�
- for(int i=1;i<=SIZE;i++){
-
- int r = ran.nextInt(chars.length);
- graphic.setColor(getRandomColor());
- graphic.setFont(new Font(null,Font.BOLD+Font.ITALIC,FONT_SIZE));
- graphic.drawString(chars[r],(i-1)*WIDTH/SIZE , HEIGHT/2);
- sb.append(chars[r]);//���ַ�������Session
- }
- //��������
- for(int i=1;i<=LINES;i++){
- graphic.setColor(getRandomColor());
- graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT), ran.nextInt(WIDTH),ran.nextInt(HEIGHT));
- }
- Map<String,BufferedImage> map = new HashMap<String,BufferedImage>();
- map.put(sb.toString(), image);
- return map;
- }
-
- private static Color getRandomColor(){
- Random ran = new Random();
- Color color = new Color(ran.nextInt(256),ran.nextInt(256),ran.nextInt(256));
- return color;
- }
- }
これによりactionは、imgラベルがある場所にストリームファイルを返し、直接表示します.
「交換がはっきり見えない機能」を追加したい場合は、actionをもう一度やり直し、生成された検証コードピクチャを歩くたびにランダムにします.
記入した検証コードが正しいことをどのように検証しますか?次のようになります.
ページでのjqueryの使用
- $(function(){
- //
- $("#validateCode").blur(function(){
- flag.validateCode=false;
- var txt = $(this).val();
- if(txt==""){
- $("#msg").html("<img src='../images/right.gif'/> !");//
- }else{
- $.post(
- "valid.action?dt="+new Date().getTime(),
- {"code":txt},
- function(data){
- if(data.ok){
- $("#numberInfo").html("<img src='../images/right.gif'/> !");
- flag.validateCode=true;
- }else{
- $("#numberInfo").html("<img src='../images/wrong.gif'/> !");
- }
- },
- "json"
- );
- }
- });
-
- });
ajaxローカルリフレッシュを使用し、中の#msg、#numberInfoはdivブロックのidです.これは言うまでもなく、valid.actionが指すactionを次に書きます.
struts 2プロファイルには次のように書かれています.
- <!-- Ajax -->
- <action name="valid" class="org.tarena.action.user.ValidImageAction">
- <result name="success" type="json"></result>
- </action>
は、ValidImageActionを指します.
- public class ValidImageAction {
- private String code = "";
- private boolean ok = false;
-
- public boolean isOk() {
- return ok;
- }
-
- public void setOk(boolean ok) {
- this.ok = ok;
- }
-
- public String execute(){
- Map<String,Object> session = ActionContext.getContext().getSession();
- //HttpServletRequest request=ServletActionContext.getRequest();
- String scode = (String)session.get("code");
- if(code.equals(scode)){
- ok = true;
- }else{
- ok = false;
- }
- return "success";
- }
-
- @JSON(serialize=false)// code json
- public String getCode() {
- return code;
- }
-
- public void setCode(String code) {
- this.code = code;
- }
-
- }
以上struts 2を使用してコントローラを作りましたが、servletやstrutsであれば少し変更します