国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

MyEclipse整合ssh三大框架環(huán)境搭載用戶注冊(cè)源碼下載

2019-11-26 13:39:08
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

前言

SSH不是一個(gè)框架,而是多個(gè)框架(struts+spring+hibernate)的集成,是目前較流行的一種Web應(yīng)用程序開(kāi)源集成框架,用于構(gòu)建靈活、易于擴(kuò)展的多層Web應(yīng)用程序。

集成SSH框架的系統(tǒng)從職責(zé)上分為四層:表示層、業(yè)務(wù)邏輯層、數(shù)據(jù)持久層和域模塊層(實(shí)體層)。

Struts作為系統(tǒng)的整體基礎(chǔ)架構(gòu),負(fù)責(zé)MVC的分離,在Struts框架的模型部分,控制業(yè)務(wù)跳轉(zhuǎn),利用Hibernate框架對(duì)持久層提供支持。Spring一方面作為一個(gè)輕量級(jí)的IoC容器,負(fù)責(zé)查找、定位、創(chuàng)建和管理對(duì)象及對(duì)象之間的依賴(lài)關(guān)系,另一方面能使Struts和Hibernate更好地工作。

使用MyEclipse整合SSH三大框架,并實(shí)現(xiàn)一個(gè)模擬用戶注冊(cè)的Demo,對(duì)應(yīng)版本:

Struts版本:2.1;

Spring版本:3.1;

Hibernate版本:3.3;

一、整合前準(zhǔn)備工作

1.建立一個(gè)Web項(xiàng)目,如下:

注意:支持action的包名必須是“action”,且action類(lèi)必須是以Action結(jié)尾,即形如XxxAction這種形式,如上圖中所示

2.創(chuàng)建數(shù)據(jù)庫(kù)以及表:

CREATE DATABASE sshdemo; CREATE table t_user( id INT PRIMARY KEY, username VARCHAR(10), password VARCHAR(20) ) 

3.導(dǎo)入數(shù)據(jù)庫(kù)連接池c3p0jar包,點(diǎn)擊可下載:

c3p0-0.9.2-pre1.jarmysql-connector-java-5.1.13-bin.jar

二、Struts框架的配置:

1.選中項(xiàng)目,右鍵選擇:MyEclipse -> Project Facets[Capabilities] -> Install Apache Struts (2.x) Facet,如下:

2.選擇版本,在這里我選擇的是2.1,點(diǎn)擊"Finish",如下:

3.完成上述步驟以后,會(huì)發(fā)現(xiàn)在src目錄下多出一個(gè)struts.xml 文件,內(nèi)容如下:

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts>  </struts> 

4.在WEB-INF目錄下的web.xml文件中多一段關(guān)于struts過(guò)濾器的配置代碼,如下:

5.參考上圖,將*.action修改為"/*",至此struts框架配置完畢;

三、Spring框架的配置:
1.參考struts的配置,選中項(xiàng)目,右鍵選擇:MyEclipse -> Project Facets[Capabilities] -> Install Spring Facet,選擇版本,在此選擇3.1如下:

2.點(diǎn)擊"Finish",會(huì)發(fā)現(xiàn)src目錄下多了一個(gè)applicationContext.xml文件,WEB-INF目錄下多了一個(gè)spring-form.tld與spring.tld文件,并且在web.xml文件中多了一段與spring配置有關(guān)的代碼,spring框架搭建基本完畢(引入命名空間會(huì)在后面講到),如下所示:

四、Hibernate框架的配置:

1.參考struts的配置,選中項(xiàng)目,右鍵選擇:MyEclipse -> Project Facets[Capabilities] -> Install HibernateFacet,選擇版本,在此選擇3.3如下:

2.點(diǎn)擊"Finish",會(huì)發(fā)現(xiàn)src目錄下多了一個(gè)缺省包(可以刪除),并且在web.xml文件中多了一段代碼(后面會(huì)重新配置),如下所示:

3.支持“@Entity”注解的jar包導(dǎo)入:選中項(xiàng)目,右鍵選擇:MyEclipse -> Project Facets[Capabilities] ->Manage...,然后照下圖中的步驟操作:

完成上述步驟,三大框架基本就搭建起來(lái)了,接下來(lái)整合它們。

五、整合

1.為了不讓applicationContext.xml看起來(lái)太臃腫,以及便于管理,我們將Hibernate有關(guān)的配置保存在另外一個(gè).xml文件中,然后再在applicationContext.xml導(dǎo)入,其具體步驟: 

