Keep Calm and Carry On

ssh_day01

一、BOS项目概述

BOS(Business Operating System),业务操作系统,这个项目的是一个企业资源计划平台(ERP)。

1. 开发环境

开发环境 产品环境
操作系统 Windows Linux/centos
开发工具 eclipse、IDEA
数据库 MySQL MySQL
Web容器 Tomcat7.0 Tomcat7.0
浏览器 FireFox(firebug)/GOOGLE Chrome/QQBrowser

2. 技术选型

编号 工具 版本 说明
1 Struts 2 2.3.15 表现层MVC框架
2 Hibernate 3.6.10 数据层持久化框架
3 Spring 3.2.0 业务管理Ioc和AOP框架
4 Junit 4 单元测试
5 jQuery 1.8.3 JS框架
6 jQuery Easy UI 1.3.2 JS前端UI框架
7 Ztree 3.5 JS树型菜单插件
8 POI 3.9 Office文档读写组件
9 Hessian 4.0.33 RMI远程调用
10 Apache Shiro 1.2.2 权限管理框架
11 Activiti 5.13 工作流框架

二、搭建开发环境

1. 创建数据库环境

  1. 使用root用户登录MySQL数据库,创建一个数据库;
1
create database bos;
  1. 为本项目创建一个数据库用户
1
create user zhuobo identified by ‘asdf’;
  1. 为新创建的用户授权访问bos数据库
1
grant all on bos.* to zhuobo;
  1. 使用新的数据库用户登录MySQL

2. 搭建web项目环境

1. 创建动态web项目

项目的目录结构

2. 导入Struts、Spring、Hibernate、数据库驱动等jar包

3. 在web.xml中配置struts、spring

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--Struts过滤器-->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
<!--请求和转发都会被Struts拦截,默认情况下只有请求会被拦截-->
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>

<!--spring配置文件-->
<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>

</web-app>

4. 创建struts.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<!--debug mode-->
<constant name="struts.devMode" value="true" />

<package name="p1" extends="struts-default" namespace="/">
<action name="page_*_*">
<result name="success">/WEB-INF/pages/{1}/{2}.jsp</result>
</action>
</package>
</struts>

5. 创建jdbc.properties

1
2
3
4
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/bos
user=zhuobo
password=asdf

6. 创建applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

<!--hibernate-->
<context:property-placeholder location="classpath:jdbc.properties"/>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"/>
<property name="jdbcUrl" value="${jdbcUrl}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--hibernate其他配置-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
<!--映射文件-->
<property name="mappingDirectoryLocations">
<list>
<!--<value>classpath:com/zhuobo/bos/domain</value>-->
</list>
</property>
</bean>

<!--事务管理器-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!--组件扫描,注解扫描范围-->
<context:component-scan base-package="com.zhuobo.bos"/>

<!--引用注解解析器-->
<context:annotation-config/>

<!--事务注解-->
<tx:annotation-driven/>
</beans>

7. 中文乱码问题

web.xml中设置guolvqi,使用utf-8编码

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
    <!--字符编码过滤器,用于解决Tomcat7的POST请求的中文乱码问题-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<!--Spring web包提供的characterEncodingFilter只能解决POST中文乱码问题-->
<!-- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>-->
<filter-class>com.zhuobo.bos.web.filter.MyEncodingFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

