搭建SSH框架顺序和注意事项

 环境为 myeclipse8.6  数据库 mysql5.0  Jdk 1.6 WindowsXp  TomCat 6.0

首先创建一个 New  =>  Web Project  起名 demo

 

然后在项目名称上 点击鼠标右键 选择 MyEclipse => Add Speing Capabilites...

 

接下来 就是选择需要加载 Spring 的版本 我在这里选择 3.0

 选择需要加载的库文件 Spring 3.0 Aop Libraries,core Libraries,Persistence Core Lisbraries,

还有一个  Web Libraries

后面基本上没啥了 直接 Finish 吧。

 

然后接着添加 hibernate

还是项目名称上 点击鼠标右键 选择 MyEclipse => Add Hibernate  Capabilites...

 

在这里我选择的hibernate版本是3.2

库文件分别是 Annotations & Entity Manager  和 Core Libraries

选择 Copy checked library Jars to project folder and add to build-path 将库文件复制到 /WebRoot /Web-INF/lib 目录下

接下来 选择 Next 进入下一配置页面 选择 Spring configuration file(applicationContext.xml) 让Spring 托管 hibernate的配置文件

选择 Existing Spring Configuration file

接下来配置数据库连接参数 jdbc

MYSql 配置如下:

Connect  URL:jdbc:mysql://localhost:3306/system

Driver Class:com.mysql.jdbc.Driver

Username:root

password:root

Dialect:MySQL

接下来 将 Create SessionFactory class 选项 取消,最后 Finish

 

最后我们来添加Struts2

还是项目名称上 点击鼠标右键 选择 MyEclipse => Add Struts Capabilites...

我们选择 struts的版本为 2.1 过滤器名称 默认为 struts2  拦截 URL pattern 选择 /* 对所有链接都进行过滤监测

 

接下来选择 加载库文件 在这里我选了 Core Libraries,DOJO Libraries,Spring Libraries(这是Struts 支持Spring 的库文件)

最后 选择 FINISH.

结束添加SSH框架的第一部;

接下来我们要配置  Web.xml 文件 添加 spring 的监听器 WebRoot/WEB-INf/web.xml

 

<!-- Struts2  -->
	<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>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>*.jsp</url-pattern>
	</filter-mapping>

	<listener>
		<listener-class>
		org.springframework.web.context.ContextLoaderListener
		</listener-class>
 	</listener>
	<!-- 设置spring监听 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<!-- 通用返回页面 -->
	<error-page>
		<error-code>404</error-code>
		<location>/page/comm/404Error.jsp</location>
	</error-page>
	<error-page>
		<error-code>500</error-code>
		<location>/page/comm/500Error.jsp</location>
	</error-page>


配置 applicationContext.xml 的 aop 头 文件

 

<?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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">


 

配置 数据库c3p0连接池

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<!-- 1.需要更改数据库ip、端口、数据库名称 -->
		<property name="jdbcUrl"
			value="jdbc:mysql://127.0.0.1:3306/system_showapp?useUnicode=true&characterEncoding=utf-8" />
		 <!-- 2.数据库用户名 -->
		<property name="user" value="root" />
		<!-- 3.数据库密码 -->
		<property name="password" value="rootroot" />
		<!--连接池中保留的最小连接数。Default: 3 -->
		<property name="minPoolSize" value="5" />
		<!--连接池中保留的最大连接数。Default: 15 -->
		<property name="maxPoolSize" value="20" />
		<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		<property name="initialPoolSize" value="10" />
		<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
		<property name="maxIdleTime" value="20" />
		<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
		<property name="acquireIncrement" value="3" />
		<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
		<property name="acquireRetryAttempts" value="30" />
		<!--两次连接中间隔时间,单位毫秒。Default: 1000 -->
		<property name="acquireRetryDelay" value="1000"></property>
		<!--连接关闭时默认将所有未提交的操作回滚。Default: false -->
		<property name="autoCommitOnClose" value="false"></property>
		<!--
			JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements属於单个connection而不是整个			连接池。所以设置这个参数需要考虑到多方面的因素。
			如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0
		-->
		<property name="maxStatements" value="0" />
		<!--每60秒检查所有连接池中的空闲连接-->
		<property name="idleConnectionTestPeriod" value="60" />
		<!--
			maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0
		-->
		<property name="maxStatementsPerConnection" value="0"></property>
		<!--
			因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的时候都将校验其有效性。建议使用					idleConnectionTestPeriod或automaticTestTable
			等方法来提升连接测试的性能。Default: false
		-->
		<property name="testConnectionOnCheckout" value="false"></property>
		<!--如果设为true那么在取得连接的同时将校验连接的有效性。Default: false -->
		<property name="testConnectionOnCheckin" value="true"></property>
	</bean>

 

如果有多个 applicationContext.xml文件可以在web.xml内配置

 <!-- 设置spring监听 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext*.xml</param-value>
  </context-param>



接着配置 struts.xml 可以多配置文件 通过 <include file="struts-1.xml"></include> 标签导入

	<constant name="struts.i18n.encoding" value="UTF-8" />
	<include file="struts-1.xml"></include>
	<include file="struts-2.xml"></include>
	<include file="struts-3.xml"></include>
	<package name="System" extends="struts-default" namespace="/">
	</package>
	<!-- 
		通用拦截器 请在package 内设置继承 extends="mystruts" 每个action内添加
		 	-拦截器- 就可以进行不登陆拦截
			<interceptor-ref name="mystack"></interceptor-ref>
	-->
		<package name="mystruts" extends="struts-default" namespace="/">
	<!-- 拦截器设置 --> 
		<interceptors>
	<!-- 定义的权限拦截器 -->
			<interceptor name="myauth" class="com.dw.util.AuthorizationInterceptor"></interceptor>
	<!-- 定义拦截器栈 -->
			<interceptor-stack name="mystack">
				<interceptor-ref name="defaultStack"></interceptor-ref>
				<interceptor-ref name="myauth"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
	<!-- 通用返回 -->
		<global-results>
			<result name="input" type="redirect">/index.jsp</result>
		</global-results>
	</package>



添加 log4j.properties

# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.
log4j.rootLogger=INFO, stdout, logfile
log4j.appender.syslog.encoding=GBK
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
#log4j.appender.logfile.File=${petstore.root}/WEB-INF/petstore.log
log4j.appender.logfile.File=D\:\\log4j.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n


 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章