SSH2整合+分页+ajax+js校验用户名
1 action: FriendAction package com.ipanel.action; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.ipanel.entity.Friend; import com.ipanel.service.FriendService; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class FriendAction extends ActionSupport { private Friend friend; private FriendService friendService; private String phone; private String name; private String sex; public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Friend getFriend() { return friend; } public void setFriend(Friend friend) { this.friend = friend; } public FriendService getFriendService() { return friendService; } public void setFriendService(FriendService friendService) { this.friendService = friendService; } // 删除好友 public String delete() { friend = this.friendService.findByName(friend.getName()); this.friendService.delete(friend); return "deletefriend"; } // 添加好友 public String save() { System.out.println(name+";"+phone+";"+sex); if(name==null||"".equals(name.trim())){ this.addFieldError(getName(), "用户名不能为空"); }else{ friend = new Friend(); friend.setName(name); friend.setPhone(Integer.parseInt(phone)); friend.setSex(sex); this.friendService.save(friend); } return "save"; } // // 查询好友 // public String find(Friend friend){ // friend=this.friendService.findByName(name); // return "findfriend"; // } // // 修改好友 // public String update(Friend friend){ // friend=this.friendService.findByName(name); // friend.setName(name); // friend.setPhone(phone); // friend.setSex(sex); // this.friendService.save(friend); // return "save"; // } //验证用户名 public String checkUserName() throws IOException{ HttpServletResponse response = ServletActionContext.getResponse(); PrintWriter out = response.getWriter(); System.out.println("jsgfs"); friend=friendService.findByName(name); System.out.println(name); if(friend==null) { out.print(1); } else { out.print(0); } return null; } @Override public String execute() throws Exception { return "input"; } } PcookAction package com.ipanel.action; import java.io.UnsupportedEncodingException; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.ipanel.entity.Pcook; import com.ipanel.service.PcookService; import com.opensymphony.xwork2.ActionSupport; public class PcookAction extends ActionSupport { private Pcook pcook; private PcookService pcookService; private String name; private String percent; private List list; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPercent() { return percent; } public void setPercent(String percent) { this.percent = percent; } //public String checkFood() throws IOException{ // HttpServletResponse response = ServletActionContext.getResponse(); // PrintWriter out=response.getWriter(); // List pcook=new ArrayList(); // pcook=pcookService.findPcookByName(name); // if(pcook==null){ // out.print(0); // }else{ // out.print(1); // } // // return null; //} public String findAll()throws UnsupportedEncodingException{ HttpServletRequest request = ServletActionContext.getRequest(); String curpage = request.getParameter("curpage"); // 当前页 String pagesize = "10"; // 页大小 list = pcookService.queryByPage(curpage, pagesize, request); return "allfind"; } public String findPcookByName() { HttpServletRequest request = ServletActionContext.getRequest(); try { request.setCharacterEncoding("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } list=this.pcookService.findPcookByName(name); System.out.println(list); return "ByName"; } public Pcook getPcook() { return pcook; } public void setPcook(Pcook pcook) { this.pcook = pcook; } public PcookService getPcookService() { return pcookService; } public void setPcookService(PcookService pcookService) { this.pcookService = pcookService; } public List getList() { return list; } public void setList(List list) { this.list = list; } } dao package com.ipanel.dao; import java.util.List; import com.ipanel.entity.Friend; public interface FriendDao { public void saveFriend(Friend friend); public void deleteFriend(Friend friend); public Friend findFriendByName(String Name); public List findAllFriend(); public void updateFriend(Friend friend); } package com.ipanel.dao; import java.util.List; import com.ipanel.entity.Pcook; public interface PcookDao { public void savePcook(Pcook pcook); public void deletePcook(Pcook pcook); public List findPcookByName(String Name); public List findAllPcook(); public void updatePcook(Pcook pcook); /** * 分页查询 * @param curpage * @param pagesize * @return * @throws Exception */ public List queryByPage(int curpage, int pagesize); /** * 得到总记录数 * @return */ public int queryCount(); } dao.impl package com.ipanel.dao.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 com.ipanel.dao.FriendDao; import com.ipanel.entity.Friend; public class FriendDaoImpl extends HibernateDaoSupport implements FriendDao { public void deleteFriend(Friend friend) { this.getHibernateTemplate().delete(friend); } @SuppressWarnings("unchecked") public List findAllFriend() { String hql="from friend order by friend.friend_id desc"; List list=this.getHibernateTemplate().find(hql); return list; } @SuppressWarnings("unchecked") public Friend findFriendByName(final String name) { final String sql = "from Friend fre where fre.name=?"; List list = this.getHibernateTemplate().executeFind( new HibernateCallback() { public Object doInHibernate(Session s) throws HibernateException, SQLException { Query query = s.createQuery(sql); query.setString(0, name); List list = query.list(); return list; } }); if (null != list && !list.isEmpty()) { return list.get(0); } return null; } public void saveFriend(Friend friend) { this.getHibernateTemplate().save(friend); } public void updateFriend(Friend friend) { this.getHibernateTemplate().update(friend); } } package com.ipanel.dao.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 com.ipanel.dao.PcookDao; import com.ipanel.entity.Friend; import com.ipanel.entity.Pcook; public class PcookDaoImpl extends HibernateDaoSupport implements PcookDao { public void deletePcook(Pcook pcook) { // TODO Auto-generated method stub } public List findAllPcook() { final String sql="from Pcook"; List list=this.getHibernateTemplate().find(sql); return list; } @SuppressWarnings("unchecked") public List findPcookByName(String name) { String sql = "from Pcook fre where 1=1"; if (null != name && !"".equals(name)) { sql += " and fre.name like '%" + name + "%'" ; } final String hql = sql; List list = this.getHibernateTemplate().executeFind( new HibernateCallback() { public Object doInHibernate(Session s) throws HibernateException, SQLException { Query query = s.createQuery(hql); // query.setString(0,"%"+Name+"%"); List list = query.list(); return list; } }); return list; } public void savePcook(Pcook pcook) { // TODO Auto-generated method stub } public void updatePcook(Pcook pcook) { // TODO Auto-generated method stub } /** * 分页查询 * @param curpage * @param pagesize * @return * @throws Exception */ public List queryByPage(int curpage, int pagesize) { List list = null; try { String hql = "from Pcook"; Query query = this.getSession().createQuery(hql); query.setFirstResult((curpage - 1) * pagesize); query.setMaxResults(pagesize); list = query.list(); return list; } catch (Exception e) { e.printStackTrace(); } return list; } /** * 得到总记录数 * @return */ public int queryCount() { String hql = "from Pcook"; List list = this.getHibernateTemplate().find(hql); return list.size(); } } entity package com.ipanel.entity; import java.util.Date; public class Friend { private int id; private String name; private int age; private String sex; private int role_id; private double weight; private double beam; private double waistl; private double chestm; private Date birthday; private double high; private int phone; public Friend() { } public Friend(int id, String name, int age, String sex, int roleId, double weight, double beam, double waistl, double chestm, Date birthday, double high,int phone) { this.id = id; this.name = name; this.age = age; this.sex = sex; role_id = roleId; this.weight = weight; this.beam = beam; this.waistl = waistl; this.chestm = chestm; this.birthday = birthday; this.high = high; this.phone = phone; } public int getPhone() { return phone; } public void setPhone(int phone) { this.phone = phone; } public String getSex() { return sex; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String isSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getRole_id() { return role_id; } public void setRole_id(int roleId) { role_id = roleId; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public double getBeam() { return beam; } public void setBeam(double beam) { this.beam = beam; } public double getWaistl() { return waistl; } public void setWaistl(double waistl) { this.waistl = waistl; } public double getChestm() { return chestm; } public void setChestm(double chestm) { this.chestm = chestm; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public double getHigh() { return high; } public void setHigh(double high) { this.high = high; } @Override public String toString(){ return this.name+this.sex+this.phone; } } package com.ipanel.entity; public class Pcook { private int id; private String name; private String percent; private String picture_path; public Pcook() { } public Pcook(int id, String name, String percent,String picture_path) { this.id = id; this.name = name; this.percent = percent; this.picture_path = picture_path; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPercent() { return percent; } public void setPercent(String percent) { this.percent = percent; } public String getPicture_path() { return picture_path; } public void setPicture_path(String picturePath) { picture_path = picturePath; } } entity.xml service package com.ipanel.service; import java.util.List; import com.ipanel.entity.Friend; public interface FriendService { public void save(Friend friend); public void update(Friend friend); public void delete(Friend friend); public ListfindAll(); public Friend findByName(String name); } package com.ipanel.service; import java.util.List; import javax.servlet.http.HttpServletRequest; import com.ipanel.entity.Pcook; public interface PcookService { public void save(PcookService pcook); public void update(PcookService pcook); public void delete(PcookService pcook); public ListfindAll(); public List findPcookByName(String Name); /** * 分页查询 * @param curpage * @param pagesize * @return * @throws Exception */ public List queryByPage(String curpage, String pagesize, HttpServletRequest request); } service.impl package com.ipanel.service.imp; import java.util.List; import com.ipanel.dao.FriendDao; import com.ipanel.entity.Friend; import com.ipanel.service.FriendService; public class FriendServiceImpl implements FriendService { private FriendDao friendDao; public FriendDao getFriendDao() { return friendDao; } public void setFriendDao(FriendDao friendDao) { this.friendDao = friendDao; } public void delete(Friend friend) { this.friendDao.deleteFriend(friend); } public List findAll() { return this.friendDao.findAllFriend(); } public Friend findByName(String name) { return this.friendDao.findFriendByName(name); } public void save(Friend friend) { this.friendDao.saveFriend(friend); } public void update(Friend friend) { this.friendDao.updateFriend(friend); } } package com.ipanel.service.imp; import java.util.List; import javax.servlet.http.HttpServletRequest; import com.ipanel.dao.PcookDao; import com.ipanel.entity.Pcook; import com.ipanel.service.PcookService; public class PcookServiceImpl implements PcookService { private PcookDao pcookDao; public PcookDao getPcookDao() { return pcookDao; } public void setPcookDao(PcookDao pcookDao) { this.pcookDao = pcookDao; } public void delete(PcookService pcook) { // TODO Auto-generated method stub } public List findAll() { return this.pcookDao.findAllPcook(); } public List findPcookByName(String Name) { return this.pcookDao.findPcookByName(Name); } public void save(PcookService pcook) { // TODO Auto-generated method stub } public void update(PcookService pcook) { // TODO Auto-generated method stub } /** * 得到总记录数 * @return */ private int queryCount() { return this.pcookDao.queryCount(); } /** * 分页查询 * @param curpage * @param pagesize * @return * @throws Exception */ public List queryByPage(String curpage, String pagesize, HttpServletRequest request) { if("".equals(pagesize) || null == pagesize) { pagesize= "9"; } if("".equals(curpage) || null == curpage) { curpage= "1"; } int counts = this.queryCount(); int size = 9; int cur = 1; try { size = new Integer(pagesize); cur = new Integer(curpage); } catch (NumberFormatException e) { size=10; cur=1; } int pages = 1; if(counts > size) { if(counts % size == 0) { pages = counts / size; } else { pages = counts / size + 1; } } if(cur < 1) { cur = 1; } if(cur > pages) { cur = pages; } request.setAttribute("counts", counts); request.setAttribute("pages", pages); request.setAttribute("curpage", cur); request.setAttribute("pagesize", pagesize); return this.pcookDao.queryByPage(Integer.parseInt(curpage), Integer.parseInt(pagesize)); } } 配置文件 com/ipanel/entity/Friend.hbm.xml com/ipanel/entity/Pcook.hbm.xml hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.show_sql=true hibernate.format_sql=true struts.xml /page/friend.jsp /laowang/hehe.jsp /page/hehe.jsp /page/friend.jsp /page/friend.jsp /index.jsp /index.jsp /index.jsp web.xml contextConfigLocation classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener struts2 org.apache.struts2.dispatcher.FilterDispatcher struts2 /* OpenSessionInViewFilter org.springframework.orm.hibernate3.support.OpenSessionInViewFilter OpenSessionInViewFilter /* index.jsp page/jsp <%@ page language="java" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
姓名:
由4-16个英文字母、数字或下划线组成(不能以数字和下划线开头).
性别: 男 女
电话:
电话号码不合法!
cjsp/pcook.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %>
参考食谱

OK 搞定 适合新手学习 贴的累死了 不懂可以留言问我 数据库自己看着字段配置
居然是茁壮的同事
楼主能不能把你这个项目发给我啊。我想看看! yemengfan@holleygrid.com 我的邮箱! 谢谢