MyCharacterEncoding.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
public class MyEncodingFilter implements Filter{

@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void destroy() {}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
//1.设置POST请求中文乱码的问题
request.setCharacterEncoding("UTF-8");
//System.out.println("拦截请求:" + request);

//2.解决get请求的中文乱码问题
//request : RequestFacade;
HttpServletRequest hsr = (HttpServletRequest)request;
if(hsr.getMethod().equalsIgnoreCase("get")){
MyRequest myRequest = new MyRequest(hsr);
//放行请求
chain.doFilter(myRequest, response);
}else{
chain.doFilter(request, response);
}
}
}
/**
* Wrapper包装类,装饰设计模式,内部有个真实对象的引用
*/
class MyRequest extends HttpServletRequestWrapper{

private HttpServletRequest request;
private boolean isEncoding = false;//是否已经utf-8编码
public MyRequest(HttpServletRequest request) {
super(request);
this.request = request;
}

@Override
public String getParameter(String name) {
return getParameterMap().get(name)[0];
}

@Override
public Map<String, String[]> getParameterMap() {
Map<String, String[]> map = request.getParameterMap();

if(isEncoding == true){
return map;
}

//遍历vlaue,改成utf-8编码
for(Entry<String, String[]> entry : map.entrySet()){
//取数组值
String[] values = entry.getValue();
for(int i=0;i<values.length;i++){
try {
values[i] = new String(values[i].getBytes("ISO-8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
isEncoding = true;

return map;
}
}

三、EasyUI

jQuery EasyUI 是一个基于 jQuery 的框架,集成了各种用户界面插件。

1. JQuery第三方框架的使用

  1. 下载
  2. 查看demo
  3. 查看源码
  4. 使用源码,修改源码

2. layout布局

3. accordion折叠选项卡

4. tabs选项卡

tabs-tool动态添加选项卡

四、ztree树形插件

ztree是一个jQuery插件

1. 标准JSON数据

  1. 一个Under List标签
1
<ul id="tree" class="ztree"></ul>
  1. 配置ztree参数
1
2
var setting = {};
// 默认参数
  1. 标准json数据
1
2
3
4
5
6
7
8
9
var zNodes = [
{name:"节点一",children:[
{name:"二级节点一"},
{name:"二级节点二"},
{name:"二级节点三"}
]},
{name:"节点二"},
{name:"节点三"}
];
  1. 初始化ztree生成树
1
2
3
$(document).ready(function () {
$.fn.zTree.init($("#tree"), setting, zNodes);
});

2. 简单JSON数据

  1. 配置ztree数据
1
2
3
4
5
6
7
8
9
10
var setting = {
check: {
enable: true
},
data: {
simpleData: {
enable: true
}
}
};
  1. 简单json数据
1
2
3
4
5
6
7
8
var nodes = [
{id:1, pId:0, name: "用户功能"},
{id:11, pId:1, name: "添加用户"},
{id:12, pId:1, name: "删除用户"},
{id:2, pId:0, name: "订单功能"},
{id:12, pId:2, name: "添加订单"},
{id:22, pId:2, name: "删除订单"}
];

3. 发送ajax请求获取菜单数据

有时候树形菜单的数据是从数据库中获取的,拼接为JSON数据返回客户端,客户端使用ztree便获取树形菜单。将一个包含json数据的目录拷贝到web目录下,模拟从数据库中获取的数据。

1
2
3
4
5
6
7
8
9
10
11
// 文档加载结束,发送异步请求,获取json数据,生成树形菜单

$(document).ready(function(){
//1.获取菜单数据
var url = "${pageContext.request.contextPath}/json/menu.json"
$.get(url,function (data) {
//console.log(data);
$.fn.zTree.init($("#treeDemo"), setting, data);
})
//
});

4. 监听菜单点击事件

点击属性菜单,便新增一选项卡,在setting中可以设置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var setting = {
data: {
simpleData: {
enable:true,
idKey: "id",
pIdKey: "pId",
rootPId: ""
}
},
callback:{
onClick:function (event, treeId, treeNode, clickFlag) {
// 父节点不需要显示选显卡
if (treeNode.isParent) return;

// 判断是否已经有相应的选项卡,如果有就变为选中状态,否则就显示一个新的选项卡
var tab = $("#tt").tabs("exists", treeNode.name);
if (tab) {
// 如果已经有对应的选项卡,设置为选中状态
$("#tt").tabs("select", treeNode.name);
} else {
// 否则就新增选项开
// 显示一个新的选项卡
$('#tt').tabs('add',{
title: treeNode.name,
content: '<div>'+treeNode.name+'</div>',
closable: true
});
} }
}
};