好记性不如赖笔头…………
1、准备工作,导入需要使用的生成图片验证码的包:ValidateCode.jar
2、创建生成图形码的类CodeResult.java,代码如下 :
package com.ckinghan.web.result;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import cn.dsna.util.images.ValidateCode;
import com.opensymphony.xwork2.ActionInvocation;
/**
* 获取验证码
* get/set方法用以在Struts中注入参数
* @author ckinghan
*/
public class CodeResult extends StrutsResultSupport {
//图片的宽度
private int width = 150;
//图片的高度
private int height = 20;
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
/**
* 生成并返回验证码,以下方法中,需要导入ValidateCode.jar包
*/
@Override
protected void doExecute(String arg0, ActionInvocation arg1)
throws Exception {
//创建ValidateCode对象,并生成验证码
ValidateCode code = new ValidateCode(width, height, 6, 20);
//获取response对象
HttpServletResponse response = ServletActionContext.getResponse();
//将生成的验证码返回
code.write(response.getOutputStream());
}
/**
* 获取验证码
* @return
*/
public String getImageCode(){
return "getImageCode";
}
}
3、创建UserLogin.java,可以不创建,根据须要更改,代码如下:
package com.ckinghan.web.action;
public class UserLogin {
/**
* 登陆页面
* @return
*/
public String login(){
return "login";
}
}
4、配置struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true"></constant>
<!-- 自定义全局参数 ,类型改为abstract,用以其它包继承-->
<package name="custom-result" extends="struts-default" abstract="true">
<!-- 自定义result type类型,在原本的类型中扩展了一种展示类型 -->
<result-types>
<result-type name="imageCode" class="com.ckinghan.web.result.CodeResult"></result-type>
</result-types>
<!-- 全局result,当子类没有result时,会来这里进行查找,前提是包要继承了本包 -->
<global-results>
<!-- 当成功后,返回的视图层为自定义的类型 :图片验证码-->
<result name="getImageCode" type="imageCode">
<!-- 注入验证码的宽度 -->
<param name="width">200</param>
<!-- 注入验证码的高度 -->
<param name="height">30</param>
</result>
</global-results>
</package>
<package name="reg" extends="custom-result">
<!-- 获取验证码: 这里的action中没有result标签,Struts在本动作配置找不到result后,会去父类中去找 -->
<action name="getCode" class="com.ckinghan.web.result.CodeResult" method="getImageCode"> </action>
<!-- 用户注册页面 -->
<action name="userReg" >
<result name="success">/reg.jsp</result>
</action>
</package>
<package name="login" extends="struts-default" namespace="/login">
<!-- 登陆页面 -->
<action name="login" class="com.ckinghan.web.action.UserLogin" method="login">
<result name="login">/login.jsp</result>
</action>
</package>
</struts>
5、创建reg.jsp文件,内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'reg.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="" method="post">
用 户 名:<input name="userName" type="text"/><br/>
输入密码:<input name="password" type="password"/><br/>
确认密码:<input name="truePassword" type="password"/><br/>
验 证 码:<input name="imageCode" type="text">
<img alt="请输入验证码" src="${pageContext.request.contextPath }/getCode.action">
<br/>
<input type="submit" value="注册 "/><input type="reset" value="重写"/>
</form>
</body>
</html>
6、创建login.jsp文件,内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>用户登陆</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="" method="post">
用 户 名:<input name="userName" type="text"/><br/>
输入密码:<input name="password" type="password"/><br/>
验 证 码:<input name="imageCode" type="text">
<img alt="请输入验证码" src="${pageContext.request.contextPath }/getCode.action">
<br/>
<input type="submit" value="登陆 "/>
</form>
</body>
</html>
配置完成。
访问userReg.action 的结果 如下 :
访问/login/login.action 的结果 如下 :
注意:本文章只是做测试用的,不能直接使用在项目中,须要根据自己的情况 更改后方可使用。