初级登录 到过程

1. denglu.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 XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户登陆</title>
<link href="<%=basePath %>style/main.css" rel="stylesheet" type="text/css" />

<link href="<%=basePath%>style/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<%=basePath %>js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="<%=basePath%>js/jquery-ui.js"></script>
<style type="text/css">
<!--
.STYLE1 {font-size: 12px}
-->
</style>
<script type="text/javascript">
function login(){

    var studentName1=document.getElementById("studentName").value;
    var studentPassword1 = document.getElementById("studentPasswod").value;
    if(studentName1==""){
    
       document.getElementById("yonghus").innerHTML="<font color='red'>请输入用户名!</font>";
       return ;
    }
    if(studentPassword1==""){
       document.getElementById("mimas").innerHTML="<font color='red'>请输入密码!</font>";
       return ;
    }

     $.ajax({
          type: "post",
          url: "<%=basePath%>background/student!findByNamePassword",
          data: 'studentName='+studentName1+'&studentPassword='+studentPassword1,
      success: function(msg){
       if(msg=="true"){
            document.getElementById("loginForm").submit();
       }else{
        document.getElementById("tishi").innerHTML="<font color='red'>用户名或者密码错误。请重新输入</font>";
       }
    }
    });
}
function yichu(){
       document.getElementById("yonghus").innerHTML="";
}
  function yichu1(){
        document.getElementById("mimas").innerHTML="";
  }
 
  //注册
function zhuce(){
  $("#addStu").dialog({title: '注册学生信息'});
    return false;
}
//验证 注册信息
function checkAddStu(){
  if($("#zstudentName").val()==""){
      $("#zname").html("<font color='red'>请输入用户!</font>");
      return;
  }
   if($("#zstudentPassword").val()==""){
      $("#ztpassword").html("<font color='red'>请输入密码!</font>");
      return;
  }
  if($("#zstudentPassword1").val()==""){
      $("#ztpassword1").html("<font color='red'>请再次输入密码!</font>");
      return;
  }
   if($("#zstudentPassword").val()!=$("#zstudentPassword1").val()){
      $("#ztpasswordx").html("<font color='red'>两次输入密码不一致!</font>");
      return;
  }
   if($("#zsex").val()==""){
      $("#ztsex").html("<font color='red'>请输入性别!</font>");
      return;
  }
   if($("#zbanji").val()==""){
      $("#ztbanji").html("<font color='red'>请输入班级!</font>");
      return;
  }

     $.ajax({
     type: "post",
     url: "<%=basePath %>background/student!findByName",
         data: 'studentName='+$("#zstudentName").val(),
     success: function(msg){
        if(msg==0){
         $("#addStuForm").submit();
        }else{
            $("#zname").html("<font color='red'>该用户已经存在!</font>");
        }
     }
  });


}

//失去焦点的时候去掉末尾提示信息
function  losname(){
     $("#zname").html("");
}
function  lospassword(){
     $("#ztpassword").html("");
}
function  lospassword1(){
     $("#ztpassword1").html("");
}
function  losbanji(){
     $("#ztbanji").html("");
}
function  losname(){
     $("#zname").html("");
}
function loaslianci(){
    $("#ztpasswordx").html("");
}


</script>

</head>
<body>

   <!-- 注册学生信息 -->
     <div id="addStu" style="display:none">
        <form action="<%=basePath%>background/student!saveAll" id="addStuForm" method="post" >
           用&nbsp;&nbsp;户&nbsp;&nbsp;名:
           <input type="text" name="zstudentName" id="zstudentName" onblur="javacript:losname();"/>   <span id="zname"></span>
           <br />
           <br />
          密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码:
           <input type="password" name="zstudentPassword" id="zstudentPassword" onblur="javacript:lospassword();" onclick="javacript:loaslianci();"/> <span id="ztpassword"></span>
           <br />
           <br />
         确认密码:
           <input type="password" name="zstudentPassword1" id="zstudentPassword1" onblur="javacript:lospassword1();" onclick="javacript:loaslianci();"/>  <span id="ztpassword1"></span>
           <br />
              <span id="ztpasswordx"></span>
           <br />
        性&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;别:
           <input type="radio" name="zsex" id="zsex" value="男" checked="checked"/>男
           <input type="radio" name="zsex" id="zsex" value="女" />女
           <br />
           <br />
        班&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;级:
           <input type="text" name="zbanji" id="zbanji" onblur="javacript:losbanji();"/> <span id="ztbanji"></span>
           <br />
           <br />
            <div align="center">
