第一次使用这3大框架进行网站编写的人没有一个清楚的流程,建起网站来会比较头疼,今天来讲讲3大框架的配置

基本流程:

用户点击页面之后,服务器收到一个请求,请求经过web.xml的拦截器过滤后进入action,不同的action执行后进入service业务处理层,业务处理层需要访问数据库的话,service将任务

告知dao数据访问层,dao层访问数据库获取到数据后将数据返回给service层,service将数据处理后返回给action,不同的action依照service的返回值执行不同的过程,然后返回一个字

符串或者数据(下面并未讲到如何在配置文件中配置返回数据,请读者自行查找),服务器依照struts.xml文件中的配置进行页面的跳转,然后在浏览器,用户便看到了不同的页面。

具体配置:

在用户点击一个链接之后,我们需要它执行一段代码,那么我们就要识别这个链接是否符合我们的要求,于是我们就要配合拦截器了

拦截器的配置需要写在WEB-INF目录下的web.xml中

类似这个样子

<filter>
<filter-name>Struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter> <filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>

通过这个,我们可以拦截action请求了,action是专门用于处理来自页面的各种请求的,相当于是后台的外交官,action在后台的处理流程一般是:

action  ->  service  ->  dao  (这个和SHH框架没有太大关系,但这样写能让各种功能分开,方便以后维护)

service是具体的业务逻辑处理层

dao是和数据库打交道的层

在action处理完数据之后,我们需要跳转页面或者返回数据,这里就又要进行配置了,我们把action的配置命名为struts.xml(这个是默认的名字,可以在web.xml中修改)

在struts.xml文件中,我们需要依照action返回的不同结果进行不同的页面跳转,具体配置类似这样子

<action name="index" class="lee.action.IndexAction">
<result name="success">/pages/index.jsp</result>
<result name="input">/error.jsp</result>
</action>

当action返回的是字符串“success”,那么页面将跳转到index.jsp页面

当然了,这里的跳转是支持action之间相互跳转的,需要这么写

<result name="input" type="redirectAction">login</result>

如果返回的字符串是“input”,那么程序将回到以login为名字的action进行处理。

要使用spring框架我们还需要对bean进行配置,将各个类默认以简单名称存储在bean中,我们将配置文件命名为beans.xml和struts.xml保存在相同目录下,在web.xml中配置

此文件的路径。在web.xml中加上这么一段

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value> </context-param>

然后我们在beans.xml中配置数据库链接:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>

这里的db.properties是数据库连接的配置文件,里面的格式是

JDBC.DBDriver=
JDBC.Connection=
JDBC.User=
JDBC.Password=

等号后面需要自己去填。

数据库链接配置完了之后,我们需要配置sessionFactory

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
<!-- 数据源的配置 -->
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<!-- 映射文件的配置 -->
<property name="packagesToScan">
<list>
<value>lee/entity</value>
</list>
</property>
</bean>

在这里hibernate.cfg.xml文件是自己写的映射的配置,lee/entity是映射的路径

下面是hibernate.cfg.xml的示例代码,大家可以参考一下

<hibernate-configuration>
<session-factory>
<!-- 数据库hibernate的信息 --> <!-- 显示sql语句 -->
<property name="show_sql">true</property>
<!-- 显示方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 若数据库中没有创建表则自动创建表 -->
<property name="hibernate.hbm2ddl.auto">update</property> <!-- hibernate映射类 -->
<mapping resource="lee/entity/Book.hbm.xml" />
<mapping resource="lee/entity/BookUnChecked.hbm.xml" />
<mapping resource="lee/entity/BookCategory.hbm.xml" />
<mapping resource="lee/entity/User.hbm.xml" />
<mapping resource="lee/entity/UserCategory.hbm.xml" />
<mapping resource="lee/entity/UserNote.hbm.xml" />
<mapping resource="lee/entity/UserView.hbm.xml" />
<mapping resource="lee/entity/BookChapter.hbm.xml" />
<mapping resource="lee/entity/BookCase.hbm.xml" /> </session-factory>
</hibernate-configuration>

然后我们还需要在beans中配置事务管理器

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

至此,3大框架大概配置基本讲完了

(最后,谢谢 衍悔 在完善文章给予的帮助)

