0251112SSH框架整合Word文档格式.docx
《0251112SSH框架整合Word文档格式.docx》由会员分享,可在线阅读,更多相关《0251112SSH框架整合Word文档格式.docx(35页珍藏版)》请在冰点文库上搜索。
![0251112SSH框架整合Word文档格式.docx](https://file1.bingdoc.com/fileroot1/2023-5/4/224c76ca-6cbb-48c8-942e-d0278f3dafd3/224c76ca-6cbb-48c8-942e-d0278f3dafd31.gif)
#设定Logger的Console(appender的名字)的Appender类型为控制台输出
#org.apache.log4j.ConsoleAppender表明Console是控制台输出
log4j.appender.Console=org.apache.log4j.ConsoleAppender
#输出的格式
#设定Console的Appender布局Layout
#org.apache.log4j.PatternLayout(可以灵活地指定布局模式)包含选项:
#ConversionPattern=%m%n:
指定怎样格式化指定的消息
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
#2009-09-2907:
30:
43,265INFOcom.itcast.web.controller.SearchCdServlet.doGet()-e
log4j.appender.Console.layout.ConversionPattern=%d%p%c.%M()-%m%n
#设定Logger的logfile(appender的名字)的Appender类型为文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
#设定文件的输出路径(指定文件的名字和路径,tomcat的启动路径)
log4j.appender.logfile.File=../logs/logstrore.log
#设定后缀可以是KB,MB或者是GB.在日志文件到达该大小时,将会自动滚动,即将原来的内容移到logstrore.log.1文件
log4j.appender.logfile.MaxFileSize=2048KB
#Keepthreebackupfiles.指定可以产生的滚动文件的最大数
log4j.appender.logfile.MaxBackupIndex=4
#设定logfile的Appender布局Layout
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d%p%c.%M()-%m%n
#####################################################################
3.在web.xml配置Struts核心过滤器
配置文件如下:
<
?
xmlversion="
1.0"
encoding="
UTF-8"
>
web-appxmlns:
xsi="
http:
//www.w3.org/2001/XMLSchema-instance"
xmlns="
xsi:
schemaLocation="
id="
WebApp_ID"
version="
2.5"
<
display-name>
SSHLogin<
/display-name>
welcome-file-list>
<
welcome-file>
index.jsp<
/welcome-file>
/welcome-file-list>
!
--核心过滤器-->
filter>
filter-name>
struts2<
/filter-name>
filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter<
/filter-class>
/filter>
filter-mapping>
url-pattern>
/*<
/url-pattern>
/filter-mapping>
/web-app>
4.添加struts.xml配置文件
在config路径下添加struts配置文件struts.xml。
配置如下:
?
DOCTYPEstrutsPUBLIC
"
-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.3//EN"
//struts.apache.org/dtds/struts-2.3.dtd"
struts>
constantname="
struts.devMode"
value="
true"
/>
packagename="
ssh"
namespace="
/"
extends="
struts-default"
/package>
/struts>
3.配置spring环境
1.导入spring的依赖jar包
本系统使用的是Spring3.2.2版本依赖的基础jar包共14个,具体如下:
2.核心配置文件applicationContext.xml
配置文件如下:
beansxmlns="
//www.springframework.org/schema/beans"
xmlns:
context="
//www.springframework.org/schema/context"
util="
//www.springframework.org/schema/util"
jee="
//www.springframework.org/schema/jee"
tx="
//www.springframework.org/schema/tx"
jpa="
//www.springframework.org/schema/data/jpa"
mvc="
//www.springframework.org/schema/mvc"
p="
//www.springframework.org/schema/p"
aop="
//www.springframework.org/schema/aop"
http:
//www.springframework.org/schema/beanshttp:
//www.springframework.org/schema/beans/spring-beans.xsd
//www.springframework.org/schema/contexthttp:
//www.springframework.org/schema/context/spring-context.xsd
//www.springframework.org/schema/utilhttp:
//www.springframework.org/schema/util/spring-util.xsd
//www.springframework.org/schema/jeehttp:
//www.springframework.org/schema/jee/spring-jee.xsd
//www.springframework.org/schema/txhttp:
//www.springframework.org/schema/tx/spring-tx.xsd
//www.springframework.org/schema/data/jpahttp:
//www.springframework.org/schema/data/jpa/spring-jpa.xsd
//www.springframework.org/schema/mvchttp:
//www.springframework.org/schema/mvc/spring-mvc.xsd
//www.springframework.org/schema/aophttp:
//www.springframework.org/schema/aop/spring-aop.xsd"
/beans>
3.在web.xml中配置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>
4.配置hibernate环境
Struts和Spring的环境配置完毕后,接下来,对hibernate的环境进行配置。
1.hibernate项目所依赖的jar包需要11个,如下:
2.添加核心配置文件hibernate.cfg.xml:
xmlversion='
1.0'
encoding='
utf-8'
DOCTYPEhibernate-configurationPUBLIC
-//Hibernate/HibernateConfigurationDTD3.0//EN"
hibernate-configuration>
session-factory>
--数据库连接信息-->
propertyname="
hibernate.connection.driver_class"
com.mysql.jdbc.Driver<
/property>
hibernate.connection.url"
jdbc:
mysql:
//localhost:
3306/test<
hibernate.connection.username"
root<
connection.password"
--hibernate配置信息-->
--SQLdialect方言,用于配置生成针对哪个数据库的-->
dialect"
--方言类,Hibernate提供的,用于封装某种特定的-->
org.hibernate.dialect.MySQL5Dialect
--生成的sql是否打印到控制台-->
show_sql"
true<
--打印的sql是否进行格式化-->
format_sql"
hbm2ddl.auto"
update<
hibernate.connection.autocommit"
--在配置文件中关联映射文件-->
/session-factory>
/hibernate-configuration>
5.进行spring和hibernate的整合:
hibernate.cfg.xml文件是Hibernate中重要的配置文件,当Spring和Hibernate整合时,由于hibernate.cfg.xml文件中的配置信息可以交予Spring来管理,所以可以选择是否使用hibernage.cfg.xml文件。
本文章将对使用hibernate.cfg.xm文件和不使用hibernate.cfg.xml文件两种情况来讲解Spring和Hibernate的整合。
Spring和Hibernate的整合,需要添加一个支持整合的jar包,srping-orm-3.2.2.RELEASE.jar。
1.首先建立实体类,新建entity包,新建User类,源码如下:
packageentity;
publicclassUser{
privateIntegerid;
privateStringusername;
privateStringpassword;
publicIntegergetId(){
returnid;
}
publicvoidsetId(Integerid){
this.id=id;
publicStringgetUsername(){
returnusername;
publicvoidsetUsername(Stringusername){
this.username=username;
publicStringgetPassword(){
returnpassword;
publicvoidsetPassword(Stringpassword){
this.password=password;
@Override
publicStringtoString(){
return"
User[id="
+id+"
username="
+username+"
password="
+password+"
]"
;
}
2.在此包下新建User.hbm.xml,源代码如下:
DOCTYPEhibernate-mappingPUBLIC
-//Hibernate/HibernateMappingDTD3.0//EN"
hibernate-mapping>
classname="
entity.User"
table="
t_user"
idname="
id"
generatorclass="
identity"
/generator>
/id>
username"
password"
/class>
/hibernate-mapping>
此时,User.hbm.xml建立好后,需要在hibernate的核心配置文件中hibernate,cfg.xml中添加这个映射文件,源码入下:
mappingresource="
entity/user.hbm.xml"
3.dao层的编码,新建dao包,新建UserDao接口类,源码如下:
packagedao;
importjava.util.List;
importentity.User;
publicinterfaceUserDao{
publicvoidsave(Useruser);
publicvoidupdate(Useruser);
publicvoiddelete(Useruser);
publicUserfindById(Integerid);
publicUserfindUser(Stringusername,Stringpassword);
publicList<
User>
findAll();
4.实现类的编码,新建dao.impl包,新建UserDaoImpl实现类,源码如下:
packagedao.impl;
importorg.springframework.orm.hibernate3.HibernateTemplate;
importdao.UserDao;
publicclassUserDaoImplimplementsUserDao{
privateHibernateTemplatehibernateTemplate;
publicvoidsetHibernateTemplate(HibernateTemplatehibernateTemplate){
this.hibernateTemplate=hibernateTemplate;
publicvoidsave(Useruser){
this.hibernateTemplate.save(user);
System.out.println("
-----------------------ddd"
publicvoidupdate(Useruser){
hibernateTemplate.update(user);
publicvoiddelete(Useruser){
hibernateTemplate.delete(user);
publicUserfindById(Integerid){
returnhibernateTemplate.get(User.class,id);
publicUserfindUser(Stringusername,Strin