`

SSH2整合+分页+ajax+js校验用户名

    博客分类:
  • SSH
阅读更多
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<Pcook> 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<Pcook> getList() {
return list;
}
public void setList(List<Pcook> 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<Friend> 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<Pcook> findPcookByName(String Name);
public List<Pcook> 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<Friend> findAllFriend() {
String  hql="from friend order by friend.friend_id  desc";
List<Friend> 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<Friend> 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<Pcook> findAllPcook() {
final String sql="from Pcook";
List<Pcook> list=this.getHibernateTemplate().find(sql);
return list;
}

@SuppressWarnings("unchecked")
public List<Pcook> 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<Pcook> 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<Pcook> 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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="entity.Friend">
<class name="com.ipanel.entity.Friend" table="friend">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="15" />
</property>
<property name="age" type="java.lang.Integer">
<column name="age" length="20" />
</property>
<property name="sex" type="java.lang.String">
<column name="sex" length="15"/>
</property>
<property name="role_id" type="java.lang.Integer">
<column name="role_id" length="20" />
</property>
<property name="weight" type="java.lang.Double">
<column name="weight" length="20" />
</property>
<property name="beam" type="java.lang.Double">
<column name="beam" length="20" />
</property>
<property name="waistl" type="java.lang.Double">
<column name="waistl" length="20" />
</property>
<property name="chestm" type="java.lang.Double">
<column name="chestm" length="20" />
</property>
<property name="birthday" type="java.sql.Date">
<column name="birthday" length="20" />
</property>
<property name="high" type="java.lang.Double">
<column name="high" length="20" />
</property>
<property name="phone" type="java.lang.Integer">
<column name="phone" length="255" />
</property>
</class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="entity.Friend">
<class name="com.ipanel.entity.Pcook" table="pcook">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="45" />
</property>
<property name="percent" type="java.lang.String">
<column name="percent" length="75" />
</property>
<property name="picture_path" type="java.lang.String">
<column name="picture_path" length="75" />
</property>
</class>
</hibernate-mapping>

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 List<Friend>findAll();
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 List<Pcook>findAll();
public List<Pcook> 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<Friend> 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<Pcook> findAll() {

return this.pcookDao.findAllPcook();
}

public List<Pcook> 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));
}


}

配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/health" />
<property name="username" value="root" />
<property name="password" value="ipanel" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value> com/ipanel/entity/Friend.hbm.xml</value>
<value> com/ipanel/entity/Pcook.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
</value>
</property>
</bean>

<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 个性配餐 -->
<bean id="pcookAction" class="com.ipanel.action.PcookAction">
<property name="pcookService" ref="pcookService"></property>
</bean>
     <bean id="pcookDao" class="com.ipanel.dao.impl.PcookDaoImpl">
     <property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
     </bean>
     <bean id="pcookService" class="com.ipanel.service.imp.PcookServiceImpl">
     <property name="pcookDao" ref="pcookDao"></property>
     </bean>
   
<!-- 好友添加 -->
      <bean id="friendAction" class="com.ipanel.action.FriendAction">
<property name="friendService" ref="friendService"></property>
</bean>
<bean id="friendDao" class="com.ipanel.dao.impl.FriendDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>

</bean>
<bean id="friendService" class="com.ipanel.service.imp.FriendServiceImpl">
<property name="friendDao" ref="friendDao"></property>
</bean>
</beans>

struts.xml
<?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>
<!-- 好友添加 -->
<package name="friendAction" extends="struts-default" >
<action name="friendaction" class="com.ipanel.action.FriendAction">
<result name="save">/page/friend.jsp</result>
<result name="input">/laowang/hehe.jsp</result>
<result name="findfriend">/page/hehe.jsp</result>
<result name="checkFail">/page/friend.jsp</result>
<result name="checkSuccess">/page/friend.jsp</result>
</action>
</package>
<!-- 个性配餐  -->
<package name="pcookAction" extends="struts-default">
<action name="pcookAction" class="com.ipanel.action.PcookAction">
<result name="allfind" >/index.jsp</result>
<result name="checkFail">/index.jsp</result>
<result name="checkSuccess">/index.jsp</result>
</action>

</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>


<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<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>/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

page/jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<script type="text/javascript">

//声明XMLHttpRequest对象
     var xmlHttp;
     //检测用户名是否存在
    function CheckName()
    {
    createXMLHTTP();//创建XMLHttpRequest对象
    var name=document.getElementById("name");
    var name = name.value;
    var url = "<%=path%>/friend/friendaction!checkUserName?name=" + name;
    xmlHttp.open("GET",url,true);
    xmlHttp.onreadystatechange=checkUserName;//修改下面的回调函数,回调函数主要处理后台返回的数据,回调函数的参数就是后台返回的数据。
    xmlHttp.send(null);
    }
     //生成xmlhttp对象
       function createXMLHTTP()
       {
         if(window.XMLHttpRequest)
           {
                     xmlHttp=new XMLHttpRequest();//mozilla浏览器
                 }
                       else if(window.ActiveXObject)
                 {
                  try
                   {
                     xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");//IE老版本
                    }
                       catch(e)
                       {}
                        try
                    {
                         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");//IE新版本
                       }
                        catch(e)
                           {}
                             if(!xmlHttp)
                                     {
                                           window.alert("不能创建XMLHttpRequest对象实例!");
                                      return false;
                                     }
                                 }
                           }

//执行检测用户名回调函数
function checkUserName()
{
    if(xmlHttp.readyState==4)//判断对象状态
    {
         if(xmlHttp.status==200)
         {
             var repResult = xmlHttp.responseText;  
             if ("0" == repResult)
             {
                document.getElementById("checkUser1111").innerHTML = "你的名字重复了。。。。";
              }
         }
    }
}
function dosave() {
var reg = /^[A-Za-z_]+[0-9]*$/;
name = document.getElementById("name").value;
if (!reg.test(name)||name.length<1||name.length>20) {
document.getElementById("checkUserDiv").style.display = "block";
return false;
} else {
document.getElementById("checkUserDiv").style.display = "none";
}
var phone = document.getElementById("phone");
if (phone == "" && phone == null) {
return false;
}
if (phone!= "") {
var phoneValue =phone.value;
alert(phoneValue);
var p1 = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;
var reg0 = /^13\d{5,9}$/;
var reg1 = /^153\d{4,8}$/;
var reg2 = /^159\d{4,8}$/;
var reg3 = /^0\d{10,11}$/;
var me = false;
if (p1.test(phoneValue)||reg0.test(phoneValue)||reg1.test(phoneValue)||reg2.test(phoneValue)||reg3.test(phoneValue))
me = true;
if (!me) {
phone.value = '';
document.getElementById("checkPhoneDiv").style.display = "block";
phone.focus();
return false;
}
else
{

document.getElementById("checkPhoneDiv").style.display = "none";

}
}
document.getElementById("regForm").submit();
alert(document.getElementById("regForm"))
         }

</script>
<style type="text/css">
.checkUser {
font-size: 20px;
color: red;
font-family: "华文行楷";
text-algin: center;
}
</style>
  <style type ="text/css">
  .checkPhone {
font-size: 20px;
color: red;
font-family: "华文行楷";
text-algin: center;
}
</style>

</head>
<body>
<form id="regForm" name="friendAction" method="post" action="friend/friendaction!save">
<table>
<tr>
<td>
姓名:
</td>
<td>
<input id="name" type="text" name="name" onblur="CheckName()"/>
</td>
<td>

<div id="checkUser1111" class="checkUser"><span  >
</span></div>

<div id="checkUserDiv"  style="display: none">
<span id="checkUser" class="checkUser" >
由4-16个英文字母、数字或下划线组成(不能以数字和下划线开头). </span>
</div>
</td>

</tr>
<tr>
<th>
性别:
</th>
<td>
<label>
<input name="sex" type="radio" id="sex_0" value="男"
checked="checked" />

</label>
<label>
<input type="radio" name="sex" value="女" id="sex_1" />

</label>
<br />

</td>
</tr>

<tr>
<td>
电话:
</td>
<td>
<input id="phone" type="text" name="phone" id="phone" />

</td>
<td>
<div id="checkPhoneDiv"  style="display: none">
<span id="checkPhone" class="checkPhone" >
电话号码不合法! </span>
</div>
</td>
</tr>
<tr>
<td colspan="3" align="left">
<input type="button" value="保存" onclick="dosave()" />
<input type="button" value="删除"/>
<input type="reset" value="重置" />
</td>
</tr>

</table>
</form>
</body>
</html>

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 + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript" language="javascript">
      

</script>
</head>
<body>
<form id="findfood" name="pcookAction"
action="pcookAction/pcookAction!findAll" method="post">
<table>
<tr>
<td>
<a> 参考食谱 </a>

</td>
</tr>
<tr align="left">
<td></td>
<td align="left">
<input type="text" name="name" />
<input type="submit" value="查询" />
</td>
</tr>
<tr>
<td>
<a> <img src="../img/personCook_pic2.png" /> </a>
</td>
<td>
<img src="../img/personCook_pic2.png" />
</td>
<td>
<img src="../img/personCook_pic2.png" />
</td>
</tr>
<tr>
<td>
<img src="../img/personCook_pic2.png" />
</td>
<td>
<img src="../img/personCook_pic2.png" />
</td>
<td>
<img src="../img/personCook_pic2.png" />
</td>
</tr>
<tr>
<td>
<a><img src="../img/personCook_pic2.png">
</a>
</td>
<td>
<img src="../img/personCook_pic2.png" />
</td>
<td>
<img src="../img/personCook_pic2.png" />
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td colspan="3" align="left">
   <input type="button" value="添加" />
   <input type="button" value="完成" />
</td>
</tr>
</table>
</form>
</body>
</html>
OK 搞定 适合新手学习 贴的累死了  不懂可以留言问我  数据库自己看着字段配置
  • hth.rar (423.7 KB)
  • 下载次数: 97
分享到:
评论
2 楼 ipanel420 2011-11-22  
居然是茁壮的同事
1 楼 yemengfan 2011-11-18  
楼主能不能把你这个项目发给我啊。我想看看!  yemengfan@holleygrid.com   我的邮箱! 谢谢

相关推荐

    ssh整合系统分页ajax

    5. 示例中的"qimo_JavaWeb"可能是一个包含示例代码的文件或项目,它可能包含了SSH整合的配置文件、Java源代码、JSP视图文件以及相关的分页、校验和Ajax实现。对于初学者来说,研究这个示例可以帮助理解如何在实际...

    SSH三大框架整合+ajax应用+jquery+json应用+分页+增删改查

    SSH三大框架整合指的是Spring、Struts2和Hibernate这三个在Java Web开发中广泛应用的开源框架的集成。这三者结合在一起,可以构建出高效、模块化的Web应用程序。 **Spring框架** 是一个全面的后端开发框架,它提供...

    ssh.zip_SSH分页_ssh的分页实现

    在这个名为"ssh.zip_SSH分页_ssh的分页实现"的压缩包中,我们关注的是SSH框架下的数据分页功能。 分页是大型Web应用程序中的一个关键特性,它允许用户以可管理的方式浏览大量数据,而不会被过长的列表压垮。在SSH...

    ssh框架的分页功能

    2. 分页参数校验:对用户输入的页码和每页大小进行校验,防止非法值导致的错误。 3. 异步分页:对于大数据量的分页,可以采用Ajax异步加载,提高用户体验。 六、SSHByPageDemo SSHByPageDemo是一个示例项目,它展示...

    ssh2框架带有分页tomcat

    总的来说,"ssh2框架带有分页tomcat"意味着在Tomcat服务器上,通过SSH2框架的集成,开发者可以方便地实现数据库的分页查询,将分页逻辑分散到Spring的事务管理、Struts2的Action控制和Hibernate的数据库操作中。...

    Struts2+Spring3+Hibernate3 用户管理系统实例源码

    本例主要是实现了struts2+spring3+hibernate3的 基本框架搭建的注册登录,以及用户增删改查,适于初学者学习。 包括:注册 登录功能 分页的实现 前端校验 验证码的实现 注册时有ajax 校验,登录时 后台从数据库...

    史上最牛的java分页技术

    在本案例中,我们将探讨一种“史上最牛”的Java分页技术,它结合了SSH(Struts、Hibernate、Spring)框架和Ajax技术。 首先,我们要明确分页的基本概念。分页是将数据库中的大量数据分割成小块,每次只加载一部分到...

    java 分页组件

    在SSH(Struts2、Hibernate、Spring)框架中实现分页,通常涉及以下几个关键知识点: 1. **Struts2分页插件**:Struts2提供了如DisplayTag或Paginator这样的分页插件,它们能够帮助我们在JSP页面上轻松地实现分页。...

    SSH实现基本的增删改

    SSH(Struts2 + Spring + Hibernate)是一个经典的Java Web开发框架,用于构建高效、可扩展的企业级应用程序。在这个场景中,我们将深入探讨SSH框架如何实现基本的增删改操作,并结合页面美化和数据校验来提升用户...

    SSH & Ajax & MD5技术资料

    ajax校验、FCK文本编辑器、jfreechart、jxl导入、log4j、md5密码加密、poi报表、session失效自动跳转到登录页面、vilidate验证、二级缓存、分页、进度条、权限配置文件(dom4j)、验证码+记住我

    基于SSH在线教务选课平台

    【SSH在线教务选课平台】是一个以Spring、Struts和Hibernate为核心技术构建的Web应用程序,旨在提供一个高效、便捷的在线选课环境。SSH框架是Java Web开发中常用的一个集成框架,它由Spring负责控制层,Struts处理视...

    SSH框架的留言板及回复功能

    SSH框架,全称为Struts2、Spring和Hibernate的组合,是Java Web开发中常见的三大开源框架集成。这个框架集合提供了模型-视图-控制器(MVC)架构模式,使得开发者可以更高效地进行业务逻辑处理和数据管理。在这个...

    JAVA WEB典型模块与项目实战大全

    第23章 在线音乐管理系统(ajax+jsp+struts 2.x)  23.1 在线音乐管理系统简述  23.2 在线音乐管理系统前期准备  23.3 在线音乐管理系统具体实现——超级管理员操作  23.4 在线音乐管理系统具体实现——注册...

    java技术点

    7. **SSH/SSI实时预览**:Struts2,Spring,Hibernate整合。 8. **数据库分页显示**:Criteria API,HQL,SQL。 9. **Spring对象事件**:ApplicationEvent,ApplicationListener。 10. **事务管理**:Spring的@...

    python入门到高级全栈工程师培训 第3期 附课件代码

    11 FTP之创建文件夹及MD5校验思路 第33章 01 操作系统历史 02 进程的概念 03 线程的概念 04 线程的调用以及join方法 05 setDaemon方法和继承式调用.baiduyun.downloading 05 setDaemon方法和继承式调用 第34章 ...

    java面试宝典

    170、Javascript如何发送一个Ajax请求? 41 171、AJAX都有哪些有点和缺点? 41 172、Ajax和javascript的区别? 41 Servlet部分 42 174、JAVA SERVLET API中forward() 与redirect()的区别? 42 178、如何现实servlet...

    java面试题大全(2012版)

    4、请写出用于校验HTML文本框中输入的内容全部为数字的javascript代码 84 5、说说你用过那些ajax技术和框架,说说它们的区别 85 四. Java web部分 85 1、Tomcat的优化经验 85 2、HTTP请求的GET与POST方式的区别 85 3...

Global site tag (gtag.js) - Google Analytics