1. 程式人生 > >關於用MyEclipse2014的SSH框架搭建以及小例子實現 (例子確實太小)

關於用MyEclipse2014的SSH框架搭建以及小例子實現 (例子確實太小)

由於這學期發現有些同學在學SSH,同時想到自己也有一段時間沒有觸碰這個東西,於是決定寫一篇關於SSH框架搭建的以及實現一個新增小功能的博文,希望能給大家帶來幫助。。。。。。。。

一、工具

由於本人有程式設計方面的潔癖,算是吧,覺得能用最新版本的框架我就採用最新的

1.MyEclipse2014

2.Navicat

3.Struts2包:struts-2.5.10.1-all

4.Hibernate包:hibernate-release-5.2.8.Final

5.spring包:spring-framework-4.3.6.RELEASE-dist


以上的jar都是個人認為目前最新也比較好用的包。

二、搭建

1.新建一個動態的web專案,這個我想大家都應該知道,這裡就不做過多敘述

2.將jar包匯入lib資料夾(jar包最後會在連結中給出)

3.修改web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SSHFamily</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- Struts核心配置 -->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- spring監聽器配置 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  
</web-app>

注:由於採用了最新的struts包,所以struts的過濾器filtter配置在這個版本中也有了變化。struts2.3.x及以前的版本使用的是org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

而struts2.5修復了以前的一些漏洞,所以這裡也變成了org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter。2.5版本的struts也要匯入llog4j-api-2.7.jar這個包,由於開始我不僅匯入了這個包,還匯入了其他兩個關於log的jar包,結果導致頁面出現404錯誤,後來發現刪除就ok了。關於struts2.5的相關改動網上也有很多大牛的有力解釋。

4.在src目錄下新增applicationcontext.xml和struts.xml

applicationcontext.xml:

<?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:c="http://www.springframework.org/schema/c"
    xmlns:context="http://www.springframework.org/schema/context"
    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.xsd
http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">




<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>


<property name="url" value="jdbc:mysql://localhost:3306/bys?useUnicode=true&amp;characterEncoding=UTF8"></property>
<property name="username" value="root"></property>
<property name="password" value="xq1996621"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
<prop key="hibernate.format_sql">
true
</prop>
<prop key="hibernate.hbm2ddl.auto">
update
</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/vae/entity/ContactUs.hbm.xml</value>
</list>
</property>
</bean>


<bean id="contactUsAction" class="com.vae.ation.ContactUsAction" scope="prototype">
<property name="contactUsService" ref="contactUsService"></property>
</bean>

<bean id="contactUsDao" class="com.vae.dao.impl.ContactUsDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="contactUsService" class="com.vae.service.impl.ContactUsServiceImpl">
<property name="contactUsDao" ref="contactUsDao"></property>
</bean>





<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

struts.xml配置:

<?xml version="1.0" encoding="UTF-8" ?>


<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">


<!-- START SNIPPET: xworkSample -->
<struts>


    <package name="sshfamily" extends="struts-default" namespace="/">
<action name="contactus_*" class="contactUsAction" method="{1}">

</action>
</package>


</struts>


<!-- END SNIPPET: xworkSample -->

5.至此就環境就搭完了 ,是不是很簡單......................

專案的整個結構:


6.頁面效果:



資料庫:


完結!!!!!!!!!!!!!

重點:該專案的壓縮包見一下連結(裡面的資料庫密碼換成自己的相關密碼以及建好表即可):

http://download.csdn.net/detail/zaq12346zaqwsx/9777256

結語:關於SSH學習:

1.主要抓住思想把資料庫裡面抓出來的資料以相應的物件或者一個集合的形式儲存,實現頁面回顯資料

2.在於同學交流中,他覺得service層的程式碼跟dao層的程式碼一樣,不用寫,其實功能簡單,這兩層確實可以簡寫為一層,因為目前功能簡單也基本上做不到什麼業務邏輯處理。但是要體現MVC分層的思想,我覺得還是有必要掌握這種思想。因為業務邏輯層即service層在一些功能複雜的專案中就不會出現也dao層程式碼一致的情況,當然也不是必然。在複雜的功能中,service層會將dao層的從資料庫取出來的資料進行進一步的加工,來實現相應的符合使用者或者頁面的要求。比如把person表中的所有人的年齡取出來,在service層中進行相加等等。不過目前專案功能簡單就可以省略service層,直接用Action層呼叫dao層即可。

3.其實用到最後,會發現struts+Hibernate+spring這個組合還是會使用起來不太方便,因為裡面包含了過多的配置檔案。所以我推薦目前想換個框架的同仁用用看springmvc,這個就比較輕量級了,springmvc也可以理解為時替換了struts。

完結   ..............撒花