您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關使用Spring如何集成和使用Hibernate與Struts,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
Spring集成所需jar包
首先,Spring集成Struts,那么applicationContext.xml和struts.xml,web.xml肯定是不能少的。前面兩個是Spring和Struts的配置文件,后面一個是整個web的全局配置文件。在每個配置文件中應該怎么配置,怎么相互關聯呢。其實就是將Struts中指定的Action 類為Spring注入的類。
三大框架集成開發并不難,難的地方在于各個包的依賴要搞清楚,版本之間的差異也是一點。下面列出Spring集成Struts所依賴的包:
依賴包
此處所有依賴為Struts2.0和Spring3.0。版本有點老,我用最新版的始終集成不正確。等搞好了再升級版本。
Number | Package | Platform | Function |
---|---|---|---|
1 | commons-fileupload-1.2.2.jar | common | 文件上傳功能 |
2 | commons-io-2.0.1.jar | common | |
3 | commons-lang-2.5.jar | common | |
4 | commons-logging-1.1.1.jar | common | 日志 |
5 | freemarker-2.3.16.jar | Struts | 模版引擎 |
6 | javassist-3.11.0.GA.jar | common | 動態編程 |
7 | ognl-3.0.1.jar | common | 表達式語言,提供屬性,方法調用 |
8 | org.springframework.asm-3.1.1.RELEASE.jar | spring | Spring獨立的asm程序,Spring2.5.6的時候需要asmJar 包3.0.6開始提供他自己獨立的asmJar。暫時我自己也不懂這事干嘛的。 |
9 | org.springframework.beans-3.1.1.RELEASE.jar | spring | Spring IOC實現 |
10 | org.springframework.context-3.1.1.RELEASE.jar | spring | Spring提供在基礎IoC功能上的擴展服務,此外還提供許多企業級服務的支持,如郵件服務、任務調度、JNDI定位、EJB集成、遠程訪問、緩存以及各種視圖層框架的封裝等 |
org.springframework.context.support-3.1.1.RELEASE.jar | spring | Spring-context的擴展支持,用于MVC方面 | |
12 | org.springframework.core-3.1.1.RELEASE.jar | spring | Spring 核心工具包 |
13 | org.springframework.expression-3.1.1.RELEASE.jar | spring | Spring表達式語言 |
14 | org.springframework.web-3.1.1.RELEASE.jar | spring | Spring Web工具包 |
15 | org.springframework.web.servlet-3.1.1.RELEASE.jar | spring | 基于servlet的MVC實現 |
16 | struts2-core-2.2.3.1.jar | struts | Struts核心庫 |
17 | xwork-core-2.2.3.1.jar | struts | xwork核心庫 |
18 | struts2-spring-plugin-2.2.3.1.jar | struts | Spring與Struts相互集成 |
19 | antlr-2.7.2.jar | common | 語言語法分析器 |
20 | aopalliance-1.0.jar | common | 面向切面編程接口 |
21 | commons-dbcp.jar | common | DBCP數據庫連接池 |
22 | commons-pool.jar | common | DBCP數據庫連接池 |
23 | dom4j-1.6.1.jar | hibernate | 靈活的xml框架 |
24 | hibernate-jpa-2.0-api-1.0.1.Final.jar | hibernate | 注解使用類 |
25 | hibernate3.jar | hibernate | 數據庫核心包 |
26 | jta-1.1.jar | hibernate | 分布式事務處理 |
27 | mysql-connector-java-5.1.18-bin.jar | hibernate | jdbc連接器 |
28 | org.springframework.jdbc-3.1.1.RELEASE.jar | hibernate | spring與jdbc集成 |
29 | org.springframework.orm-3.1.1.RELEASE.jar | hibernate | 數據庫集成 |
30 | org.springframework.transaction-3.1.1.RELEASE.jar | hibernate | 事務集成 |
31 | slf4j-api-1.6.1.jar | common | 日志系統 |
集成
model層
新建Usermodel,如下:
package com.action; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity @javax.persistence.Table(name="user") public class User implements Serializable{ private static final long serialVersionUID = 1L; @Id @GeneratedValue @Column(name="id") public int id; @Column(name="name") public String name; @Column(name="password") public String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [name=" + name + ", password=" + password + "]"; } }
dao層
新建dao接口:
package com.dao.impl; import java.util.List; import com.action.User; import com.action.UserAction; public interface UserDao { public void save(User action); public User getUser(int id); public void update(User action); public void delete(User userAction); public List<User> findByName(String name); }
實現dao接口
package com.dao.impl; import java.util.List; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate3.HibernateTemplate; import com.action.User; import com.action.UserAction; public class UserDaoImpl implements UserDao { private SessionFactory sessionFactory; private HibernateTemplate mHibernateTemplate; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public HibernateTemplate getHbernateTemplate() { if (mHibernateTemplate==null) { mHibernateTemplate = new HibernateTemplate(sessionFactory); } return mHibernateTemplate; } public void save(User action) { // TODO Auto-generated method stub getHbernateTemplate().save(action); } public User getUser(int id) { // TODO Auto-generated method stub User userAction = getHbernateTemplate().get(User.class, new Integer(id)); return userAction; } public void update(User action) { // TODO Auto-generated method stub getHbernateTemplate().update(action); } public void delete(User userAction) { // TODO Auto-generated method stub getHbernateTemplate().delete(userAction); } @SuppressWarnings("unchecked") public List<User> findByName(String name) { // TODO Auto-generated method stub String queryString = "from User u where u.name like ?"; return getHbernateTemplate().find(queryString); } }
view層
顯示以及action
/** * */ package com.action; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.dao.impl.UserDaoImpl; import com.opensymphony.xwork2.ActionSupport; /** * @author kevin * */ public class UserAction extends ActionSupport { public String name; public String password; private UserDaoImpl userDao; public String getName() { return name; } public void setUserDao(UserDaoImpl userDao) { this.userDao = userDao; } public UserDaoImpl getUserDao() { return userDao; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String execute() throws Exception { // 不能直接new 得從applicationContext中獲取 HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/xml;charset=UTF-8"); User user = new User(); user.name = name; user.password = password; userDao.save(user); response.getWriter().write(user.toString()); return "success"; } }
第一個頁面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h2>測試</h2> <s:form action="user"> <s:textfield name="name" label="username"></s:textfield> <s:textfield name="password" label="password"></s:textfield> <s:submit></s:submit> </s:form> </body> </html>
第二個頁面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h2>媽的智障</h2> ${name} ${password} </body> </html>
配置文件
添加全局web配置文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringSS</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> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
Spring配置文件
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost/spring</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>123456</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource"/> </property> <property name="annotatedClasses"> <list> <value>com.action.User</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="show_sql">true</prop> </props> </property> </bean> <bean id="userDao" class="com.dao.impl.UserDaoImpl"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> </beans>
Struts配置文件
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.objectFactory" value="spring" /> <package name="default" extends="struts-default"> <action name="user" class="userAction"> <result name="success">/user.jsp</result> </action> </package> </struts>
結果顯示
輸入頁面
結果頁面
數據庫
最后看起來,還是不難的嘛。其實UserDao可以抽象出來,只需要單次注入,等以后再完善。
看完上述內容,你們對使用Spring如何集成和使用Hibernate與Struts有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。