<a href="javascript:checkAddStu();">
<img alt="" src="<%=basePath%>image/queren.gif" border="0" />
</a>
<a href="" onclick="javascript:go: back(-1);">
<img alt="" src="<%=basePath%>image/fanhui.gif" border="0"  />
</a>
</div>
        </form>
     </div>
    
     <!-- 结束注册学生信息 -->
    <div align="center">
       <form action="<%=basePath%>background/student!findAll" id="loginForm" method="post">
         <table>
            <tr><td><span id="tishi"></span></td>></tr>
            <br />
            <br />
            <br />
            <br />
            <br />
           <tr>
                <td>用户姓名:<input type="text" id="studentName" name="studentName" onblur="javascript:yichu();"/><span id="yonghus"></span></td>
           </tr>
              <br />
              <br />
           <tr>
                <td>用户密码:<input type="password" id="studentPasswod" name="studentPasswod" onblur="javascript:yichu1();"/><span id="mimas"></span>  </td>
            </tr>
            <br />
            <br />
            <tr>
               <td> <input type="button" value="确认" onclick="javascript: login();" />  <input type="button" value="注册" onclick="javascript: zhuce();" />    </td>
            </tr>
          </table>
       </form>
    </div>
</body>
</html>




2.action 中的


package cn.com.kxrj.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

import cn.com.kxrj.manager.StudentManager;
import cn.com.kxrj.tool.Pagination;

public class StudentAction extends ActionSupport implements RequestAware,SessionAware{
/**
*
*/
private static final long serialVersionUID = 1L;
private StudentManager  studentManager;
private Map<String,Object > request;
private Map<String,Object > session;
private PrintWriter  out;


public String findAll(){
HttpServletRequest request= ServletActionContext.getRequest();
    String currPage=request.getParameter("currPage");
    if(currPage==null||currPage.equals("")){
    currPage=String.valueOf("1");
    }
    Pagination pagination =studentManager.findAll(Integer.valueOf(currPage));
    request.setAttribute("data", pagination.getList());
    request.setAttribute("currPage", pagination.getCurrPage());
    request.setAttribute("maxPage", pagination.getMaxPage());
    request.setAttribute("maxRow", pagination.getMaxRow());
    request.setAttribute("pageSize", pagination.getPageSize());
return "student_list";
}


public String deleteById(){
HttpServletRequest request = ServletActionContext.getRequest();
String studentId = request.getParameter("studentId");
System.out.println("学生ID   "+studentId);
studentManager.deleteById(Integer.valueOf(studentId));
return this.findAll();

}


public String saveAll(){
HttpServletRequest request = ServletActionContext.getRequest();
String studentName =request.getParameter("zstudentName");
String sex = request.getParameter("zsex");
String banji  = request.getParameter("zbanji");
String studentPassword = request.getParameter("zstudentPassword");
System.out.println("password :   "+studentPassword);
studentManager.saveAll(studentName,studentPassword, sex, banji);
return "success";

}
public void  findByName(){
int counts=0;
try {
HttpServletRequest request  = ServletActionContext.getRequest();
String  studentName = request.getParameter("studentName");
counts =studentManager.findByName(studentName,counts);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
out = response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("count :  数值 等于 :  "+ counts);
out.print(counts);
}


//验证登陆
public void findByNamePassword() {
try {
HttpServletRequest request  = ServletActionContext.getRequest();
String  studentName = request.getParameter("studentName");
String studentPassword = request.getParameter("studentPassword");
boolean flag = studentManager.findByNamePassword(studentName, studentPassword);
System.out.println("action flag =  "+flag);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
out =response.getWriter();
out.print(flag);

} catch (IOException e) {
e.printStackTrace();
}
}

public void setRequest(Map<String, Object> request) {
this.request = request;
}


public void setSession(Map<String, Object> session) {
this.session = session;
}


public void setStudentManager(StudentManager studentManager) {
this.studentManager = studentManager;
}




}




3.manager中的

package cn.com.kxrj.manager.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cn.com.kxrj.dao.StudentDAO;
import cn.com.kxrj.manager.StudentManager;
import cn.com.kxrj.model.Student;
import cn.com.kxrj.tool.Pagination;

public class StudentManagerImpl implements StudentManager {

private StudentDAO studentDAO;

public void setStudentDAO(StudentDAO studentDAO) {
this.studentDAO = studentDAO;
}

//删除数据
public void deleteById(int id) {
Student model=studentDAO.findById(id);
studentDAO.delete(model);

}
  
//查询所有的数据
public Pagination findAll(int currPage) {
    List list  = new ArrayList();
Pagination pagination = new Pagination();
pagination.setCurrPage(currPage);
pagination.setPageSize(PAGESIZE);
/*********动态构造SQL***********/
StringBuffer sql= new StringBuffer(" from Student where 1=1 ");
List<Object> objval = new ArrayList<Object>();

Iterator it = objval.iterator();
Object[] values= new Object[objval.size()];
int i=0;
while(it.hasNext()){
/* Object val = it.next();
values[i]=val;*/
values[i]=it.next();
i++;
}
String hql = sql.toString();
System.out.println("hql  :  "+hql);
int offset = (pagination.getCurrPage()-1)*PAGESIZE;
list = studentDAO.findByPage(hql, values, offset, PAGESIZE);
pagination.setList(list);
int maxRow = studentDAO.maxRow(hql, values).size();
pagination.setMaxRow(maxRow);
return pagination;
}


public Student findByI(int id) {
// TODO Auto-generated method stub
return null;
}

public int findByName(String studentName, int counts) {
List list= studentDAO.findByName(studentName);
   if(list.size()>0){
   counts=1;
   }
return counts;
}

public boolean findByNamePassword(String studentName, String password) {
boolean flag = false;
List list =  studentDAO.findByNamePassword(studentName, password);
System.out.println("manager list    "+list.size());
if(list.size()>0){
flag=true;
}
System.out.println("manager flag    "+flag);
return flag;
}

public void saveAll(String studentName, String password, String sex,
String banji) {
studentDAO.insert(new Student(studentName,password,sex,banji));
}

}




4。 dao 中的 :
1.

package cn.com.kxrj.tool.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


import cn.com.kxrj.tool.BaseDAO;

public class BaseDAOImpl extends HibernateDaoSupport implements BaseDAO {

/**
    * 使用hql语句进行分页查询
    * @param hql 需要查询的hql语句
    * @param values 如果hql有多个个参数需要传入,values就是传入hql的参数数组
    * @param offset 第一条记录索引
    * @param pageSize 每页需要显示的记录数
    * @return 当前页的所有记录
*/
public List findByPage(final String hql,final Object[] values,final int offset,final int pageSize) {
List list = this.getHibernateTemplate().executeFind(new HibernateCallback(){

public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
   for(int i=0; i<values.length;i++){
   query.setParameter(i, values[i]);
   }
   List result = query.setFirstResult(offset).setMaxResults(pageSize).list();
return result;
}

});

return list;
}

