準備整合環境
- 數據庫環境
MySQL 數據庫中創建一個名稱爲 ssh 的數據庫,並在數據庫中創建一個名稱爲 user 的表
-
配置 Struts2 環境
1.創建項目並導入 Struts2 框架所需的 JAR 包
. 2.添加 log4j.properties 文件
3.配置 Struts2 的核心過濾器
4.添加 struts.xml 配置文件 -
配置 Spring 環境
1.導入 Spring 依賴的 JAR 包
2.添加 Spring 的核心配置文件
3.配置 Spring 的監聽器和過濾器 -
配置 Hibernate 環境
1.導入 Hibernate 所需的 JAR 包
2.添加核心配置文件 hibernate.cfg.xml
Spring 與 Hibernate 整合
- 創建實體類
- 創建映射文件
- 編寫 Spring 的配置信息
Spring 與 Struts2 整合
- 實現 Service 的配置
1.創建接口
2.創建接口實現類
3.配置 Spring - 實現 Action 的配置
1.創建 Action
2.創建和引入 Struts2 的配置文件
3.配置 Action 的 Bean 信息
SSH框架運行流程
1服務器識別web.xml,解析標籤查找.
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
2.服務器查找login.jsp,解析主頁面
<body>
<center><form method="post"action="login" >
UserName<input type="text"name="name" id="name"/><br/>
Password<input type="password"name="password" id="password"/><br/>
<input type="submit"value="login" /></form></center>
</body>
3.此時服務器解析form標籤裏的action屬性,根據action的值去Struts.xml配置文件中查找對應的action。
<action name="login"class="com.test.action.LoginAction" method="login">
<result name="login">login.jsp</result>
<result name="error">loginError.jsp</result>
</action>
4.class和method屬性,程序根據這兩個屬性鎖定後臺執行函數。進入業務處理函數,以Class LoginAction爲起點進入業務處理,返回值對應result的name屬性。
LoginAction.java:
private String password;
private UserService userService1;
public String login(){
if( Booleanu=userService1.login(name, password)
{
return"ok";
}
else{
return"error";
}
}
5.查找UserService類的login函數
public boolean login(String name, String password) {
Useru=userDAO.Getuser(name) if(u.getPassword().equals(password)){
return true;
}
else{
return false;
}
}
- 函數調用了userDAO的Getuser()函數
public User Getuser(String name){
Queryquery = sessionFactory.getCurrentSession().createQuery("from User where username=?");
query.setString(0,name);
Useru=(User)query.uniqueResult();
return u; }