app服务器上的文件组成,聊天App—服务端的准备(SSH的学习)

经过十一周两场考试和电子实训后,学习也进入了新的阶段,突发奇想想尝试做一个简易的聊天app项目,以后在朋友间使用也是一件挺有趣的事情。

首先先从服务端的搭建开始,服务端最主要的就是进行数据的处理和数据库的连接。我目前学习的有了jdbc连接数据库+MVC开发模式、使用MyBatis搭建,虽然用以有的知识也能搞定但学多的知识总有好处,所以我选择了最经典的Struts2+Spring+Hibernate(SSH)来搭建服务端。

由于这次是直接使用ssh框架对每一个组成的框架我只是有一个大概了解,以后有时间再深入学习。

一、SSH所需开发包

刚学习SSH遇到的第一个困难就是导包,因为实在是太多太多了,毕竟我还是一只菜鸡没见过什么大工程= =。

spring所需的jar包

27e0f0982462?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

spring.png

struts2所需jar包

27e0f0982462?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

struts.png

Hibernate所需jar包

27e0f0982462?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

hibernate.png

项目使用MySql数据库,所以还有一个MySql的驱动包总共的jar包应该有41个,接下来对一些重要jar包做一下解释。

struts2-convention-plugin-2.3.15.3.jar -struts注解开发包

struts2-spring-plugin-2.3.15.3.jar -struts整合spring的jar包

事务管理jar包

spring-tx-3.2.0.RELEASE.jar

spring-jdbc-3.2.0.RELEASE.jar

spring-orm-3.2.0.RELEASE.jar -spring整合hibernate的jar包

spring-web-3.2.0.RELEASE.jar -spring整合web项目的jar包

spring-test-3.2.0.RELEASE.jar Junit测试单元

com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar c3p0连接池jar包

二、引入配置文件

struts配置文件

web.xml

struts.xml

spring配置文件

web.xml

applicationContext.xml

在ssh整合框架中不需要Hibernate的配置文件因此不用引入

三、配置文件的配置

因为内容有些多而且不好解释直接上代码

web.xml

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:applicationContext.xml

struts

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts

/*

index.jsp

applicationContext.xml

xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

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/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd">

struts.xml

/p>

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

这里需要万分注意的是Spring核心监听器的配置即

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:applicationContext.xml

ContextLoaderListener中有一个参数,他默认是加载WEB-INF目录下的applicationContext.xml,但我们需要加载的是classes目录下的applicationContext.xml,所以在这里边我们需要配置一个全局触发参数。

首先引入源代码:

ctrl+左击点击org.springframework.web.context.ContextLoaderListener进入如下页面

27e0f0982462?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

ContextLoaderListener.png

然后点击AttachSource

27e0f0982462?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

引入源码.png

此部很重要因此在此详细说明,还有就是配置文件都应该放在src目录下

三、创建包结构

使用SSH框架在不知不觉中以使用了MVC开发模式,此包结构仅当参考

27e0f0982462?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

包结构的创建.png

四、Struts整合Spring-编写Action/Service/

public class EmployeeAction extends ActionSupport implements ModelDriven{

//模型驱动

private Employee employee=new Employee();

public Employee getModel() {

// TODO Auto-generated method stub

return employee;

}

//注入业务层

private EmployeeService employeeService;

public void setEmployeeService(EmployeeService employeeService) {

this.employeeService = employeeService;

}

}

在Spring中只要有set方法就可以完成自动注入,Service提供set方法注入Dao

public class EmployeeServiceImpl implements EmployeeService {

private EmployeeDao employeeDao;

/**

* @param employeeDao the employeeDao to set

*/

public void setEmployeeDao(EmployeeDao employeeDao) {

this.employeeDao = employeeDao;

}

}

五、Struts整合Spring-在配置文件中配置Action/Service/

打开applicationContext.xml

六、Spring整合Hibernate-创建数据库

手动创建好数据库后,交给hibernat会自动帮我们创立表

找到相应的Bean并创建对应映射文件。

Department.java

public class Department {

private Integer did;

private String dname;

private String dinfo;

//员工集合

private Set employee=new HashSet();

public Department() {

}

public Integer getDid() {

return did;

}

public void setDid(Integer did) {

this.did = did;

}

public String getDname() {

return dname;

}

public void setDname(String dname) {

this.dname = dname;

}

public String getDinfo() {

return dinfo;

}

public void setDinfo(String dinfo) {

this.dinfo = dinfo;

}

public Set getEmployee() {

return employee;

}

public void setEmployee(Set employee) {

this.employee = employee;

}

}

与之相对应的xml

/p>

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

注意在配置相应xml的时候要引入Hibernate的约束条件。约束条件在相应hibernate的核心包下面的hibernate-mapping-3.0dtd中,将映射条件复制到我们数据库映射文件下即可。

七、spring整合hibernate配置文件

jdbc的配置文件

jdbc.properties

jdbc.driverClass=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/good

jdbc.username=root

jdbc.password=12312313

jdbc配置文件要在applicationContext.xml中引入

org.hibernate.dialect.MySQLDialect

true

true

update

cn/crm/ssh/bean/xml/Department.hbn.xml

cn/crm/ssh/bean/xml/Employee.hbn.xml

总结

现在对SSH只是有一个基本了解,与之前学习最大的不同即是没有了Servlet取而代之的是Action。数据库不用手动去创建直接交给hibernate自动生成即可,在使用SSH框架时不知不觉间已使用了MVC的开发模式,这对以后进一步学习服务端搭建提供了很好的学习基础,最后希望这篇文章对你有用让我们共同进步。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值