一、JSP代码:
1、事件触发:onblur
2、编写AJAX代码:向Action中提交,传递username参数
<script> function checkUsername(){ //获得文本框的值 //function checkUsername(field){ //field.value; var username=document.getElementById("username").value; //判断是否输入用户名 if(username.length !=0){ //1.创建异步交互对象 var xhr=createXmlHttp(); //2.设置监听 xhr.onreadystatechange=function(){ if(xhr.readyState==4){ if(xhr.status==200){ document.getElementById("span1").innerHTML=xhr.responseText; } } } //3.打开连接 xhr.open("GET","${pageContext.request.contextPath}/user_findByName.action?time="+new Date().getTime()+"&username="+username,true); //4.发送 xhr.send(null); }else{ document.getElementById("span1").innerHTML=""; } } function createXmlHttp(){ var xmlHttp; try{//Firefox,opera,Safari xmlHttp=new XMLHttpRequest(); } catch(e){ try{//Internet explorer xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch(e){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){} } } return xmlHttp; } } <!-- 事件触发 --> <!-- <input type="text" id="username" name="username" class="text" maxlength="20" onblur="checkUsername(this);"/> --> <input type="text" id="username" name="username" class="text" maxlength="20" onblur="checkUsername();"/> <span id="span1"></span>
二、Java方法类Action代码:
接收username:模型驱动接收
package cn.itcast.shop.user.action; import java.io.IOException; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import cn.itcast.shop.user.service.UserService; import cn.itcast.shop.user.vo.User; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; /* * 用户模块Action的类 * 郭贤达 */ //接受username,模型驱动接收--extends ActionSupport implements ModelDriven<User> public class UserAction extends ActionSupport implements ModelDriven<User>{ //模型驱动要使用的对象 private User user=new User(); public User getModel() { return user; } //注入UserService private UserService userService; public void setUserService(UserService userService) { this.userService = userService; }/* * ajax进行异步校验用户名的执行方法 */ public String findByName() throws IOException{ //调用Service进行查询 User existUser=userService.findByUsername(user.getUsername()); //获得response对象,向页面输出: HttpServletResponse response=ServletActionContext.getResponse(); response.setContentType("text/html;charset=UTF-8"); //判断 if(existUser != null){ //查询到该用户:用户名已经存在 response.getWriter().println("<font color='red'>用户名已经存在</font>"); } else{ //没查到该用户:用户名可以使用 response.getWriter().println("<font color='green'>用户名可以使用</font>"); } return NONE; } }
三、Java实体类代码:
1、编写实体类
package cn.itcast.shop.user.vo; /* * 用户模块的实体类 */ public class User { private Integer uid; private String username; private String password;public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
2、编写映射
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.itcast.shop.user.vo.User" table="user"> <id name="uid"> <generator class="native"/> </id> <property name="username"/> <property name="password"/> <property name="name"/> </class> </hibernate-mapping>
四、Dao代码:
继承HibernateDaoSupport
package cn.itcast.shop.user.dao; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import cn.itcast.shop.user.vo.User; /* * 用户模块持久层的代码 */ public class UserDao extends HibernateDaoSupport { //按名次查询是否有该用户 public User findByUsername(String username){ String hql="from User where username=?"; List<User> list=this.getHibernateTemplate().find(hql,username); if(list !=null && list.size()>0){ return list.get(0); } return null; } }
五、Service代码:
package cn.itcast.shop.user.service; import org.springframework.transaction.annotation.Transactional; import cn.itcast.shop.user.dao.UserDao; import cn.itcast.shop.user.vo.User; /* * 用户模块业务层的代码 */ @Transactional public class UserService { //注入UserDao private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } //按用户名查询用户的方法 public User findByUsername(String username){ return userDao.findByUsername(username); } }
六、在spring中配置实体类、映射和注入:
<!-- 配置Hibernate的映射文件 --> <property name="mappingResources"> <list> <value>cn/itcast/shop/user/vo/User.hbm.xml</value> </list> </property> </bean> <!-- 事务管理: --> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 开启注解事务 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 首页访问的action --> <bean id="indexAction" class="cn.itcast.shop.index.action.IndexAction" scope="prototype"> </bean> <!-- 用户模块的action --> <bean id="userAction" class="cn.itcast.shop.user.action.UserAction" scope="prototype"> <!-- 注入service --> <property name="userService" ref="userService"/> </bean> <!-- Service配置 --> <bean id="userService" class="cn.itcast.shop.user.service.UserService"> <property name="userDao" ref="userDao"/> </bean> <!-- Dao配置 --> <bean id="userDao" class="cn.itcast.shop.user.dao.UserDao"> <property name="sessionFactory" ref="sessionFactory"/> </bean>
天地大矣,我心辽矣;恰同学少年,来日方长!