一.Jar 包准备
Struts2 中的
Hibernate 中
Sping 中
只需要一个 spring.jar
还需要 配置 log 的配套 jar
Slf4j-api.XXX .jar
Slf4j.-simle.XXX.jar
这两个包从网上下,具体搜下,注意添加包的时候只要这两个包,否则会报错
还需要 mysql 驱动包,具体自己找
二. 配置 WEB-INF/web.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"
version="2.5">
<display-name>NBS</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-
class>org.springframework.web.context.ContextLoaderListener</
listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-
class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
三.建立,注册测试 jsp 页面
在 WebContent/user/下
注册页面:regist.jsp
注册成功页面:welcome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
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><s:text name="
用户注册
" />
</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">
-->
<script type="text/javascript">
function changeValidateCode(obj) {
//获取当前的时间作为参数,无具体意义
var timenow = new Date().getTime();
//每次请求需要一个不同的参数,否则可能会返回同样的验证码
//这和浏览器的缓存机制有关系,也可以把页面设置为不缓存,这样就不用这
个参数了。
obj.src="rand?d="+timenow;
}
function changeImg(){
var img= document.getElementById("img");
var timenow = new Date().getTime();
img.src="rand?d="+timenow;
document.getElementById("validateCode").value="";
}
</script>
</head>
<body>
<s:form action="regist" theme="simple">
<s:text name="
请输入用户名
"></s:text>
<s:textfield name="id" label="
用户名
"></s:textfield><br>
<s:text name="
请输入密码
"></s:text>
<s:password name="password" label="
密码
"><
/s:password><br></br>
<s:text name="
请确认密码
"></s:text>
<s:password name="rePassword" label="
确认密码
"><
/s:password><br></br>
<s:text name="
请输入真实姓名
"></s:text>
<s:textfield name="name" label="
真实
"><
/s:textfield><br></br>
<s:text name="
请输入联系电话
"></s:text>
<s:textfield name="telephone" label="
电话
"><
/s:textfield><br></br>
<s:text name="
请输入地址
"></s:text>
<s:textfield name="address" label="
地址
"><
/s:textfield><br></br>
<img id="img" src="rand" onclick="changeValidateCode(this)"
/>
<input type="button" value="
看不清楚换一张
"
onclick="changeImg()"/><br></br>
<s:text name="
请输入验证码
"></s:text>
<s:textfield name="validateCode" label="
验证码
"
id="validateCode" /><br></br>
<s:submit value="
提交
"></s:submit>
</s:form>
</body>
</html>
第二个页面
Welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-
1">
<title>Insert title here</title>
</head>
<body>
欢迎
</body>
</html>
三.处理 action 的类
生成验证码的代码
rand.action 包下
产生随机数的工具类
package rand.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;
public class RandomNumUtil {
private ByteArrayInputStream image;//图像
private String str;//验证�?
private RandomNumUtil(){
init();//初始化属�?
}
/*
* 取得RandomNumUtil实例
*/
public static RandomNumUtil Instance(){
return new RandomNumUtil();
}
/*
* 取得验证码图�?
*/
public ByteArrayInputStream getImage(){
return this.image;
}
/*
* 取得图片的验证码
*/
public String getString(){
return this.str;
}
private void init() {
// 在内存中创建图象
int width=85, height=20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下�?