- 浏览: 1140706 次
文章分类
最新评论
-
yunyueqian:
此方法有Bug digitUppercase(587.03 ...
Java版 数字金额大写转换 -
yangcheng33:
建议还是使用lookup方法注入比较解耦
spring单例和多例详解。如何在单例中调用多例对象 -
justrun1983:
能加上文章的出处吗?这个是我博客园上2013年3月31号的原创 ...
为什么项目总是失败? -
mxbin2008:
很好的文章,必须顶
HTTP协议之状态码详解 -
lhs588:
不厚道,转发文章还删除别人的信息,也不注明来源地址:http: ...
我的权限系统设计实现MVC4 + WebAPI + EasyUI + Knockout(一)
实例:SSH整合Maven实现图片的添加与查询显示
使用SSH框架,并且结合Maven来实现图片的添加与查询显示功能。
1、整个功能的结构如图所示:在Java类中包含了action(控制层)、model(模型层)、service(接口)、serviceImpl(接口的实现类),还包括Spring的配置文件applicationContext.xml,Spring依赖注入配置文件applicationContext_bean.xml,Spring对数据库链接的配置文件applicationContext_db.xml,Struts2的配置文件struts.xml,初始化工程配置信息的web.xml,Maven的配置文件pom.xml。还包括添加图片页码inputphoto.jsp,添加图片成功页面add_success.jsp,查询图片outputphoto.jsp,显示查询出的页面out.jsp。
2、配置pom.xml文件,导入SSH的jar包。如果你没有用到Maven技术,这一步可以跳过不用看。但是你要用合适的方法导入SSh的jar包。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion><!-- 版本号,自己定义 --> <groupId>price</groupId> <!-- 组的ID --> <artifactId>photo</artifactId><!-- 此工程对应的ID,一个 groupId下面可以有多个不同名的artifactId--> <packaging>war</packaging> <!-- 有jar和war。war定义工程,定义为jar可以被其他的工程引用 --> <version>0.0.1-SNAPSHOT</version> <name>photo Maven Webapp</name> <url>http://maven.apache.org</url> <build> <finalName>photo</finalName> <!-- 工程名 --> </build> <dependencies> <!-- struts包 --> <!-- struts2核心包 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.4</version> </dependency> <!-- struts2与spring整合的包 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.3.4</version> </dependency> <!-- 在 Struts2中要使用 Ajax获得Json数据。要使用Ajax必须引用此Jar --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-json-plugin</artifactId> <version>2.3.4</version> </dependency> <!-- Hibernate包 --> <!-- Hibernate核心包 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.5.6-Final</version> </dependency> <!-- spring3可选的依赖注入,不可缺少 --> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.5.3</version> </dependency> <!-- 扩展Java类与实现Java接口 --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.1_3</version> </dependency> <!-- 运用Log4j必须用到这个包 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> <scope>compile</scope> </dependency> <!-- Spring包 --> <!-- Spring核心包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6</version> <type>jar</type> <scope>compile</scope> </dependency> <!-- Spring在WEB上的MVC框架上加上这个包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>2.5.6</version> <type>jar</type> <scope>compile</scope> </dependency> <!-- 数据源、驱动、日志、工具类包 --> <!-- log4j日志包 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> <scope>compile</scope> </dependency> <!-- jsp接口 --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <!-- JDBC连接池 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- Oracle数据库JDBC连接包 --> <dependency> <groupId>private.oracle</groupId> <artifactId>ojdbc</artifactId> <version>10.2.0.2.0</version> <scope>compile</scope> </dependency> <!-- servlet接口 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.4</version> <scope>provided</scope> </dependency> </dependencies> </project>
3、配置web.xml文件初始化工程配置信息,主要包括配置Struts2过滤器和Spring监听和定位applicationContext.xml的物理位置。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- Sttuts2过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.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> <!-- 定位applicationContext.xml的物理位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> </web-app>
4、配置applicationContext.xml的文件,指向依赖注入的Spring文件和数据库配置的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: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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<import resource="applicationContext_bean.xml"/>
<import resource="applicationContext_db.xml"/>
</beans>
5、在Oracle中表创建表Photo,包含2个字段:主键ID类型为VARCHAR2(20),存储照片的字段ZP类型为BLOB。
6、在Model中创建类Photo.java
package model; public class Photo { private String id;//ID主键 private byte[] zp;//照片 public String getId() { return id; } public void setId(String id) { this.id = id; } public byte[] getZp() { return zp; } public void setZp(byte[] zp) { this.zp = zp; } }
7、根据Photo.java产生映射文件Photo.hbm.xml文件
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2013-5-3 20:42:59 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="model.Photo" table="PHOTO"> <id name="id" type="java.lang.String"> <column name="ID" /> <generator class="assigned" /> </id> <property name="zp"> <column name="ZP" /> </property> </class> </hibernate-mapping>
8、配置applicationContext_db.xml文件以连接Oracle数据库
<?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: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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 用Bean定义数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!-- 定义数据库驱动 --> <property name="driverClass"> <value>oracle.jdbc.driver.OracleDriver</value> </property> <!-- 定义数据库URL --> <property name="jdbcUrl"> <value>jdbc:oracle:thin:@localhost:1521:orcl</value> </property> <!-- 定义数据库的用户名 --> <property name="user"> <value>lhq</value> </property> <!-- 定义数据库的密码 --> <property name="password"> <value>lhq</value> </property> <property name="minPoolSize"> <value>1</value> </property> <property name="maxPoolSize"> <value>40</value> </property> <property name="maxIdleTime"> <value>1800</value> </property> <property name="acquireIncrement"> <value>2</value> </property> <property name="maxStatements"> <value>0</value> </property> <property name="initialPoolSize"> <value>2</value> </property> <property name="idleConnectionTestPeriod"> <value>1800</value> </property> <property name="acquireRetryAttempts"> <value>30</value> </property> <property name="breakAfterAcquireFailure"> <value>true</value> </property> <property name="testConnectionOnCheckout"> <value>false</value> </property> </bean> <!--定义Hibernate的SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 定义SessionFactory必须注入dataSource --> <property name="dataSource"> <ref bean="dataSource" /> </property> <!-- 定义Hibernate的SessionFactory属性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect </prop> </props> </property> <!-- 定义POJO的映射文件 --> <property name="mappingResources"> <list> <value>model/Photo.hbm.xml</value> <!-- Hibernate的映射文件 --> </list> </property> </bean> <!-- 配置事务拦截器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" /><!-- 接口实现类的方法,命名必须以这些开头才有效 --> <tx:method name="find*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="interceptorPointCuts" expression="execution(* PhotoServiceImpl..*.*(..))" /><!-- 接口实现类的路径 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" /> </aop:config> </beans>
9、编写接口PhotoService.java
package service; import model.Photo; public interface PhotoService { // 插入照片 public void save(Photo photo); //根据ID查询照片 public Photo find(String xh); }
10、编写接口的实现类PhotoServiceImpl.java
package serviceimpl; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import service.PhotoService; import model.Photo; public class PhotoServiceImpl extends HibernateDaoSupport implements PhotoService{ public void save(Photo photo) { this.getHibernateTemplate().save(photo);//保存照片 } public Photo find(String xh) {//根据ID 获取照片信息 List list=this.getHibernateTemplate().find("from Photo where id=?",xh); if(list.size()>0) return (Photo) list.get(0); else return null; } }
<?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: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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 业务层Service --> <bean id="photoService" class="serviceimpl.PhotoServiceImpl"> <property name="sessionFactory"> <ref bean="sessionFactory"></ref> </property> </bean> <!-- 控制层Action --> <bean id="PhotoAction" class="action.PhotoAction"> <property name="photoServices"> <ref bean="photoService" /> </property> </bean> </beans>
12、编写控制层PhotoAction.java
package action; import java.io.File; import java.io.FileInputStream; import java.util.List; import java.util.Map; import javax.imageio.stream.FileImageInputStream; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import model.Photo; import org.apache.log4j.Logger; import org.apache.struts2.ServletActionContext; import service.PhotoService; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class PhotoAction extends ActionSupport { private Photo photo; private PhotoService photoServices;//接口photoServices依赖注入 photoServices的名称必须和application_bean中名称一致 private File zpfile;//文档,存放照片 //插入id和照片 public String addPhoto() throws Exception{ Photo stu=new Photo(); stu.setId(photo.getId());//ID主键 System.out.println("照片"); if(this.getZpfile()!=null){//照片处理 FileInputStream fis=new FileInputStream((File) this.getZpfile()); byte[] buffer=new byte[fis.available()]; fis.read(buffer); stu.setZp(buffer); } photoServices.save(stu);//保存信息 return SUCCESS; } //根据ID获取照片 public String findPhoto() throws Exception { String zp = photo.getId(); Photo zhaopian = photoServices.find(zp); Map request = (Map) ActionContext.getContext().get("request"); request.put("photo", zhaopian); return SUCCESS; } //获取图片 public String getImage() throws Exception { HttpServletResponse response = ServletActionContext.getResponse(); String zp = photo.getId();//获取照片Id Photo photos = photoServices.find(zp);//根据Id获取照片 byte[] img = photos.getZp();//获照片 response.setContentType("image/jpeg"); ServletOutputStream os = response.getOutputStream(); if (img != null && img.length != 0) {//输出照片 for (int i = 0; i < img.length; i++) { os.write(img[i]); } os.flush(); } return NONE; } public Object getZpfile() { return zpfile; } public void setZpfile(File zpfile) { this.zpfile = zpfile; } public Photo getPhoto() { return photo; } public void setPhoto(Photo photo) { this.photo = photo; } public PhotoService getPhotoServices() { return photoServices; } public void setPhotoServices(PhotoService photoServices) { this.photoServices = photoServices; } }
13、编写图片添加页码inputphoto.jsp
<%@page import="java.util.Date"%> <%@page import="java.text.SimpleDateFormat"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!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></title> </head> <body bgcolor="#d9dfaa"> <hr width="700" align="left"> <h3>插入照片</h3> <s:form action="addPhoto" method="post" enctype="multipart/form-data"> <table border="0" cellpadding="1" cellspacing="0"> <tr> <td><s:textfield name="photo.id" label="照片ID" value=""></s:textfield> </td> </tr> <tr> <td><s:file name="zpfile" label="照片" value=""></s:file></td> </tr> </table> <p> <input type="submit" value="添加"> <input type="reset" value="重置"> </s:form> <hr width="700" align="left"> </body> </html>
14、编程图片添加成功页码add_success.jsp
<%@page import="java.util.Date"%> <%@page import="java.text.SimpleDateFormat"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!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></title> </head> <body bgcolor="#d9dfaa"> 添加成功 </body> </html>
15、编写图片查询页码
<%@page import="java.util.Date"%> <%@page import="java.text.SimpleDateFormat"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!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></title> </head> <body bgcolor="#d9dfaa"> <hr width="700" align="left"> <h3>查询图片</h3> <s:form action="findPhoto" method="post"> <table border="0" cellpadding="1" cellspacing="0"> <tr> <td><s:textfield name="photo.id" label="照片ID" value=""></s:textfield> </td> </tr> </table> <p> <input type="submit" value="确定"> <input type="reset" value="重置"> </s:form> <hr width="700" align="left"> </body> </html>
16、编写图片现实页码out.jsp
<%@page import="java.util.Date"%> <%@page import="java.text.SimpleDateFormat"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!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></title> </head> <body bgcolor="#d9dfaa"> <h3>输出照片:</h3> <s:set name="photo" value="#request.photo"></s:set> <s:form action="photoInfo.action" method="post"> <table border="0" cellpadding="5"> <tr> <td>照片ID:</td> <td width="100"><s:property value="#photo.id" /></td> </tr> <tr> <td>照片:</td> <td rowspan="6"><img src="getImage.action?photo.id=<s:property value="#photo.id"/>" with="300" height="400"><!-- 根据情况调整照片大小 --> </td> </tr> </table> </s:form> </body> </html>
17、开启工程进行测试,输入网址:http://localhost:8080/photo/inputphoto.jsp可见到如图所示:
18、输入一个没有重复的ID主键,并且选择一张图片
19、打开对应的Oracle数据库表,找到Photo这张表,点击ZP字段
20、打开页面http://localhost:8080/photo/outputphoto.jsp 输入刚才插入的照片ID:123
21、实现功能,到此结束。
相关推荐
Maven整合SSH案例 ************************************************************************************* 注意:运行前请配置Maven环境,和修改config.properties中数据库名称用户和密码,并且修改Tuser中的...
Maven ssh整合框架Maven ssh整合框架Maven ssh整合框架Maven ssh整合框架Maven ssh整合框架
ssh+maven实现对mysql数据库的增删改查询,前提需要你配置maven环境
maven3 Spring4 SpringMVC4 Hibernate4
ssh整合demo ,structs hibernate spring 整合到一起的开发基础demo 使用了maven 开发 动态链接jar包
maven整合SSH框架,ehcache搭建,可直接运行导入运行
maven ssh实例
毕设项目:基于maven+SSM框架实现的云端汽修后台管理系统 毕设项目:基于maven+SSM框架实现的云端汽修后台管理系统 毕设项目:基于maven+SSM框架实现的云端汽修后台管理系统 毕设项目:基于maven+SSM框架实现的云端...
ssh整合所需要的jar包,ssh整合所需要的jar包,ssh整合所需要的jar包,
基于Idea开发工具,运用maven仓+SSH(struts2+spring+hibernate)整合三大框架,使用Mysql数据库的的一个简单项目demo项目实例。
Maven更新问题 今天Maven在更新的时候发现一直更新不成功,总结下解决方法。 在apache-maven-3.5.2/conf/setting.xml中加入以下配置即可解决 alimaven aliyun maven ...
ssh框架整合maven+ssh配置文件,包括hibernate详细配置,struts2的详细配置,spring的整合配置
在maven环境下,整合ssh
apache-maven的插件包:apache-maven-3.3.9-bin.zip。。
SSH整合视频,无法免费上传,共同学习,主页有更多的学习视频
SSH+maven搭建的项目
MAVEN安装包:apache-maven-3.0.5.zip
利用Maven整合SSH,包括整合说明以及实例代码,导入Eclipse可直接运行,适合初学SSH开发框架的同学
这个框架是不通过maven将spring strus2 hibernate整合到一起的 包括两个方法 显示 (product_show)和 新增(addProduct.jsp) 数据库名称mavenssh 数据库表名称 product