(1)在src目錄下(與applicationContext.xml同級(jí))創(chuàng)建一個(gè)名為hibernateContext.xml的文件,復(fù)制applicationContext.xml里面的內(nèi)容,然后再做修改; 

(2)hibernateContext.xml文件里面的內(nèi)容:

<?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"  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">    <!-- sessionFactory 配置 -->  <bean id="sessionFactory"  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  <!-- dataSource的屬性會(huì)在applicationContext.xml文件中配置,在這里先引用 -->  <property name="dataSource" ref="dataSource"></property>  <!-- 設(shè)置hibernate相關(guān)的配置項(xiàng) -->  <property name="hibernateProperties">   <!-- props標(biāo)簽是為了注入Properties這個(gè)類(lèi)型的屬性 -->   <!-- key必須加上hibernate.前綴 -->   <props>   <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>   <!-- show_sql目的是打印sql語(yǔ)句 -->   <prop key="hibernate.show_sql">true</prop>   <!-- 美化SQL的打印格式 -->   <prop key="hibernate.format_sql">true</prop>   <!-- a) create-drop:在執(zhí)行程序的時(shí)候創(chuàng)建數(shù)據(jù)表,在執(zhí)行完了之后刪除表,實(shí)際開(kāi)發(fā)中,常用于測(cè)試    b) create:在每次執(zhí)行程序的時(shí)候重新創(chuàng)建數(shù)據(jù)表    c) update:在執(zhí)行程序的時(shí)候會(huì)判斷,如果存在,不創(chuàng)建表,否則創(chuàng)建數(shù)據(jù)表,并且會(huì)根據(jù)實(shí)體類(lèi)中的屬性的增加,而自動(dòng)增加數(shù)據(jù)表中的字段(開(kāi)發(fā)環(huán)境)    d) validate:在執(zhí)行程序的時(shí)候會(huì)判斷,如果實(shí)體類(lèi)中的屬性與表中的字段不一致,那么就報(bào)錯(cuò)(生產(chǎn)環(huán)境) -->   <prop key="hibernate.hbm2ddl.auto">validate</prop>   </props>  </property>   <!-- 配置hibernate的實(shí)體類(lèi) -->  <property name="packagesToScan">   <!--list標(biāo)簽是用來(lái)注入String[]類(lèi)型的屬性 ,其值一般是對(duì)應(yīng)的bean包的全限名,而bean包中的類(lèi)一般又是與數(shù)據(jù)庫(kù)中的表對(duì)應(yīng)-->   <list>   <value>com.beauxie.bean</value>   </list>  </property>  </bean>   <!-- 配置 hibernateTemplate模板 -->  <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">  <property name="sessionFactory" ref="sessionFactory"></property>  </bean>  </beans>

 (3)在applicationContext.xm刪除“sessionFactory”的配置(因?yàn)樵趆ibernateContext.xml中已經(jīng)配置好了),然后導(dǎo)入已經(jīng)修改好的hibernateContext.xml內(nèi)容,導(dǎo)入完以后,此時(shí)applicationContext.xml內(nèi)容如下:

<?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"  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">    <bean id="dataSource"  class="org.apache.commons.dbcp.BasicDataSource">  </bean>   <!-- 導(dǎo)入其他的spring配置文件 ,如果都放在一個(gè)文件里,會(huì)看起來(lái)比較臃腫-->  <import resource="hibernateContext.xml"/>  </beans> 

2.在applicationContext.xm文件中原先dataSource的基礎(chǔ)上,修改其配置(數(shù)據(jù)庫(kù)名、用戶名、密碼等),(注意:value標(biāo)簽中一定不能含有空格、回車(chē)!!),如下所示:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">   <property name="jdbcUrl">   <!--如果直接用value屬性,而不用value標(biāo)簽,則需要將“&”轉(zhuǎn)義(&) ,用value標(biāo)簽,<span style="color:#FF0000;">標(biāo)簽中一定不能含有空格、回車(chē),因?yàn)樗鼤?huì)將空格轉(zhuǎn)換成" "</span>,導(dǎo)致數(shù)據(jù)庫(kù)會(huì)連接不上,除非重寫(xiě)數(shù)據(jù)源 -->   <value><![CDATA[jdbc:mysql://localhost:3306/sshdemo?useUnicode=true&characterEncoding=UTF8&useServerPrepStmts=true&prepStmtCacheSqlLimit=256&cachePrepStmts=true&prepStmtCacheSize=256&rewriteBatchedStatements=true]]></value>  </property>  <property name="driverClass" value="com.mysql.jdbc.Driver"></property>  <property name="user" value="root"></property>  <property name="password" value="root"></property>    <property name="acquireIncrement" value="3"></property>  <property name="initialPoolSize" value="10"></property>  <property name="minPoolSize" value="2"></property>  <property name="maxPoolSize" value="10"></property>  </bean> 

