?
本文描述的是框架SSH集成的示例,由于在这个过程中有一些小的细节容易被遗忘,特别撰写了一篇小的博文来记录这个过程,希望对自己以及后来者能够起到积极意义。
本文中使用的框架和版本号为:
struts-2.3.1.2、hibernate-3.6.10、spring-3.1.1
其下载地址分别为:
http://struts.apache.org/download.cgi#struts2312
http://sourceforge.net/projects/hibernate/files/hibernate3/3.6.10.Final/
http://www.springsource.org/download/community?project=Spring%2520Framework&version=3.1.1.RELEASE
分别将其解压,需要注意的是本例中hibernate3还需要的是log4j,spring还需要commons-loging、commons-dbcp和commons-pool,在这里就不再赘述出其下载地址了。
需要删除的重复包为:asm-commons-3.3.jar和javassist-3.11.0.GA.jar
需要使用的JAR包截图:
?接下来在jee中新建一个Dynamic Web Project,选择好运行环境:
?相关文件组织如下:
?源代码列举如下:
UserAction.java
package com.ssh.action;import com.opensymphony.xwork2.ActionSupport; import com.ssh.pojo.User; import com.ssh.service.UserService; public class UserAction extends ActionSupport { private static final long serialVersionUID = 1L; private User user; private UserService service; public UserService getService() { return service; } public void setService(UserService service) { this.service = service; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String execute(){ if (service.save(user)!=null) { return SUCCESS; }else { return ERROR; } } }
?
?User.java
?
package com.ssh.pojo; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="user") public class User { private Integer id; private String username; private String password; @Id @GeneratedValue @Column(name="hr_id") public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
?UserService.java
?
package com.ssh.service; import org.hibernate.Session; import org.hibernate.SessionFactory; import com.ssh.pojo.User; public class UserService { private SessionFactory factory = null; public SessionFactory getFactory() { return factory; } public void setFactory(SessionFactory factory) { this.factory = factory; } public User save(User user){ Session session = factory.getCurrentSession(); session.beginTransaction(); session.save(user); session.getTransaction().commit(); return user; } }
?log4j.properties
?
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file hibernate.log ### #log4j.appender.file=org.apache.log4j.FileAppender #log4j.appender.file.File=hibernate.log #log4j.appender.file.layout=org.apache.log4j.PatternLayout #log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### log4j.rootLogger=warn, stdout #log4j.logger.org.hibernate=info #log4j.logger.org.hibernate=debug ### log HQL query parser activity #log4j.logger.org.hibernate.hql.ast.AST=debug ### log just the SQL #log4j.logger.org.hibernate.SQL=debug ### log JDBC bind parameters ### #log4j.logger.org.hibernate.type=info #log4j.logger.org.hibernate.type=debug ### log schema export/update ### log4j.logger.org.hibernate.tool.hbm2ddl=debug ### log HQL parse trees #log4j.logger.org.hibernate.hql=debug ### log cache activity ### #log4j.logger.org.hibernate.cache=debug ### log transaction activity #log4j.logger.org.hibernate.transaction=debug ### log JDBC resource acquisition #log4j.logger.org.hibernate.jdbc=debug ### enable the following line if you want to track down connection ### ### leakages when using DriverManagerConnectionProvider ### #log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
?struts.xml
?
<?xml version="1.0" encoding="UTF-8" ?> <!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.devMode" value="true" /> <package name="ssh" extends="struts-default"> <action name="loginApp" class="userAction"> <result name="error">/WEB-INF/content/error.jsp</result> <result name="success">/WEB-INF/content/success.jsp</result> </action> <action name="*"> <result>/WEB-INF/content/{1}.jsp</result> </action> </package> </struts>
?error.jsp
?
<%@ 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>出错页面</title> </head> <body> 对不起,出错了 </body> </html>
?register.jsp
?
<%@ 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>注册页面</title> </head> <body> 欢迎注册,请在这里输入必要的注册信息: <s:form action="loginApp" method="post"> <s:textfield name="user.username" label="用户名"></s:textfield> <s:password name="user.password" label="密码"></s:password> <s:submit label="提交"></s:submit> </s:form> </body> </html>
?success.jsp
?
<%@ 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>成功页面</title> </head> <body> 成功了 </body> </html>
?applicationContext.xml
?
<?xml version="1.0" encoding="utf-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:9906/ssh" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="annotatedClasses"> <list> <value>com.ssh.pojo.User</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.show_sql=true hibernate.hbm2ddl.auto=update hibernate.current_session_context_class=thread </value> </property> </bean> <bean id="userService" class="com.ssh.service.UserService"> <property name="factory" ref="sessionFactory"></property> </bean> <bean id="user" class="com.ssh.pojo.User"></bean> <bean id="userAction" class="com.ssh.action.UserAction"> <property name="user" ref="user"></property> <property name="service" ref="userService"></property> </bean> </beans>
?web.xml
?
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>ssh03_demo</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
?然后在Tomcat7上部署该项目即可,在浏览器中输入地址:http://localhost:8080/ssh_demo/register即可进入下面的页面:
?至此,SSH框架整合完成。
相关源码和jar包放在附件中。
?