本文以给出了一个完整的SSH(Struts+Spring+Hibernat)框架配置方案。(附源文件)
本文的读者:适合刚刚学习SSH、想体验一下SSH和想用SSH做点事情的朋友。
你需要的知识:会eclipse的基本操作、tomcat的配置,如果不会你可能犯了“躁进”的毛病,先学基础吧(建议你读一下《学习原来是这样的》)。当然你要有一定的Struts、Spring、Hibernate知识,至少知道struts-config.xml、hibernate.cfg.xml、ApplicationContext.xml是干什么的。 因为要配置的文件比较多,为了给读者一个直观印象,在正式开始前,我先给出配置的路线图。
图中按照hello.do请求的处理顺序,描述了6个配置文件之间的依赖关系。下面我们将一个实际的例子,依次来讲述各个文件的配置。
例子SSHDemo演示了又前台传入请求hello.do,请求,返回一条由数据库中取出的数据。
第一步、建立项目
1.用MyEclipse建立SSHDemo项目。你至少需要下面的jar文件:
1.Struts.jar 2.commons-beanutils.jar 3.commons-digester.jar
4. commons-collections.jar 5. commons-logging.jar 6.Hibernate3.jar
7. antlr-2.7.6.jar 8.asm.jar 9.cglib-2.1.1.jar
10. dom4j-1.6.1.jar 11.dom4j-1.6.1.jar 12. ehcache-1.2.jar 13.jta.jar
14. log4j-1.2.11.jar 15. mysql-connector-java-5.0.0-beta-bin.jar 16. spring.jar
这些文件你可以在spring的下载包中找到。把上面的jar文件加入classPath中,并拷贝到WEB-INF中的lib中。
第二步、建立相关文件,包括类文件,jsp文件等
1 1.建立hello.jsp 用来显示由数据库传来的信息
hello.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>Struts Demo</title>
</head>
<body>
<H1>Hell, ${userInfo["username"]} !</H1>
</body>
</html>
2. 建立数据库表
create table us (id int(10) primary key auto_increment, name varchar(20))type=innodb;
insert into us (name) values("zhaoyuan");
3.创建User.java
package edu.bjtu.zhao.ssh;
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4. 创建UserDAO.java
package edu.bjtu.zhao.ssh;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class UserDAO {
private SessionFactory sessionFactory;
public UserDAO() {
}
public UserDAO(SessionFactory sessionFactory) {
this.setSessionFactory(sessionFactory);
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public User find(Integer id) {
Session session = sessionFactory.openSession();
User user = (User) session.load(User.class, id);
Hibernate.initialize(user);
session.close();
return user;
}
}
5.创建HalloAction.java
package edu.bjtu.zhao.ssh;
import java.util.*;
import javax.servlet.http.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloAction extends Action {
// private UserChecker userChecker;
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String username = request.getParameter("user");
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//建立DAO对象
UserDAO userDAO = (UserDAO) context.getBean("userDAO");
User user = new User();
user = userDAO.find(new Integer(1));
if(username==null )
{
username=user.getName();
}
Map model = new HashMap();
model.put("username", username);
request.setAttribute("userInfo", model);
return mapping.findForward("helloUser");
}
}
第三步,开始配置路线图中的6个xml文件(你可以参考文章的路线图)
1. 配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee [url]http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd[/url]">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<!-- Standard Action Servlet Configuration -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!—配置Spring的 applicationContext.xml-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
</web-app>
2. 配置struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<action-mappings>
<action
path="/hello"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward
name="helloUser"
path="/WEB-INF/jsp/hello.jsp"/>
</action>
</action-mappings>
<!-- 因为使用了代理类,DelegatingActionProxy,注册一个插件,让它通过/Hello去寻找真正的实现类 -->
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/action-servlet.xml"/>
</plug-in>
</struts-config>
3.配置ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="userDAO" class="edu.bjtu.zhao.ssh.UserDAO">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>hibernate.cfg.xml</value>
</property>
</bean>
</beans>
4. 配置action-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 与struts-config.xml里的"/hello"对应,由代理类返回HelloActoine类
到此spring 与struts配置完毕,也就是还说spring只是管理了action
-->
<bean name="/hello"
class="edu.bjtu.zhao.ssh.HelloAction">
</bean>
</beans>
5. 配置hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--是否使运行时生成的sql输出到控制台以供调试 -->
<property name="show_sql">true</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/sample
</property>
<property name="connection.username">root</property>
<property name="connection.password">
</property>
<!-- 以下设置对象与数据库表格的映射文件(所用用到的hbm文件在此配置),必须是相对于根的全路径 -->
<mapping resource="edu/bjtu/zhao/ssh/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6. 创建映射文件User.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="edu.bjtu.zhao.ssh.User"
table="user">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
</class>
</hibernate-mapping>
第四步,测试
1. 打开TOMCAT_HOME/conf/server.xml,在<Host>下配置
<Context path="/SSHDemo" docBase="D:\j2ee\workspace\SSHDemo\WebRoot" reloadable="true" debug="0">
</Context>
2. 启动TOMCAT,
在IE中输入[url]http://localhost:8080/SSHDemo/hello.do[/url]
将显示“Hello zhaoyuan”
在IE中输入[url]http://localhost:8080/SSHDemo/hello.do?user=yourname[/url]
将显示“hello yourname”;
以上程序在
tomcat-5.5.17,jdk1.5.0_06 MySQLversion: 4.0.14-nt 下测试成功。
看到输出的结果,说明配置成功,你拥有了自己的SSH开发框架。这只是一个入门级别的框架配置,用来做个Demo还可以,如果用它来做企业级开发,我基本上可以断言不能。因为至少连接池没配,再说用SSH这种东西来开发,虽然网上炒的比较热,但在具体的项目中还是要慎用。
附件:http://down.51cto.com/data/2348632
©著作权归作者所有:来自51CTO博客作者jyz608的原创作品,如需转载,请与作者联系,否则将追究法律责任
配置StrutsSpringSpring