3.在applicationContext.xm中,配置spring的掃描器,這樣給我們的類(lèi)加上spring組件注解,就可以實(shí)現(xiàn)bean的自動(dòng)載入,具體步驟如下:(1)引入context命名空間,支持context標(biāo)簽,點(diǎn)擊底部的"Namespaces",然后勾選context那一項(xiàng)即可:


(2)配置spring掃描器:

<!-- 配置spring的掃描器,然后給我們的類(lèi)加上spring組件注解,就可以實(shí)現(xiàn)bean的自動(dòng)載入--> 
<context:component-scan base-package="com.beauxie.action,com.beauxie.service,com.beauxie.dao"> 
</context:component-scan> 

至此ssh三大框架環(huán)境搭建完畢,接下來(lái)是在ssh框架基礎(chǔ)上實(shí)現(xiàn)用戶注冊(cè)

六、案例:簡(jiǎn)單的模仿用戶注冊(cè)

1.前臺(tái)注冊(cè)頁(yè)面代碼,index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%  String path = request.getContextPath();  String basePath = request.getScheme() + "://"   + request.getServerName() + ":" + request.getServerPort()   + path + "/"; %>  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>">  <title>歡迎注冊(cè)</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!--  <link rel="stylesheet" type="text/css" href="styles.css">  --> </head>  <body>  <form action="${pageContext.request.contextPath }/user/regist" method="POST">  <!-- 也可以使用user.username自動(dòng)裝入user屬性,但在這里不是重點(diǎn),所以就在后臺(tái)手動(dòng)獲取其值-->  用戶名:<input type="text" name="username"><br> 密   碼:<input type="password" name="password"><br>  <input type="submit" value="注冊(cè)">  </form> </body> </html> 

2.User類(lèi)代碼:

package com.beauxie.bean;  import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table;  /**  * @author Beauxie  * 在這里User的屬性應(yīng)當(dāng)與t_user表中的字段相同,  * 否則就需要手動(dòng)為不相同的屬性指定對(duì)應(yīng)表中的字段  */ @Entity//映射數(shù)據(jù)庫(kù)表 @Table(name="t_user")//不加這個(gè)注解,默認(rèn)對(duì)應(yīng)的是user表 public class User {   @Id//對(duì)應(yīng)t_user表中的主鍵  private int id;//用戶ID   private String username;//用戶名   private String password;//密碼    public int getId() {  return id;  }   public void setId(int id) {  this.id = id;  }   public String getUsername() {  return username;  }   public void setUsername(String username) {  this.username = username;  }   public String getPassword() {  return password;  }   public void setPassword(String password) {  this.password = password;  }  } 

3.UserDao類(lèi)代碼:

package com.beauxie.dao;  import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.stereotype.Repository;  import com.beauxie.bean.User;  /**  * @author Beauxie  * Dao層,對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作  */ @Repository//這個(gè)屬性對(duì)應(yīng)的是持久層(一般為Dao層),說(shuō)明交給spring管理,而對(duì)應(yīng)的包下的類(lèi)名也會(huì)有一個(gè)"S" public class UserDao {   @Autowired//自動(dòng)注入,不需要設(shè)值,因?yàn)樵趕pring配置文件中已經(jīng)配置過(guò)  private HibernateTemplate template;    /**  * 用戶注冊(cè),即向表中添加一條新的記錄  * @param user  */  public void addUser(User user){  //往數(shù)據(jù)庫(kù)中添加一條數(shù)據(jù),一句話就可以搞定  template.save(user);  }  } 

4.UserService類(lèi)代碼:

package com.beauxie.service;  import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;  import com.beauxie.bean.User; import com.beauxie.dao.UserDao;  /**  * @author Beauxie  * Service層  */  @Service//這個(gè)屬性對(duì)應(yīng)的是業(yè)務(wù)層一般為Service層),說(shuō)明交給spring管理,而對(duì)應(yīng)的包下的類(lèi)名也會(huì)有一個(gè)"S" public class UserService {   @Autowired//同樣是自動(dòng)注入  private UserDao userDao;   public void addUser(User user){  //調(diào)用Dao層的addUser方法  userDao.addUser(user);  } } 

5.UserAction類(lèi)代碼:

