ssh整合1

本文描述的是框架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

Java代码   收藏代码
  1. package com.ssh.action;import com.opensymphony.xwork2.ActionSupport;  

  2. import com.ssh.pojo.User;  

  3. import com.ssh.service.UserService;  

  4. publicclass UserAction extends ActionSupport {  

  5. privatestaticfinallong serialVersionUID = 1L;  

  6. private User user;  

  7. private UserService service;  

  8. public UserService getService() {  

  9. return service;  

  10.    }  

  11. publicvoid setService(UserService service) {  

  12. this.service = service;  

  13.    }  

  14. public User getUser() {  

  15. return user;  

  16.    }  

  17. publicvoid setUser(User user) {  

  18. this.user = user;  

  19.    }  

  20. public String execute(){  

  21. if (service.save(user)!=null) {  

  22. return SUCCESS;  

  23.        }else {  

  24. return ERROR;  

  25.        }  

  26.    }  

  27. }  


User.java


Java代码   收藏代码
  1. package com.ssh.pojo;  

  2. import javax.persistence.Column;  

  3. import javax.persistence.Entity;  

  4. import javax.persistence.GeneratedValue;  

  5. import javax.persistence.Id;  

  6. import javax.persistence.Table;  

  7. @Entity

  8. @Table(name="user")  

  9. publicclass User {  

  10. private Integer id;  

  11. private String username;  

  12. private String password;  

  13. @Id

  14. @GeneratedValue

  15. @Column(name="hr_id")  

  16. public Integer getId() {  

  17. return id;  

  18.    }  

  19. publicvoid setId(Integer id) {  

  20. this.id = id;  

  21.    }  

  22. public String getUsername() {  

  23. return username;  

  24.    }  

  25. publicvoid setUsername(String username) {  

  26. this.username = username;  

  27.    }  

  28. public String getPassword() {  

  29. return password;  

  30.    }  

  31. publicvoid setPassword(String password) {  

  32. this.password = password;  

  33.    }  

  34. }  

UserService.java


Java代码   收藏代码
  1. package com.ssh.service;  

  2. import org.hibernate.Session;  

  3. import org.hibernate.SessionFactory;  

  4. import com.ssh.pojo.User;  

  5. publicclass UserService {  

  6. private SessionFactory factory = null;  

  7. public SessionFactory getFactory() {  

  8. return factory;  

  9.    }  

  10. publicvoid setFactory(SessionFactory factory) {  

  11. this.factory = factory;  

  12.    }  

  13. public User save(User user){  

  14.        Session session = factory.getCurrentSession();  

  15.        session.beginTransaction();  

  16.        session.save(user);  

  17.        session.getTransaction().commit();  

  18. return user;  

  19.    }  

  20. }  

log4j.properties


Properties代码   收藏代码
  1. ### direct log messages to stdout ###  

  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender  

  3. log4j.appender.stdout.Target=System.out  

  4. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  

  5. log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n  

  6. ### direct messages to file hibernate.log ###  

  7. #log4j.appender.file=org.apache.log4j.FileAppender  

  8. #log4j.appender.file.File=hibernate.log  

  9. #log4j.appender.file.layout=org.apache.log4j.PatternLayout  

  10. #log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n  

  11. ### set log levels - for more verbose logging change 'info' to 'debug' ###  

  12. log4j.rootLogger=warn, stdout  

  13. #log4j.logger.org.hibernate=info  

  14. #log4j.logger.org.hibernate=debug  

  15. ### log HQL query parser activity  

  16. #log4j.logger.org.hibernate.hql.ast.AST=debug  

  17. ### log just the SQL  

  18. #log4j.logger.org.hibernate.SQL=debug  

  19. ### log JDBC bind parameters ###  

  20. #log4j.logger.org.hibernate.type=info  

  21. #log4j.logger.org.hibernate.type=debug  

  22. ### log schema export/update ###  

  23. log4j.logger.org.hibernate.tool.hbm2ddl=debug  

  24. ### log HQL parse trees  

  25. #log4j.logger.org.hibernate.hql=debug  

  26. ### log cache activity ###  

  27. #log4j.logger.org.hibernate.cache=debug  

  28. ### log transaction activity  

  29. #log4j.logger.org.hibernate.transaction=debug  

  30. ### log JDBC resource acquisition  

  31. #log4j.logger.org.hibernate.jdbc=debug  

  32. ### enable the following line if you want to track down connection ###  

  33. ### leakages when using DriverManagerConnectionProvider ###  

  34. #log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace  

struts.xml


