将SSH开发中所用的包导入到项目中,配置好struts.xml和application.xml,进行的第一个从页面到底层数据的查询工作,主要介绍下SSH框架中类和方法的调用过程。
一、MVC执行
1.页面action:
在用户名的input标签中添加onblur的方法
<span style="font-family:FangSong_GB2312;"><td> <input type="text" id="username" name="username" class="text" maxlength="20" onblur="checkUserName()" /> <span id="span1"></span> </td> </span>
页面Ajax代码:
<span style="font-family:FangSong_GB2312;">function checkUserName(){ alert("name"); var username = document.getElementById("username").value; //传统的ajax校验 //1.创建异步交互对象, var xhr = createXmlHttp(); alert(xhr); //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); } //创建XmlHttp对象 function createXmlHttp(){ var xmlHttp; try{ xmlHttp= new XMLHttpRequest(); }catch(e){ try{ xmlHttp=new ActiveXObject("Msxm12.XMLHTTP"); }catch(e){ try{ xmlHttp= new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){} } } return xmlHttp; } </span>
2、struts.xml
当焦点离开input标签时会触发onblur方法,执行user_findByName.action,该action首先通过struts.xml,调用对应的执行class="userAction"
<span style="font-family:FangSong_GB2312;"> <!-- 配置用户模块的action --> <action name="user_*" class="userAction" method="{1}"> <result name="registPage">/WEB-INF/jsp/regist.jsp</result> </action> </span>
3、application.xml
然后在application中找对应类class="userAction"的具体执行类。
<span style="font-family:FangSong_GB2312;"><bean id="userAction" class="cn.itcast.shop.user.action.UserAction" scope="prototype"> <!-- 注入service --> <property name="userService" ref="userService"/> </bean> </span>
在具体类cn.itcast.shop.user.action.UserAction中执行要操作的方法,用于操作后台数据和提供数据供前台页面显示。前面这些是mvc的执行过程。
二、后台调用
1、Aciton的具体执行方法
在findByName()中调用复合三层的概念,调用业务逻辑层,业务逻辑调用数据操作层。
<span style="font-family:FangSong_GB2312;">public String findByName() throws IOException{ //调用service来查询 User exisUser =userService.findByUsername(user.getUsername()); //获得response HttpServletResponse response= ServletActionContext.getResponse(); response.setContentType("text/html;charset=UTF-8"); if(exisUser != null){ //存在 response.getWriter().println("<font color='red'>用户已经存在</font>"); }else{ response.getWriter().println("<font color='green'>用户名可以使用</font>"); } return NONE; } </span>2、Service
业务逻辑层的方法
<span style="font-family:FangSong_GB2312;">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 { //用户名查询用户的方法 private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public User findByUsername(String username){ return userDao.findByUsername(username); } }</span>3、UserDao
操作数据库的方法
<span style="font-family:FangSong_GB2312;">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; } } </span>4、实体类和配置文件
<span style="font-family:FangSong_GB2312;">package cn.itcast.shop.user.vo; /** * 用户的实体类 * @author lll */ public class User { private Integer uid; private String username; . . . . . . } </span>4.2User.hbm.xml
<span style="font-family:FangSong_GB2312;"><?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> <property name="password"></property> <property name="name"></property> <property name="email"></property> <property name="phone"></property> <property name="addr"></property> <property name="state"></property> <property name="code"></property> </class> </hibernate-mapping> </span>4.3、application.xml中spring配置
<span style="font-family:FangSong_GB2312;"> <!-- 注册的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></span>