package com.beauxie.action;  import javax.servlet.http.HttpServletRequest;  import org.apache.struts2.ServletActionContext; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller;  import com.beauxie.bean.User; import com.beauxie.service.UserService;  /**  * @author Beauxie  *  */ @Controller//用于標(biāo)注控制層組件 @Namespace("/user")//url前綴 @Scope("prototype")//Action默認(rèn)是單例,但實(shí)際開(kāi)發(fā)中,一般是多例,因?yàn)橐话阋粋€(gè)Action可能會(huì)對(duì)應(yīng)多個(gè)不同的請(qǐng)求 //@ParentPackage("struts-default")//繼承特定的package,默認(rèn)是“struts-default”,因此可以省略不寫(xiě) @Results({  @Result(name="registSuccess",location="/msg.jsp") }) public class UserAction {   @Autowired//自動(dòng)注入  private UserService service ;   //struts默認(rèn)攔截“.action以及不加任何后綴”  @Action(value="regist")//訪問(wèn):/user/regist.action 或 /user/regist  public String regist(){    //獲取request  HttpServletRequest request = ServletActionContext.getRequest();    //獲取表單提交的數(shù)據(jù)  String username = request.getParameter("username");  String password = request.getParameter("password");  //封裝userBean  User user = new User();  user.setId(1000);  user.setUsername(username);  user.setPassword(password);   //調(diào)用service層的方法,向數(shù)據(jù)庫(kù)中增加一條記錄  service.addUser(user);   //將提示信息存入request域中,用以前臺(tái)顯示  request.setAttribute("msg", "恭喜您,注冊(cè)成功!<br>注冊(cè)名:"+username);   return "registSuccess";  }  } 

6.消息提示界面:msg.jsp代碼,如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%  String path = request.getContextPath();  String basePath = request.getScheme() + "://"   + request.getServerName() + ":" + request.getServerPort()   + path + "/"; %>  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>">  <title>消息提示</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!--  <link rel="stylesheet" type="text/css" href="styles.css">  --> </head>  <body>  ${msg } </body> </html> 

7.將項(xiàng)目添加到服務(wù)器中,啟動(dòng)服務(wù),打開(kāi)瀏覽器,訪問(wèn):http://localhost/SSHDemo/user/regist

8.輸入用戶名與密碼,點(diǎn)擊“注冊(cè)”,顯示結(jié)果:

9.控制臺(tái)輸出sql語(yǔ)句(在hibernateContext.xml文件中已經(jīng)配置過(guò)輸出并美化SQL語(yǔ)句):

10.查看數(shù)據(jù)庫(kù)結(jié)果:

到此這個(gè)簡(jiǎn)單的案例就已經(jīng)結(jié)束了,關(guān)于表單提交數(shù)據(jù)校驗(yàn)、以及亂碼問(wèn)題并未涉及,后續(xù)應(yīng)該會(huì)更新吧、、、

七、總結(jié):

1.三大框架的整合,應(yīng)該先引入每個(gè)框架以后,再整合;

2.一定要記得導(dǎo)入數(shù)據(jù)庫(kù)jar包;

3.Action類(lèi)應(yīng)該要放在包名為"action"的包下,并且類(lèi)名應(yīng)當(dāng)要以Action結(jié)尾,形如“XxxAction”;

4.在配置Hibernate時(shí),一定要導(dǎo)入支持“@Entity”注解的jar包;

5.可以再struts.xml文件中定義struts攔截的請(qǐng)求類(lèi)型,默認(rèn)為.action與不加后綴

6.可以再web.xml文件中定義struts過(guò)濾器的過(guò)濾類(lèi)型,默認(rèn)為*.action,應(yīng)當(dāng)改為/*;

7.在applicationContext.xm文件中需要配置:sessionFactory、hibernate的實(shí)體類(lèi)、hibernateTemplate模板 、數(shù)據(jù)源dataSource、spring掃描器五部分(包含hibernateContext.xml);

8.各個(gè)類(lèi)中一定要加上對(duì)應(yīng)的注解,以及Action中的方法上也要加注解。

實(shí)例源碼下載:http://xiazai.VeVB.COm/201610/yuanma/SSHzhuce(VeVB.COm).rar

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 蚌埠市| 平湖市| 新河县| 安丘市| 板桥市| 衡东县| 股票| 卢氏县| 邢台市| 繁峙县| 新泰市| 仙游县| 富平县| 巴塘县| 云龙县| 左权县| 祁阳县| 辉南县| 柳江县| 河北区| 龙游县| 西丰县| 年辖:市辖区| 宜兰县| 商都县| 东平县| 大足县| 罗田县| 黑龙江省| 宁武县| 凯里市| 綦江县| 淮北市| 许昌县| 河南省| 盱眙县| 聂荣县| 呈贡县| 广德县| 浏阳市| 博客|