Java 在Struts 2表单中显示SimpleCaptcha
我正在尝试用Struts 2实现Java 在Struts 2表单中显示SimpleCaptcha,java,jsp,struts2,captcha,simplecaptcha,Java,Jsp,Struts2,Captcha,Simplecaptcha,我正在尝试用Struts 2实现SimpleCaptcha,到目前为止,图像已经显示出来了。但是,它显示在的顶部 register.jsp: <constant name="struts.enable.DynamicMethodInvocation" value="true"/> public class RegisterAction extends ActionSupport { private String userId; pr
SimpleCaptcha
,到目前为止,图像已经显示出来了。但是,它显示在
的顶部
register.jsp:
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
public class RegisterAction extends ActionSupport {
private String userId;
private String userEmail1;
private String userEmail2;
private String userPassword1;
private String userPassword2;
private String captchaResponse;
private InputStream inputStream;
//getters and setters
public String create() {
//RegisterAction is the form bean of the current action and captchaResponse is the field of user input
String answer = (String) ActionContext.getContext().getSession().get("CorrectAnswer");
if (answer == null || getCaptchaResponse()==null || !answer.equals(getCaptchaResponse())){
addFieldError("captchaResponse", getText("error.captcha"));
}
return SUCCESS;
}
@Action(value = "captcha", results = {@Result(type="stream", params = {"contentType", "image/jpeg"})})
public String captcha() {
try {
Captcha captcha = new Captcha.Builder(200, 50).addText(new DefaultTextProducer()).gimp(new DropShadowGimpyRenderer()).build();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//write the image
CaptchaServletUtil.writeImage(outputStream, captcha.getImage());
//store the answer for this in session
ActionContext.getContext().getSession().put("CorrectAnswer", captcha.getAnswer());
//return image
inputStream = new ByteArrayInputStream(outputStream.toByteArray());
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
error.captcha=Invalid value of shown text!
...
登记
请填写下面的表格
" />
无法读取?刷新页面以获取新的验证码。
如何使图像显示在代码中指定的“输入验证码”文本字段上方?图像应通过验证码操作生成。然后在
“alt=”验证码图像“height=”45“>
“alt=”Reload“onclick=”document.forms[0]。captchaImg.src=''+'?id=''+Math.random();“style=”光标:指针“/>
无法读取?刷新页面以获取新的验证码。
这将构造Captcha和文本字段以输入值,Struts错误消息以显示captchaResponse
字段中的错误,以及刷新图标
注意:我们在这里使用的一个很好的技巧是javascriptMath.random()
函数,这种方法可以防止某些浏览器(如Firefox)缓存URL并不断发布相同的验证码图像,这样就可以在不做任何努力的情况下强制执行它以获取新值
下面是它的样子:
有关更多详细信息,请访问网站:这只是为了说明我是如何解决问题的。我不知道您可以将
放在
中。多亏了Roman C,我获得了验证码图像,并将其显示在我想要的位置
register.jsp:
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
public class RegisterAction extends ActionSupport {
private String userId;
private String userEmail1;
private String userEmail2;
private String userPassword1;
private String userPassword2;
private String captchaResponse;
private InputStream inputStream;
//getters and setters
public String create() {
//RegisterAction is the form bean of the current action and captchaResponse is the field of user input
String answer = (String) ActionContext.getContext().getSession().get("CorrectAnswer");
if (answer == null || getCaptchaResponse()==null || !answer.equals(getCaptchaResponse())){
addFieldError("captchaResponse", getText("error.captcha"));
}
return SUCCESS;
}
@Action(value = "captcha", results = {@Result(type="stream", params = {"contentType", "image/jpeg"})})
public String captcha() {
try {
Captcha captcha = new Captcha.Builder(200, 50).addText(new DefaultTextProducer()).gimp(new DropShadowGimpyRenderer()).build();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//write the image
CaptchaServletUtil.writeImage(outputStream, captcha.getImage());
//store the answer for this in session
ActionContext.getContext().getSession().put("CorrectAnswer", captcha.getAnswer());
//return image
inputStream = new ByteArrayInputStream(outputStream.toByteArray());
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
error.captcha=Invalid value of shown text!
...
登记
请填写下面的表格
" />
不识字?按F5刷新。
RegisterAction.java:
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
public class RegisterAction extends ActionSupport {
private String userId;
private String userEmail1;
private String userEmail2;
private String userPassword1;
private String userPassword2;
private String captchaResponse;
private InputStream inputStream;
//getters and setters
public String create() {
//RegisterAction is the form bean of the current action and captchaResponse is the field of user input
String answer = (String) ActionContext.getContext().getSession().get("CorrectAnswer");
if (answer == null || getCaptchaResponse()==null || !answer.equals(getCaptchaResponse())){
addFieldError("captchaResponse", getText("error.captcha"));
}
return SUCCESS;
}
@Action(value = "captcha", results = {@Result(type="stream", params = {"contentType", "image/jpeg"})})
public String captcha() {
try {
Captcha captcha = new Captcha.Builder(200, 50).addText(new DefaultTextProducer()).gimp(new DropShadowGimpyRenderer()).build();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//write the image
CaptchaServletUtil.writeImage(outputStream, captcha.getImage());
//store the answer for this in session
ActionContext.getContext().getSession().put("CorrectAnswer", captcha.getAnswer());
//return image
inputStream = new ByteArrayInputStream(outputStream.toByteArray());
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
error.captcha=Invalid value of shown text!
导入nl.captcha.captcha;
...
公共类RegisterAction扩展ActionSupport实现SessionAware、消息{
私有静态最终长serialVersionUID=1L;
私人Map会议;
私有字符串用户标识;
私有字符串userEmail1;
私有字符串userEmail2;
私有字符串userPassword1;
私有字符串userPassword2;
私有字符串;
@凌驾
公共字符串execute()引发异常{
//将用户插入数据库的业务逻辑
回归成功;
}
@凌驾
public void validate(){
Captcha Captcha=(Captcha)session.get(Captcha.NAME);
如果(!captcha.isCorrect(GetCaptchAnswer())){
addFieldError(“验证码应答”,不正确的验证码);
}
//其他验证
}
@凌驾
公共无效设置会话(映射会话){
this.session=会话;
}
//接球手和接球手
}
struts.xml:
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
public class RegisterAction extends ActionSupport {
private String userId;
private String userEmail1;
private String userEmail2;
private String userPassword1;
private String userPassword2;
private String captchaResponse;
private InputStream inputStream;
//getters and setters
public String create() {
//RegisterAction is the form bean of the current action and captchaResponse is the field of user input
String answer = (String) ActionContext.getContext().getSession().get("CorrectAnswer");
if (answer == null || getCaptchaResponse()==null || !answer.equals(getCaptchaResponse())){
addFieldError("captchaResponse", getText("error.captcha"));
}
return SUCCESS;
}
@Action(value = "captcha", results = {@Result(type="stream", params = {"contentType", "image/jpeg"})})
public String captcha() {
try {
Captcha captcha = new Captcha.Builder(200, 50).addText(new DefaultTextProducer()).gimp(new DropShadowGimpyRenderer()).build();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//write the image
CaptchaServletUtil.writeImage(outputStream, captcha.getImage());
//store the answer for this in session
ActionContext.getContext().getSession().put("CorrectAnswer", captcha.getAnswer());
//return image
inputStream = new ByteArrayInputStream(outputStream.toByteArray());
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
error.captcha=Invalid value of shown text!
/login.jsp
/register.jsp
web.xml:(
是可选的。)
SimpleCaptcha
nl.captcha.servlet.SimpleCaptchaServlet
验证码宽度
200
验证码高度
50
SimpleCaptcha
/simple-captcha.png
支柱2
org.apache.struts2.dispatcher.ng.filter.strutspreadexecutefilter
支柱2
/*
输出:
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
public class RegisterAction extends ActionSupport {
private String userId;
private String userEmail1;
private String userEmail2;
private String userPassword1;
private String userPassword2;
private String captchaResponse;
private InputStream inputStream;
//getters and setters
public String create() {
//RegisterAction is the form bean of the current action and captchaResponse is the field of user input
String answer = (String) ActionContext.getContext().getSession().get("CorrectAnswer");
if (answer == null || getCaptchaResponse()==null || !answer.equals(getCaptchaResponse())){
addFieldError("captchaResponse", getText("error.captcha"));
}
return SUCCESS;
}
@Action(value = "captcha", results = {@Result(type="stream", params = {"contentType", "image/jpeg"})})
public String captcha() {
try {
Captcha captcha = new Captcha.Builder(200, 50).addText(new DefaultTextProducer()).gimp(new DropShadowGimpyRenderer()).build();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//write the image
CaptchaServletUtil.writeImage(outputStream, captcha.getImage());
//store the answer for this in session
ActionContext.getContext().getSession().put("CorrectAnswer", captcha.getAnswer());
//return image
inputStream = new ByteArrayInputStream(outputStream.toByteArray());
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
error.captcha=Invalid value of shown text!
感谢您在回答问题之外的其他问题。这肯定会对每个人都有帮助。我还根据上面链接的SimpleCaptcha的sourceforge J2EE示例发布了一个答案。