内容
1、用户注册模块
1、创建domain-user
2、写Hibernate.hbm配置文件
3、创建action
4、配置Struts.xml
5、创建service和dao
6、配置applicationConfig文件。(一定要记得,创建完xxxDao或者service时候,写bean标签)
前端代码
function checkID(){
var user_id = $("#user_code").val();
// 在if判断中不能写user_id==null 不知道是为什么
if( user_id.trim()==""){
//用户名为空,提示信息.名称不能有混淆,否则会发生不报错的错误
$("#user_id1").addClass("error");
$("#user_id1").html("用户名不能为空");
}else{
// 利用ajax查询数据库,是否存在同名的用户
var url="${pageContext.request.contextPath }/user_check.action";
var param={"user_code":user_id};
$.get(url,param,function(data){
if(data && data=="no"){
$("#user_id1").addClass("error");
$("#user_id1").html("名字已存在");
}else{
$("#user_id1").removeClass("error");
$("#user_id1").html("可以注册");
}
});
}
}
//可以阻止表单的提交
function formCheck(){
// 先让校验名称的方法先执行以下
checkID();
// 获取error的数量,如果数量 > 0,说明存在错误,不能提交表单
if($(".error").size() > 0){
return false;
}
}
后端代码
2、用户登录
2.1 利用模型驱动的方式进行获取前端传来的用户名和密码
2.2 调用service层进行业务处理
2.3 在dao层进行读取,传入用户名和密码,若正确返回user对象(list.get(0))
2.4 action层进行验证,设置session对象
2.5 及时的配置bean标签
2.6 通过result进行重定向到index页面或者login页面
2.7用户退出ServletActionContext.getRequest().getSession().remove(session)
3 数据字典表的引入
3.1 数据字典表的作用:规范开发中数据的写法
字段表与客户表是一对多的关系修改客户表,添加外键(使用SQLyog进行修改)
问题:没有形成自己的一套方法
class com.chuantao.domain.User not found while looking for property: user_id
发现重新写一遍user_id 以及setget方法 和相对应的配置文件就可以了。
3.2 配置映射文件(系统性的敲代码)
编写字典表的JavaBean和映射的配置文件
修改Customer的JavaBean,因为是多方,需要把外键字段换成字典对象
修改Customer.hbm.xml的配置文件,配置多对一
数据字典表属于一方
Customer属于多方
distBean
private String dict_id;
// 数据字典类别代码 01 06
private String dict_type_code;
// 类别名称 01所属于行业 06客户级别
private String dict_type_name;
// 字典项目名称
private String dict_item_name;
private String dict_item_code;
// 排序字段
private Integer dict_sort;
private String dict_enable;
private String dict_memo;
映射文件
<hibernate-mapping>
<class name="com.chuantao.domain.Dict" table="base_dict">
<id name="dict_id" column="dict_id">
<!-- 换成字符串生成的策略 -->
<generator class="uuid"/>
</id>
<property name="dict_type_code" column="dict_type_code"/>
<property name="dict_type_name" column="dict_type_name"/>
<property name="dict_item_name" column="dict_item_name"/>
<property name="dict_item_code" column="dict_item_code"/>
<property name="dict_sort" column="dict_sort"/>
<property name="dict_enable" column="dict_enable"/>
<property name="dict_memo" column="dict_memo"/>
</class>
</hibernate-mapping>
在Customer.hbm.xml中配置多对一的关联
<!-- 配置的多方 name是JavaBean对象类的属性名称 class="一方类的全路径" cloumn="外键关联对应的数据库中的字段名称" -->
<many-to-one name="source" class="com.chuantao.domain.Dict" column="cust_source"/>
<many-to-one name="industry" class="com.chuantao.domain.Dict" column="cust_industry"/>
<many-to-one name="level" class="com.chuantao.domain.Dict" column="cust_level"/>
同时在Customer中不应该在创建set和get集合。Customer实体类中的改变。同时Hibernate.hbm.xml去掉
private Long cust_id;
// 客户名称
private String cust_name;
private Long cust_user_id;
private Long cust_create_id;
/*// 客户的来源
private String cust_source;
// 所属于行业
private String cust_industry;
// 客户的级别
private String cust_level;*/
// 联系人名称
private String cust_linkman;
// 固定电话
private String cust_phone;
// 移动电话
private String cust_mobile;
// 描述的是 一客户的来源,多是客户
private Dict source;
// 一客户的行业 多是客户
private Dict industry;
// 一客户级别 多是客户
private Dict level;
4、分页查询
4.1 前端情况
在menu.jsp点击客户列表跳转到Customer_findByPage.action后端
后端处理后前端的关键代码
<SCRIPT language=javascript>
function to_page(page){
if(page){
$("#currentPage").val(page);
}
document.customerForm.submit();
}
</SCRIPT>
<DIV
style="LINE-HEIGHT: 20px; HEIGHT: 20px; TEXT-ALIGN: right">
共[<B>${page.totalCount}</B>]条记录,[<B>${page.totalPage}</B>]页
,每页显示
<select name="pageSize">
<option value="1" <c:if test="${page.pageSize==1 }">selected</c:if>>1</option>
<option value="2" <c:if test="${page.pageSize==2 }">selected</c:if>>2</option>
</select>
条
[<A href="javascript:to_page(${page.currentPage-1})">前一页</A>]
<B>${page.currentPage}</B>
[<A href="javascript:to_page(${page.currentPage+1})">后一页</A>]
到
<input type="text" size="3" id="currentPage" name="currentPage" />
页
<input type="button" value="Go" οnclick="to_page()"/>
</DIV>
<TBODY>
<TR
style="FONT-WEIGHT: bold; FONT-STYLE: normal; BACKGROUND-COLOR: #eeeeee; TEXT-DECORATION: none">
<TD>客户名称</TD>
<TD>客户级别</TD>
<TD>客户来源</TD>
<TD>联系人</TD>
<TD>电话</TD>
<TD>手机</TD>
<TD>操作</TD>
</TR>
<c:forEach items="${page.list }" var="customer">
<TR
style="FONT-WEIGHT: normal; FONT-STYLE: normal; BACKGROUND-COLOR: white; TEXT-DECORATION: none">
<TD>${customer.cust_name }</TD>
<TD>${customer.level.dict_item_name }</TD>
<TD>${customer.source.dict_item_name }</TD>
<TD>${customer.cust_linkman }</TD>
<TD>${customer.cust_phone }</TD>
<TD>${customer.cust_mobile }</TD>
<TD>
<a href="${pageContext.request.contextPath }/customerServlet?method=edit&custId=${customer.cust_id}">修改</a>
<a href="${pageContext.request.contextPath }/customerServlet?method=delete&custId=${customer.cust_id}">删除</a>
</TD>
</TR>
</c:forEach>
</TBODY>
4.2 后端处理结果为
Action中的进行属性驱动的方式获得传递的属性
// 设置默认页。这是什么写法 属性驱动的方式。
private Integer currentPage = 1;
public void setCurrentPage(Integer currentPage) {
if(currentPage == null){
currentPage = 1;
}
this.currentPage = currentPage;
}
// 每页显示的数据的条数
private Integer pageSize = 2;
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public String findByCurrentPage(){
//
// 调用service业务层
DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
// 查询
PageBean<Customer> page = customerService.findByCurrentPage(currentPage,pageSize,criteria);
// 压栈
ValueStack vs = ActionContext.getContext().getValueStack();
// 栈顶是map<"page",page对象>
vs.set("page", page);
return "page";
}
dao层处理
/**
* 分页查询的功能
* 1、查出总的数量
* 2、设置总的数量
* 3、根据当前页查出页面显示的客户,
* 4、将客户封装到list集合中返回
*/
@Override
public PageBean<Customer> findByCurrentPage(Integer currentPage, Integer pageSize, DetachedCriteria criteria) {
PageBean<Customer> pageBean = new PageBean<Customer>();
// 将页面传递的信息进行封装
pageBean.setCurrentPage(currentPage);
pageBean.setPageSize(pageSize);
// 查询出总的数据量,使用count()查询
criteria.setProjection(Projections.rowCount());
List<Number> list = (List<Number>) this.getHibernateTemplate().findByCriteria(criteria);
if(list != null && list.size() > 0){
int totalCount = list.get(0).intValue();
// 封装总的记录数
pageBean.setTotalCount(totalCount);
}
// 查询完后 需要清除count()
criteria.setProjection(null);
// 在传入当前页查询,个数
List<Customer> beanList = (List<Customer>) this.getHibernateTemplate().findByCriteria(criteria, (currentPage-1)*pageSize, pageSize);
// 将结果进行封装到list集合中
pageBean.setList(beanList);
return pageBean;
}