要求:
必備知識:JAVA/Struts2,JS/JQuery,HTML/CSS基礎語法;
開發環境:MyEclipse 10
關於UI部分請查看下列鏈接,有詳細制作步驟:
前段時間學校剛學完Struts2-Action篇,又自學了一點AJAX/JQuery,到網上看了一些CSS3知識。突然想要不要干脆做一個用戶注冊與登入功能。下面是JAVA部分的核心代碼, 如果這樣的邏輯和大家想的很有出入的話,歡迎拍磚劈斧,呵呵。
UserAction.java
package action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import dao.UserDao;
import entity.User;
public class UserAction extends ActionSupport {
private String contentType = "text/html;charset=utf-8";
private User user;
private LinkedList<User> users;
public LinkedList<User> getUsers() {
return users;
}
public void setUsers(LinkedList<User> users) {
this.users = users;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
/**
* 查詢用戶 登入驗證
* @return
* @throws IOException
*/
public void select() throws IOException{
//指定輸出內容類型和編碼
ServletActionContext.getResponse().setContentType(contentType);
//獲取輸出流,然后使用
PrintWriter out = null;
out = ServletActionContext.getResponse().getWriter();
this.user=new UserDao().select(user); //給this.user賦值
if(user==null){
out.print("登入失敗");
}else{
ActionContext actionContext=ActionContext.getContext();
actionContext.getSession().put("user",user);
actionContext.getSession().put("users",new UserDao().getList());
out.print("登入成功");
}
out.flush();
out.close();
}
/**
* 添加用戶控制器
* @throws Exception
*/
public void add() throws IOException{
//指定輸出內容類型和編碼
ServletActionContext.getResponse().setContentType(contentType);
//獲取輸出流,然后使用
PrintWriter out = null;
out = ServletActionContext.getResponse().getWriter();
int rs=new UserDao().add(this.user);
if(rs==1){
ActionContext actionContext=ActionContext.getContext();
actionContext.getSession().put("user",user);
actionContext.getSession().put("users",new UserDao().getList());
}
out.print(rs);
out.flush();
out.close();
//System.out.print(new UserDao().add(this.user)); 這里不能在用 System.out.print() 否則后台報錯
}
public String upd(){
return null;
}
public String del(){
return null;
}
/*@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return super.execute();
}*/
}
UserDao.java
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import tools.ConvertJson;
import tools.JDBCUtilSingle;
import entity.User;
public class UserDao {
/**
* 插入操作 注冊功能
* @param user 用戶實例 POJO
* @return 操作標記 1成功 2郵箱存在 3用戶名存在
*/
public int add(User user){
Connection connection=null;
PreparedStatement statement=null;
ResultSet rs=null;
connection=JDBCUtilSingle.getInitJDBCUtil().getConnection();
String sql="select * from form2_user where name=? or email=?";
try {
statement=connection.prepareStatement(sql);
statement.setString(1,user.getName());
statement.setString(2, user.getEmail());
rs=statement.executeQuery();
if(rs.next()){
if(rs.getString("email").equals(user.getEmail())){return 2;} //2郵箱存在
if(rs.getString("name").equals(user.getName())){return 3;} //3用戶名存在
}
sql="INSERT INTO form2_user (`id`, `email`, `name`, `pass`) VALUES (NULL,?,?,?)";
statement=connection.prepareStatement(sql);
statement.setString(1,user.getEmail());
statement.setString(2,user.getName() );
statement.setString(3, user.getPass());
statement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
JDBCUtilSingle.getInitJDBCUtil().closeConnection(rs, statement, connection);
}
return 1; //1表示成功注冊
}
/**
* 用戶登錄 放回登入用戶對象信息
* @param user 用戶對象
* @return
*/
public User select(User user){
Connection connection=null;
PreparedStatement statement=null;
ResultSet rs=null;
User myUser=null;
connection=JDBCUtilSingle.getInitJDBCUtil().getConnection();
String sql="select * from form2_user where (name=? or email=?) and pass=?";
try {
statement=connection.prepareStatement(sql);
statement.setString(1,user.getName());
statement.setString(2,user.getName());
statement.setString(3,user.getPass());
rs=statement.executeQuery();
if(rs.next()){
myUser=new User();
myUser.setName(rs.getString("name"));
myUser.setEmail(rs.getString("email"));
myUser.setPass(rs.getString("pass"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBCUtilSingle.getInitJDBCUtil().closeConnection(rs, statement, connection);
}
return myUser;
}
/**
* 獲取所有用戶信息
* @return 用戶集合
*/
public LinkedList<User> getList(){
Connection connection=null;
PreparedStatement statement=null;
ResultSet rs=null;
User myUser=null;
LinkedList<User> users=new LinkedList<User>();
connection=JDBCUtilSingle.getInitJDBCUtil().getConnection();
String sql="select * from form2_user";
try {
statement=connection.prepareStatement(sql);
rs=statement.executeQuery();
while(rs.next()){
myUser=new User(rs.getString("email"),rs.getString("name"), rs.getString("pass"));
users.add(myUser);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBCUtilSingle.getInitJDBCUtil().closeConnection(rs, statement, connection);
}
return users;
}
}
User.java
package entity;
public class User {
private String email;
private String name;
private String pass;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public User(){}
public User(String email,String name, String pass){
this.email=email;
this.name=name;
this.pass=pass;
}
}
呵呵,又結束了,不知到你們看懂了沒。請原諒童鞋我目前的表述能力只能到這了。歡迎大家來拍磚來劈斧,希望我幼小的心靈能抗得住。
作者: Li-Cheng




