搭建SSH架構順序和注意事項

來源:互聯網
上載者:User

 環境為 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, logfilelog4j.appender.syslog.encoding=GBKlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%nlog4j.appender.logfile=org.apache.log4j.RollingFileAppender#log4j.appender.logfile.File=${petstore.root}/WEB-INF/petstore.loglog4j.appender.logfile.File=D\:\\log4j.loglog4j.appender.logfile.MaxFileSize=512KB# Keep three backup files.log4j.appender.logfile.MaxBackupIndex=3# Pattern to output: date priority [category] - messagelog4j.appender.logfile.layout=org.apache.log4j.PatternLayoutlog4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n


 

 

 

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.