Xml代码   收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>

  2. <!DOCTYPE struts PUBLIC  

  3.    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  

  4.    "http://struts.apache.org/dtds/struts-2.0.dtd">

  5. <struts>

  6. <constantname="struts.devMode"value="true"/>

  7. <packagename="ssh"extends="struts-default">

  8. <actionname="loginApp"class="userAction">

  9. <resultname="error">/WEB-INF/content/error.jsp</result>

  10. <resultname="success">/WEB-INF/content/success.jsp</result>

  11. </action>

  12. <actionname="*">

  13. <result>/WEB-INF/content/{1}.jsp</result>

  14. </action>

  15. </package>

  16. </struts>

error.jsp


Html代码   收藏代码
  1. <%@ page language="java"contentType="text/html; charset=UTF-8"

  2. pageEncoding="UTF-8"%>

  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

  4. <html>

  5. <head>

  6. <metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

  7. <title>出错页面</title>

  8. </head>

  9. <body>

  10. 对不起,出错了  

  11. </body>

  12. </html>

register.jsp


Html代码   收藏代码
  1. <%@ page language="java"contentType="text/html; charset=UTF-8"

  2. pageEncoding="UTF-8"%>

  3. <%@ taglib prefix="s"uri="/struts-tags" %>

  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

  5. <html>

  6. <head>

  7. <metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

  8. <title>注册页面</title>

  9. </head>

  10. <body>

  11. 欢迎注册,请在这里输入必要的注册信息:  

  12. <s:formaction="loginApp"method="post">

  13. <s:textfieldname="user.username"label="用户名"></s:textfield>

  14. <s:passwordname="user.password"label="密码"></s:password>

  15. <s:submitlabel="提交"></s:submit>

  16. </s:form>

  17. </body>

  18. </html>

success.jsp


Html代码   收藏代码
  1. <%@ page language="java"contentType="text/html; charset=UTF-8"

  2. pageEncoding="UTF-8"%>

  3. <%@ taglib prefix="s"uri="/struts-tags" %>

  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

  5. <html>

  6. <head>

  7. <metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

  8. <title>成功页面</title>

  9. </head>

  10. <body>

  11. 成功了  

  12. </body>

  13. </html>

applicationContext.xml


Xml代码   收藏代码
  1. <?xmlversion="1.0"encoding="utf-8"?>

  2. <beansxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xmlns="http://www.springframework.org/schema/beans"

  4. xsi:schemaLocation="http://www.springframework.org/schema/beans  

  5.    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

  6. <beanid="dataSource"destroy-method="close"

  7. class="org.apache.commons.dbcp.BasicDataSource">

  8. <propertyname="driverClassName"value="com.mysql.jdbc.Driver"/>

  9. <propertyname="url"value="jdbc:mysql://localhost:9906/ssh"/>

  10. <propertyname="username"value="root"/>

  11. <propertyname="password"value="root"/>

  12. </bean>

  13. <beanid="sessionFactory"

  14. class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

  15. <propertyname="dataSource"ref="dataSource"/>

  16. <propertyname="annotatedClasses">

  17. <list>

  18. <value>com.ssh.pojo.User</value>

  19. </list>

  20. </property>

  21. <propertyname="hibernateProperties">

  22. <value>

  23. hibernate.dialect=org.hibernate.dialect.MySQLDialect  

  24. hibernate.show_sql=true

  25. hibernate.hbm2ddl.auto=update

  26. hibernate.current_session_context_class=thread

  27. </value>

  28. </property>

  29. </bean>

  30. <beanid="userService"class="com.ssh.service.UserService">

  31. <propertyname="factory"ref="sessionFactory"></property>

  32. </bean>

  33. <beanid="user"class="com.ssh.pojo.User"></bean>

  34. <beanid="userAction"class="com.ssh.action.UserAction">

  35. <propertyname="user"ref="user"></property>

  36. <propertyname="service"ref="userService"></property>

  37. </bean>

  38. </beans>

web.xml


Xml代码   收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>

  2. <web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

  5. id="WebApp_ID"version="3.0">

  6. <display-name>ssh03_demo</display-name>

  7. <listener>

  8. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  9. </listener>

  10. <filter>

  11. <filter-name>struts2</filter-name>

  12. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

  13. </filter>

  14. <filter-mapping>

  15. <filter-name>struts2</filter-name>

  16. <url-pattern>/*</url-pattern>

  17. </filter-mapping>

  18. <welcome-file-list>

  19. <welcome-file>index.html</welcome-file>

  20. <welcome-file>index.jsp</welcome-file>

  21. </welcome-file-list>

  22. </web-app>

然后在Tomcat7上部署该项目即可,在浏览器中输入地址:http://localhost:8080/ssh_demo/register即可进入下面的页面:



至此,SSH框架整合完成。

相关源码和jar包放在附件中。



你可能感兴趣的:(SSH整合)