功能二:用easyUI做一个主页面
1.页面布局 layout
Lalyout :依懒panel,resizable。
布局容器有5个区域:北、南、东、西和中间。中间区域面板是必须的,边缘的面板都是可选的。每个边缘区域面板都可以通过拖拽其边框改变大小,也可以点击折叠按钮将面板折叠起来。布局可以进行嵌套,用户可以通过组合布局构建复杂的布局结构。
<div id="cc" class="easyui-layout" style="width:600px;height:400px;">
<div data-options="region:'north',title:'North Title',split:true" style="height:100px;"></div>
<div data-options="region:'south',title:'South Title',split:true" style="height:100px;"></div>
<div data-options="region:'east',iconCls:'icon-reload',title:'East',split:true" style="width:100px;"></div>
<div data-options="region:'west',title:'West',split:true" style="width:100px;"></div>
<div data-options="region:'center',title:'center title'" style="padding:5px;background:#eee;"></div>
</div>
如果要填满整个网页:直接在body中,添加layout
<body class="easyui-layout"></body>
split:true:可拖动边框。
我们在这里只是需要三个:
<body class="easyui-layout">
<div data-options="region:'north'" style="height:100px;background-image:url(/img/back2.jpg);" >
<div style="width: 800px;">
<marquee direction="right" behavior="alternate" scrollamount="15" scrolldelay="10" height="100" align="absmiddle">
<font face="隶书" size="9"> 智能销售系统</font>
</marquee>
</div>
</div>
<div data-options="region:'west',title:'菜单',split:true" style="width:200px;background-image:url(/img/back2.jpg);">
<ul id="tree"></ul>
</div>
<div id="table" class="easyui-tabs" data-options="region:'center'" >
<div title="主页面">
<p>我是主页面</p>
</div>
</div>
</body>
2.实现tree菜单
<ul id="menutree"></ul>
$("#menutree").tree({//添加菜单
method:"get",
url:'json/menuTree.json',
)}
3.点击菜单栏的菜单在中间添加一个panel
onClick:function(node){
//获取节点的名称,以及属性
var $text=node.text;
var $iconCls = node.iconCls;
$("#main").tabs('add',{//添加页面
title: $text,
iconCls:$iconCls,
content: '<div style="padding:10px">'+$text+'</div>',
href:'employee.html',
closable: true
})
}
4.点击主菜单不会显示
If(node.url):子菜单有url,主菜单没有
5.点击已经存在在主页面显示的菜单,不会再显示
//判断是否已经已经点击了
var tabspanel=$("#main").tabs("getTab",$text);
if(tabspanel){
//选中相应的面板
$("#mainUI").tabs("select",$text)}
<script> $(function () { $('#tree').tree({ url:'/json/menu.json', onClick: function(node){ //判断是否为子菜单 if(node.url){ var nodeName = node.text; var tab = $("#table").tabs("getTab",nodeName); //判断这个选项卡是否已经存在,存在就选中,不存在重新打开 if(tab){ $("#table").tabs("select",nodeName) }else { //嵌入一个单独的页面 var connect= '<iframe scrolling="auto" frameborder="0" src="'+node.url+'" style="width:100%;height:100%;"></iframe>'; //添加选项卡 $('#table').tabs('add',{ title:nodeName, content:connect, closable:true }); } } } }); }) </script>
功能三:实现用户管理界面
1.添加一个dataGrid显示所有数据
<table id="dataGrid" class="easyui-datagrid"
data-options="
url:'/employee/page',
fitColumns:true,
singleSelect:true,
fit:true,
toolbar:'#gao',
pagination:true">
<thead>
<tr>
<th data-options="field:'headImage',width:100,formatter:gethead" >头像</th>
<th data-options="field:'username',width:100">用户名</th>
<th data-options="field:'password',width:100">密码</th>
<th data-options="field:'email',width:100,align:'right'">邮箱</th>
<th data-options="field:'age',width:100,align:'right'">年龄</th>
<th data-options="field:'department',width:100,align:'right',formatter:getdepartment">部门</th>
</tr>
</thead>
</table>
1.显示头像
function gethead(value,row,index) {
return "<img width='60px' src='"+value+"' alt='没有头像'>"
}
2.部门下拉(下面有后台部门的代码)
部门:<input name="departmentId" class="easyui-combobox"
panelHeight="auto"
data-options="valueField:'id',textField:'name',url:'/util/dpartment'" />
function getdepartment(value,row,index) {
return value?value.name:"无部门"
}
3.高级查询
$(function () {
var datagrid = $('#dataGrid');
$("*[data-method]").on("click",function () {
var method=$(this).data("method");//拿到方法名字
itsource[method]();//根据方法名执行相应 的方法
})
window.itsource={
search:function () {
//获取from表单的值
var par=$("#searchform").serializeObject();
//刷新页面
datagrid.datagrid("load",par);
}
}
})
功能四:添加部门
Domain
@Table(name = "department")
public class Dpartment extends BaseDaomain {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "Dpartment{" +
"name='" + name + '\'' +
", id=" + id +
"} ";
}
}
与employee建立多对一的关联(在employee中)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="department_id")
//Json Ignore(忽略) Properties(属性)
//@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"})
private Dpartment department;
当设置了懒加载的时候会报两个错:
noSession:
原因:当我们在连表的时候配置了懒加载的时候,那么我们的在查询后就将session给关了
我们的department是懒加载,是需要的时候才去查询,但是我们的session已经关了,所以就会报一个nosession
解决:在web.xml中加上
<!-- 加上OpenEntityManager -->
<filter>
<filter-name>openEntity</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openEntity</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2.No serializer:这个问题在使用Springmvc+jpa中加懒加载一定会出现这个问题。
原因:因为SpringMVC会返回一些json数据,这些数据中有一些序列化要求。当没有懒加载对象时是没有问题的,现在使用jpa返回懒加载对象,它自动在里面加了一些属性来完成懒加载功能("hibernateLazyInitializer","handler","fieldHandler")。
解决办法一:麻烦,每一个关联都要写
在类上加属性:生成的时候把这个字段忽略了:
@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"})
解决办法二:一次配置
创建一个新的类(重写com.fasterxml.jackson.databind.ObjectMapper)
package cn.itsource.aisell.common;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class CustomMapper extends ObjectMapper {
public CustomMapper() {
this.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// 设置 SerializationFeature.FAIL_ON_EMPTY_BEANS 为 false
this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
}
}
在applicationContext-mvc.xml中配置
<!-- No serializer:配置 objectMapper 为我们自定义扩展后的 CustomMapper,解决了返回对象有关系对象的报错问题 -->
<property name="objectMapper">
<bean class="cn.itsource.aisell.common.CustomMapper"></bean>
</property>
2.Pository
package cn.itsource.aisell.repository;
import cn.itsource.aisell.domain.Dpartment;
public interface DepartmentRepository extends BaseRepository<Dpartment,Long> {
}
3.Service
父类接口
子类实现
package cn.itsource.aisell.service;
import cn.itsource.aisell.domain.Employee;
import cn.itsource.aisell.query.EmployeeQuery;
import cn.itsource.aisell.service.IBaseService;
public interface IEmployeeService extends IBaseService<Employee,Long>{
}
import cn.itsource.aisell.domain.Dpartment;
import cn.itsource.aisell.domain.Employee;
import cn.itsource.aisell.service.IDartmentService;
import cn.itsource.aisell.service.IEmployeeService;
import org.springframework.stereotype.Service;
@Service//service不能继承的
public class DpartmentServiceImpl
extends BaseServiceImpl<Dpartment,Long> implements IDartmentService {
}
扩展功能一:排序
添加属性:sortable=true;
添加了这个属性,每一次一点击在前台就会传回sort,order
后台拿到这个数据给orderName orderType
//排序(前台传回来的是sort,order)
public void setSort(String sort){
this.orderName=sort;
}
public void setOrder(String order){
this.orderType=order;
}
由于我们已经写好了排序,这里个这两个自赋值,就可以直接使用了。
隐藏字段
1.到http://www.easyui-extlib.com/的grid部分找到
2.找到需要的js,css
3.查看元素,找到我们需要的js,css
4.拷贝js,css
5.将拷贝的放到项目中去
6.引入
<script src="/js/model/employee.js"></script><%--外部映入要写全--%>
<script type="text/javascript" src="/easyui/plugin/datagrid/jeasyui.extensions.datagrid.getColumnInfo.js"></script>
<script type="text/javascript" src="/easyui/plugin/datagrid/jeasyui.extensions.menu.js"></script>
<script type="text/javascript" src="/easyui/plugin/datagrid/jeasyui.extensions.datagrid.css"></script>
<script type="text/javascript" src="/easyui/plugin/datagrid/jeasyui.extensions.datagrid.columnToggle.js"></script>
7.根据扩展属性配置属性