下面应用struts2框架实现一个简单的聊天平台。按流程分为以下几个步骤:
一、准备工作:
1.建立一个数据库think,建立数据表think_login:
再建立一个think_friend表:
2.在eclipse中建立一个工程Struts2_Chat ;
3.在工程的lib目录下添加相关包:
和lib同级目录下建立一个web.xml文件完成相关声明配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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>*.action</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4.在WEB项目的源码文件夹中,建立struts.xml配置文件,这是映射jsp文件和java文件的,即完成前后台的信息传输:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="utf-8" />
<package name="struts2" extends="struts-default">
<action name="">
</action>
</package>
</struts>
现在action里面还是空的,后面会逐渐增加action标签
5.编写数据库操作类MysqlUser.java,封装数据库的连接和增删改查操作:
package com.zt;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.*;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class MysqlUser {
Connection con=null; //连接
Statement stat=null; //操作
ResultSet rs=null; //查询的结果集
//连接数据库
public MysqlUser(){
try{
Class.forName("com.mysql.jdbc.Driver");
con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/think","root","****");
stat=(Statement) con.createStatement();
}catch(Exception e){
con=null;
}
}
//查并返回结果集
public ResultSet executeQuery(String sql){
try{
rs=stat.executeQuery(sql);
}catch(Exception e){
rs=null;
}
return rs;
}
//增、删或者改操作
public int executeUpdate(String sql){
try{
stat.executeUpdate(sql);
return 0;
}catch(Exception e){
}
return -1;
}
}
现在来看一下,目录是这样的:
二、实现注册登录:
1.编写inex.jsp,作为入口文件,实现登录:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="index.action" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" id="name" name="name"></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="pwd" id="pwd">
</tr>
<tr>
<td><input type="submit" name="sub" id="sub" value="登录"></td>
<td><a href="login.jsp">还没有帐号,去注册</a>
</tr>
</table>
</form>
</body>
</html>
在struts.xml中映射这个action:
<action name="index" class="com.zt.IndexAction" method="land">
<result name="success">welcome.jsp</result>
<result name="error">land_error.jsp</result>
</action>
创建com.zt这个包,在这个包下编写IndexAction.java文件,按照struts.xml配置文件中的映射,land函数完成后台登录的功能:
package com.zt;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.opensymphony.xwork2.ActionSupport;
public class IndexAction extends ActionSupport {
private String name;
private String pwd;
private MysqlUser mysqlUser=new MysqlUser();//连接数据库;
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setPwd(String pwd){
this.pwd=pwd;
}
public String getPwd(){
return this.pwd;
}
//登录
public String land(){
String sql="select * from think_login where name='"+this.getName()+"' and pwd='"+this.getPwd()+"'";
ResultSet rs=mysqlUser.executeQuery(sql);
try {
if(rs.next())
return "success";
else
return "error";
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "error";
}
}
这里登录失败的话跳到land_error.jsp界面,代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
<script>
alert('帐号或密码有错');
</script>
</body>
</html>
登录成功则跳到welcome.jsp界面,这个第三步再看。
2.编写login.jsp文件,实现注册:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>Insert title here</title>
</head>
<script>
function fun(){
form1=document.getElementById("form1");
name=form1.name.value;
pwd=form1.pwd.value;
rpwd=form1.rpwd.value;
//document.write(name+pwd+rpwd);
if(name=="" || pwd=="" || rpwd=="")
alert('输入不可以为空');
else{
if(pwd!=rpwd)
alert('两次输入密码不相等');
else
form1.submit();
}
}
</script>
<body>
<form action="login.action" method="post" name="form1" id="form1">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="name" id="name"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd" id="pwd" ></td>
</tr>
<tr>
<td>确认密码:</td>
<td><input type="password" name="rpwd" id="rpwd"></td>
</tr>
<tr>
<td></td>
<td><input type="button" id="sub" name="sub" value="注册" οnclick="fun()"></td>
</tr>
</table>
</form>
</body>
</html>
在struts.xml中映射这个action:
<action name="login" class="com.zt.IndexAction" method="login">
<result name="success">index.jsp</result>
<result name="error">login_error.jsp</result>
</action>
按照struts.xml配置文件中的映射,在com.zt包下的IndexAction这个java文件中编写login函数完成后台注册的功能:
//注册
public String login() throws SQLException{
String sql="select * from think_login where name='"+this.getName()+"'";
ResultSet rs=mysqlUser.executeQuery(sql);
if(rs.next())
return "error";
else{
String sql1="insert into think_login values('"+this.getName()+"','"+this.getPwd()+"')";
int a=mysqlUser.executeUpdate(sql1);
if(a>-1)
return "success";
else
return "error";
}
}
注册成功进入登录界面(index.jsp),注册失败进入login_error.jsp界面:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
<script>
alert('该用户名已被注册');
</script>
</body>
</html>
值得注意的是,这里的表单都要采用post的方式提交,不然提交的数据会有误,我也不知道为什么,反正声明为post就不会出错。
现在再来看一下,目录是这样的:
三、登录成功后,来到主界面welcome.jsp
1.编写welcome.jsp页面:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
<table width="100%">
<tr>
<td width="33%"><a href="welcome.jsp">添加好友</a></td>
<td width="33%"><a href="fr_new.jsp">新朋友</a></td>
<td width="33%"><a href="fr_my.jsp">朋友</a></td>
</tr>
</table>
<center>
<form action="find.action" method="post">
<input type="text" name="name" id="name">
<input type="submit" name="sub" id="sub" value="查询">
</form>
</center>
</body>
</html>
<action name="find" class="com.zt.AddAction" method="find">
<result name="suc">fr_add.jsp</result>
<result name="error">welcome.jsp</result>
</action>
3.在com.zt包下创建AddAction.java文件,并编写find函数完成搜索帐号的功能:
package com.zt;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class AddAction extends ActionSupport{
private String name;
private MysqlUser mysqlUser=new MysqlUser();
private Map<String,Object> session;
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
//搜索帐号
public String find() throws SQLException{
String sql="select name from think_login where name='"+this.getName()+"'";
ResultSet rs=mysqlUser.executeQuery(sql);
if(rs.next()){
//存在该人
ActionContext context=ActionContext.getContext();
session=context.getSession();
session.put("fr_applyor", this.getName());
return "suc";
}else{
//不存在该人
this.setName("");
return "error";
}
}
}
如何存在该人,跳转到fr_add.jsp界面
4.fr_add.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
<center>
添加<s:property value="name"/>为好友<br>
发送验证消息:
<form action="app.action" method="post">
<textarea name="qq" id="qq"></textarea><br>
<input type="submit" name="sub" id="sub" value="发送">
</form>
</center>
</body>
</html>
添加action:
<action name="app" class="com.zt.AddAction" method="app">
<result name="suc">fr_add.jsp</result>
<result name="error">add_error.jsp</result>
</action>
AddAction.java中添加app方法:
//申请添加好友
public String app(){
ActionContext context=ActionContext.getContext();
session=context.getSession();
me=(String) session.get("me");//我
String applyor=(String) session.get("fr_applyor");//对方
String sql="insert into think_friend values(0,'"+applyor+"','"+me+"','"+this.getQq()+"')"; //0表示不是好友
int a=mysqlUser.executeUpdate(sql);
if(a>-1)
return "suc";
else
return "error";
}
现在的目录是这样的:
四、来看看fr_new.jsp,即我的新朋友: