通讯录管理功能的实现
查看联系人
增加联系人
查找联系人
修改联系人信息
lookFriends.jsp
<%@page import="JavaBean.MyFriBean"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
增加联系人
好友姓名
好友电话
邮箱地址
工作单位
家庭住址
QQ
<%
ArrayList friends=(ArrayList)session.getAttribute("friends");
if(friends==null||friends.size()==0){
%>
<%="您还没有添加联系人!"%>
<%
}else{
for(int i=friends.size()-1;i>=0;i--){
MyFriBean ff=(MyFriBean)friends.get(i);
%>
<%=ff.getName()%>
<%=ff.getPhone()%>
<%=ff.getEmail()%>
<%=ff.getWorkplace()%>
<%=ff.getPlace()%>
<%=ff.getQQ()%>
<%
}
}
%>
lookFriends.jsp对应的业务控制器FindFriAction
package edu.friendManager.Action;
import DBJavaBean.DB;
import com.opensymphony.xwork2.ActionSupport;
import java.sql.ResultSet;
import javax.servlet.http.HttpServletRequest;
import javax.swing.JOptionPane;
import org.apache.struts2.interceptor.ServletRequestAware;
public class FindFriAction extends ActionSupport implements ServletRequestAware{
private String friendname;
private String userName;
private ResultSet rs=null;
private String message=ERROR;
private HttpServletRequest request;
public String getFriendname() {
return friendname;
}
public void setFriendname(String friendname) {
this.friendname = friendname;
}
public void setServletRequest(HttpServletRequest hsr) {
request=hsr;
}
public void message(String msg){
int type=JOptionPane.YES_NO_OPTION;
String title="信息提示";
JOptionPane.showMessageDialog(null,msg,title,type);
}
public void validate(){
if(this.getFriendname().equals("")||this.getFriendname().length()==0){
message("联系人姓名不允许为空!");
addFieldError("friendname","联系人姓名不允许为空!");
}else{
try{
DB mysql=new DB();
userName=mysql.returnLogin(request);
rs=mysql.selectFri(request, userName, this.getFriendname());
if(!rs.next()){
message("联系人姓名不存在!");
addFieldError("friendname","联系人姓名不存在!");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
public String execute() throws Exception {
DB mysql=new DB();
userName=mysql.returnLogin(request);
String fri=mysql.findFri(request, userName, this.getFriendname());
if(fri.equals("ok")){
message=SUCCESS;
}
return message;
}
}
lookFriends.jsp使用到的JavaBean,MyFriBean
package JavaBean;
public class MyFriBean{
private String name;
private String phone;
private String email;
private String workplace;
private String place;
private String QQ;
public MyFriBean(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getWorkplace() {
return workplace;
}
public void setWorkplace(String workplace) {
this.workplace = workplace;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getQQ() {
return QQ;
}
public void setQQ(String QQ) {
this.QQ = QQ;
}
}
addFriend.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
查看联系人
所对应的action类AddFriAction
package edu.friendManager.Action;
import DBJavaBean.DB;
import com.opensymphony.xwork2.ActionSupport;
import java.sql.*;
import javax.servlet.http.HttpServletRequest;
//import javax.swing.JOptionPane;
import org.apache.struts2.interceptor.ServletRequestAware;
public class AddFriAction extends ActionSupport implements ServletRequestAware{
private String name;
private String phone;
private String email;
private String workplace;
private String place;
private String QQ;
private ResultSet rs=null;
private String message=ERROR;
private HttpServletRequest request;
private String userName=null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getWorkplace() {
return workplace;
}
public void setWorkplace(String workplace) {
this.workplace = workplace;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getQQ() {
return QQ;
}
public void setQQ(String QQ) {
this.QQ = QQ;
}
@Override
public void setServletRequest(HttpServletRequest hsr) {
request=hsr;
}
// public void message(String msg){
// int type=JOptionPane.YES_NO_OPTION;
// String title="信息提示";
// JOptionPane.showMessageDialog(null,msg,title,type);
// }
@Override
public void validate(){
if(getName()==null||getName().length()==0){
addFieldError("name","用户姓名不允许为空");
}else{
try {
DB mysql=new DB();
userName=mysql.returnLogin(request);
rs=mysql.selectFri(request, userName, this.getName());
if(rs.next()){
addFieldError("name","此用户已存在!");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if(getPhone()==null||getPhone().length()==0){
addFieldError("phone","用户电话不允许为空");
}
if(getEmail()==null||getEmail().length()==0){
addFieldError("email","邮箱地址不允许为空");
}
if(getWorkplace()==null||getWorkplace().length()==0){
addFieldError("workplace","工作单位不允许为空");
}
if(getPlace()==null||getPlace().length()==0){
addFieldError("place","家庭住址不允许为空");
}
if(getQQ()==null||getQQ().length()==0){
addFieldError("QQ","用户QQ不允许为空");
}
}
public String execute() throws Exception{
DB mysql=new DB();
userName=mysql.returnLogin(request);
String fri=mysql.insertFri(request, userName, this.getName(), this.getPhone(), this.getEmail(), this.getWorkplace(), this.getPlace(), this.getQQ());
if(fri.equals("ok")){
message=SUCCESS;
}else if(fri.equals("one")){
message=INPUT;
}
return message;
}
}
findFriend.jsp
<%@page import="JavaBean.MyFriBean"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
增加联系人
查看联系人
用户姓名
用户电话
邮箱地址
用户职称
家庭住址
用户QQ
用户操作
<%
ArrayList friends=(ArrayList)session.getAttribute("findfriend");
if(friends==null||friends.size()==0){
%>
<%="您还没有添加联系人!"%>
<%
}else{
for(int i=friends.size()-1;i>=0;i--){
MyFriBean ff=(MyFriBean)friends.get(i);
%>
<%=ff.getName()%>
<%=ff.getPhone()%>
<%=ff.getEmail()%>
<%=ff.getWorkplace()%>
<%=ff.getPlace()%>
<%=ff.getQQ()%>
修改
删除
<%
}
}
%>
findFriend.jsp所使用的JavaBean在前面已经给出MyFriBean
updateFriend.jsp
<%@page import="JavaBean.MyFriBean"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
增加联系人
查看联系人
<%
ArrayList delemess=(ArrayList)session.getAttribute("findfriend");
if(delemess==null||delemess.size()==0){
%>
<%="您还没有添加联系人!"%>
<%
}else{
for(int i=delemess.size()-1;i>=0;i--){
MyFriBean ff=(MyFriBean)delemess.get(i);
%>
<%
}
}
%>
对应的业务控制器UpdateFriAction
package edu.friendManager.Action;
import DBJavaBean.DB;
import com.opensymphony.xwork2.ActionSupport;
import java.sql.ResultSet;
import javax.servlet.http.HttpServletRequest;
import javax.swing.JOptionPane;
import org.apache.struts2.interceptor.ServletRequestAware;
public class UpdateFriAction extends ActionSupport implements ServletRequestAware{
private String name;
private String phone;
private String email;
private String workplace;
private String place;
private String QQ;
private String message=ERROR;
private HttpServletRequest request;
private ResultSet rs=null;
private String userName;
private String friendname;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getWorkplace() {
return workplace;
}
public void setWorkplace(String workplace) {
this.workplace = workplace;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getQQ() {
return QQ;
}
public void setQQ(String QQ) {
this.QQ = QQ;
}
@Override
public void setServletRequest(HttpServletRequest hsr) {
request=hsr;
}
@Override
public void validate(){
if(getName()==null||getName().length()==0){
addFieldError("name","用户姓名不允许为空");
}
if(getPhone()==null||getPhone().length()==0){
addFieldError("phone","用户电话不允许为空");
}
if(getEmail()==null||getEmail().length()==0){
addFieldError("email","邮箱地址不允许为空");
}
if(getWorkplace()==null||getWorkplace().length()==0){
addFieldError("workplace","工作单位不允许为空");
}
if(getPlace()==null||getPlace().length()==0){
addFieldError("place","家庭住址不允许为空");
}
if(getQQ()==null||getQQ().length()==0){
addFieldError("QQ","用户QQ不允许为空");
}
}
public String execute() throws Exception {
DB mysql=new DB();
userName=mysql.returnLogin(request);
friendname=mysql.returnFri(request);
String fri=mysql.updateFri(request, userName,friendname, this.getName(), this.getPhone(), this.getEmail(), this.getWorkplace(), this.getPlace(), this.getQQ());
if(fri.equals("ok")){
message=SUCCESS;
}
return message;
}
}
删除操作所对应的业务控制器DeleteFriAction
package edu.friendManager.Action;
import DBJavaBean.DB;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
//import javax.swing.JOptionPane;
import org.apache.struts2.interceptor.ServletRequestAware;
public class DeleteFriAction extends ActionSupport implements ServletRequestAware{
private String message=ERROR;
private String userName;
private String name;
private HttpServletRequest request;
public void setServletRequest(HttpServletRequest hsr) {
request=hsr;
}
public String execute() throws Exception {
DB mysql=new DB();
userName=mysql.returnLogin(request);
name=mysql.returnFri(request);
String del=mysql.deleteFri(request, userName, name);
if(del.equals("ok")){
message=SUCCESS;
}
return message;
}
// public void message(String msg){
// int type=JOptionPane.YES_NO_OPTION;
// String title="信息提示";
// JOptionPane.showMessageDialog(null,msg,title,type);
// }
}
项目源码