struts2、hibernate以及spring是如何配置的的更多相关文章

  1. 【hibernate】spring+ jpa + hibername 配置过程遇到的问题

    org.springframework.data.jpa.domain.support.AuditingEntityListener Auditing: 审计:查账Hades:Hades是一个开源库, ...

  2. C3P0连接池在hibernate和spring中的配置

    首先为什么要使用连接池及为什么要选择C3P0连接池,这里就不多说了,目前C3P0连接池还是比较方便.比较稳定的连接池,能与spring.hibernate等开源框架进行整合. 一.hibernate中 ...

  3. Struts2+hibernate+spring 配置事物

    今天自信看了看hibernate的事物配置问题,转载了其他人的日志,仅用来学习. struts+hibernate+spring事务配置 (2009-01-14 21:49:47) 转载▼ 标签: i ...

  4. SSH整合(Struts2+hibernate+spring)

    1.创建表 create table t_user( id int primary key auto_increment, username varchar(50), password varchar ...

  5. 整合Struts2、Hibernate、Spring

    将项目中的对象和对象之间的管理,纳入spring容器,由spring管理 1 实现spring+hibernate集成 1.1 新建web项目 建立项目的包结构(package) 1.2加入jar包 ...

  6. Struts2,Hibernate和Spring之间的框架整合关系

    1.首先要认清,hibernate和struts没有半点关系,所以他们之间没有任何可以整合的东西.a:struts 作为中心控制器,肯定要调用一些类来完成一些逻辑.而hibernate开发中,经常使用 ...

  7. struts2,hibernate,spring整合笔记(2)

    上一话struts2,hibernate,spring整合笔记(1) 接下来继续 配置完struts之后就要开始hibernate的配置 hibernate的环境并不依赖web开发环境,在我第一次配置 ...

  8. 工作笔记3.手把手教你搭建SSH(struts2+hibernate+spring)环境

    上文中我们介绍<工作笔记2.软件开发经常使用工具> 从今天開始本文将教大家怎样进行开发?本文以搭建SSH(struts2+hibernate+spring)框架为例,共分为3步: 1)3个 ...

  9. struts1,struts2,hibernate,spring的运行原理结构图

    一.struts1运行原理 1.初始化:struts框架的总控制器ActionServlet是一个Servlet,它在web.xml中配置成自动启动的Servlet,在启动时总控制器会读取配置文件(s ...

随机推荐

  1. 连接hive

    bin/hiveserver2 nohup bin/hiveserver2 1>/var/log/hiveserver.log 2>/var/log/hiveserver.err & ...

  2. MVC 移动识别

    ASP.NET MVC移动端识别 上面我们已经说了 响应式布局,但那是客户端的,针对于同一个视图页面的.不过 同一个视图页面 通过响应式布局 也是有缺点的 会导致页面 样式十分庞大 页面加载效率降低, ...

  3. ABP框架系列之十三:(Authorization-授权)

    Introduction Almost all enterprise applications use authorization in some level. Authorization is us ...

  4. ABP框架系列之二十五:(Embedded-Resource-Files-嵌入式资源文件)

    Introduction ASP.NET Boilerplate provides an easy way of using embedded Razor views (.cshtml files) ...

  5. 深入浅出WPF文摘

    第一部分 深入浅出XMAL 第一章 XMAL概览 第二章 从零起步认识XMAL 第三章 系统学习XMAL语法 第四章 X名称空间详解 第五章 控件与布局 GUI:图形化用户界面 逻辑树: 可视树: : ...

  6. Python_day5

    局部变量 全局变量 def test(): # 声明使用全局变量x global x x = 100 y = 300 # 局部变量:作用域和生存周期仅在从定义开始到函数结束 x = 200 # 全局变 ...

  7. 响应式---web设计之CSS3 Media Queries

    一:设置视口  (view [vju:] 看; ==看待  port [pɔ:t] 接口)==视口 <meta name="viewport" content="w ...

  8. 2019swpuj2ee作业3

    静态页面: 在网站设计中,纯粹html格式的网页通常被称为“静态网页”,早期的网站一般都是由静态网页制作的.静态网页是相对于动态网页而言,是指没有后台数据库.不含程序和不可交互的网页.你编的是什么它显 ...

  9. docker安装镜像

    CMD 容器启动命令 CMD指令用于为执行容器提供默认值.每个Dockerfile只有一个CMD命令,如果指定了多个CMD命令,那么只有最后一条会被执行,如果启动容器的时候指定了运行的命令,则会覆盖掉 ...

  10. 【转】Map 与 Unordered_map

    map和unordered_map的差别和使用 map和unordered_map的差别还不知道或者搞不清unordered_map和map是什么的,请见:http://blog.csdn.net/b ...