当前位置: 移动技术网 > IT编程>开发语言>Java > Spring集成Struts与Hibernate入门详解

Spring集成Struts与Hibernate入门详解

2019年07月22日  | 移动技术网IT编程  | 我要评论

前言

最近将spring,struts,hiberbate基础已经学习完成。想自己把这三个框架集成一下,然后再写一个后台管理网站练练手。spring的作用是依赖注入,而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>
<h1>测试</h1>
<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>
<h1>妈的智障</h1>
${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></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可以抽象出来,只需要单次注入,等以后再完善。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网