当前位置:编程学习 > JAVA >>

在Action中用了get/set,但是没有放入值栈中去,放到session中也没反应


CodeAction.java
package Action;


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

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CodeAction extends ActionSupport  {
/**
 * http://465886163.iteye.com/blog/1189394
 */
private static final long serialVersionUID = 1L;
private String code1;
private ByteArrayInputStream inputStream;  

 public void setInputStream(ByteArrayInputStream inputStream) {  
        this.inputStream = inputStream;  
    }  
  
     public ByteArrayInputStream getInputStream() {  
        return inputStream;  
    }  

public String getCode1() {
return code1;
}

public void setCode1(String code1) {
this.code1 = code1;
}


 
    public String execute() throws Exception {  
        // 在内存中创建图象  
        int width = 85, height = 20;  
  
          BufferedImage image = new BufferedImage(width, height,  
                BufferedImage.TYPE_INT_RGB);  
  
        // 获取图形上下文  
        Graphics g = image.getGraphics();  
  
        // 生成随机类  
        Random random = new Random();  
  
        // 设定背景色  
        g.setColor(getRandColor(200, 250));  
        g.fillRect(0, 0, width, height);  
  
        // 设定字体  
        g.setFont(new Font("Times New Roman", Font.PLAIN, 18));  
  
        // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到  
        g.setColor(getRandColor(160, 200));  
        for (int i = 0; i < 155; i++) {  
            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);  
        }  
  
        // 取随机产生的认证码(6位数字)  
        String sRand = "";  
          for (int i = 0; i < 6; i++) {  
            String rand = String.valueOf(random.nextInt(10));  
            sRand += rand;  
            // 将认证码显示到图象中  
            g.setColor(new Color(20 + random.nextInt(110), 20 + random  
                    .nextInt(110), 20 + random.nextInt(110)));  
            // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成  
            g.drawString(rand, 13 * i + 6, 16);  
        }  
       code1=sRand;
       System.out.println(code1);
        // 将认证码存入SESSION  
        ActionContext.getContext().getApplication().put("rand", sRand);  
  
        // 图象生效  
        g.dispose();  
        ByteArrayOutputStream output = new ByteArrayOutputStream();  
        ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);  
        ImageIO.write(image, "JPEG", imageOut);  
        imageOut.close();  
        ByteArrayInputStream input = new ByteArrayInputStream(output  
                .toByteArray());  
       this.setInputStream(input);  
       return SUCCESS;  
    }  
  
    /*  
     * 给定范围获得随机颜色  
     */  
    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);  
    }    
}



jsp页面

 <body>
   <s:textfield  name="code" label="请输入验证码:"></s:textfield>
   <s:property value="#application.rand"/>
  <img src="rand.action" onclick="changeValidateCode(this)" /> 
  <s:debug></s:debug>
  </body>
配置文件
 <action name="rand" class="Action.CodeAction">  
     
            <result type="stream">  
                <param name="contentType">image/jpeg</param>  
                <param name="inputName">inputStream</param>  
            </result>
       
    
        </action>   Sturts  验证码 取值 --------------------编程问答-------------------- 没有试过这种做法,不过直接访问servlet获取验证码的例子:

http://blog.csdn.net/w4bobo/article/details/8259560 --------------------编程问答-------------------- 你现在是把生成的验证码字符串赋值给了code1,你再jsp页面上直接调用就可以了
<s:property value="code1"/>
这样就可以获得你的验证码字符串了 --------------------编程问答-------------------- 楼主这个代码是有放入值栈的,楼主你多刷新几次这个jsp就会看到你<s:property value="#application.rand"/>每次显示的验证码都是上一次的验证码。这个是因为楼主的jsp页面的顺序而导致的
<s:property value="#application.rand"/>
  <img src="rand.action" onclick="changeValidateCode(this)" /> 
  <s:debug></s:debug>
你是先显示application.rand后请求rand.action所以你第一次打开这个jsp页面的时候你先取这个application.rand还没请求当然就没有赋值到值栈。你刷新一下就会发现显示的是你上一次的rand --------------------编程问答--------------------  我把jsp代码改成下面的就能拿到值,但是还是上一次的验证啊,想问一下,怎么才能拿到当前的结果呢
<img src="rand.action" onclick="changeValidateCode(this)" /> 
   <s:property value="#application.rand"/>
--------------------编程问答-------------------- 2楼的亲故,code1他没有进入值栈 --------------------编程问答-------------------- 那你debug进你的Action类看下 看页面一加载的时候 进你的验证码那个CodeAction了吗。
然后你看在你的CodeAction返回(return)以前 那个code1的值是多少。
action中的参数市放在ValueStack中的,只要你在Action中有get方法那么在页面上就是可以获取的。
希望能够帮到你
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,