    /**
    * 使用hql语句进行分页查询
    * @param hql 需要查询的hql语句
    * @param values 如果hql有多个个参数需要传入,values就是传入hql的参数数组
    * @param offset 第一条记录索引
    * @param pageSize 每页需要显示的记录数
    * @return 当前页的所有记录
    */   
public List maxRow(final String hql,final Object[] values) {
List list = this.getHibernateTemplate().executeFind(new HibernateCallback(){

public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);   //查询数据
for(int i=0;i<values.length;i++){
query.setParameter(i, values[i]);
}
List result= query.list();
return result;
}

});
return list;
}




}



2.package cn.com.kxrj.dao.impl;

import java.util.List;

import cn.com.kxrj.dao.StudentDAO;
import cn.com.kxrj.model.Student;
import cn.com.kxrj.tool.impl.BaseDAOImpl;

public class StudentDAOImpl extends BaseDAOImpl implements StudentDAO {

//删除学生信息
public void delete(Student model) {
this.getHibernateTemplate().delete(model);
}

//根据id 查找一条数据
public Student findById(int id) {
return (Student) this.getHibernateTemplate().get(Student.class, id);
}

//依照姓名查找
public List<Student> findByName(String studentName) {
System.out.println("学生查找dao 名字    "  +studentName);
List<Student> list =this.getHibernateTemplate().find("from Student where stuname=? ", studentName);
System.out.println("dao list   "+list.size());
return list;
}

//验证
public List<Student> findByNamePassword(String studentName, String studentPassword) {

return this.getHibernateTemplate().find("from Student where stuname=? and stupassword=?", new Object[]{studentName,studentPassword});
}

//保存所有的信息
public void insert(Student model) {
this.getHibernateTemplate().save(model);
}


}


你可能感兴趣的:(DAO,jquery,Hibernate,Ajax,css)