最近公司要用struts2、spring3、hibernate3整合,自己弄了一套,网上有好多helloworld例子,我也记录下我的helloworld吧。
初了struts2,spring3,hibernate3包中的jar文件,还需要本人附件中的jar包进行引用,这些jar均来源各自官方网站下载。
?
WEB-INF/web.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"> <welcome-file-list> <welcome-file>searchAction</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
src/struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="com.scs.action" extends="struts-default"> <action name="TestAction" class="com.scs.action.TestAction"> <result name="success">/success.jsp</result> </action> <action name="regUserAction" class="com.scs.action.RegUserAction"> <result name="success">/success.jsp</result> <result name="failed">/regist.jsp</result> </action> <action name="searchAction" class="com.scs.action.SearchAction"> <result name="success">/search.jsp</result> </action> <action name="addAction" class="com.scs.action.AddAction"> <result name="success">/add.jsp</result> </action> </package> </struts>
src/struts.properties
struts.objectFactory = spring struts.objectFactory.spring.autoWire = name struts.i18n.encoding = utf-8 struts.multipart.maxSize=10000000 struts.devMode = true struts.enable.DynamicMethodInvocation = false
src/log4j.properties
log4j.rootLogger=WARN, Console log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=(%r ms) [%t] %-5p: %c#%M %x: %m%n log4j.logger.com.genuitec.eclipse.sqlexplorer=DEBUG log4j.logger.org.apache=INFO log4j.logger.org.hibernate=INFO
src/applicationContext.xml
<?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 --> <context:annotation-config /> <context:component-scan base-package="com.scs" /> <!-- 数据库连接池--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/TestSCS" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <!-- hibernate sessionFactory 创建 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mappingResources"> <list> <value>com/scs/pojo/TestUser.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 事物配置 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="find*" read-only="true" /> <tx:method name="add*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <bean id="regUserAction" class="com.scs.action.RegUserAction"> <property name="testUserDao" ref="testUserDao"></property> </bean> <bean id="searchAction" class="com.scs.action.SearchAction"> <property name="testUserDao" ref="testUserDao"></property> </bean> <bean id="testUserDao" class="com.scs.dao.impl.TestUserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
com/scs/pojo/TestUser.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"> <hibernate-mapping package="com.scs.pojo"> <class name="TestUser" table="TEST_USER"> <id name="id" column="ID"> <generator class="identity"/> </id> <property name="name" type="java.lang.String" insert="true" column="NAME"></property> </class> </hibernate-mapping>
com.scs.pojo.TestUser
package com.scs.pojo; import java.io.Serializable; public class TestUser implements Serializable { private static final long serialVersionUID = 1L; 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; } }
com.scs.dao.TestUserDao
package com.scs.dao; import java.util.List; import com.scs.pojo.TestUser; public interface TestUserDao { // 创建用户 public Integer save(TestUser user); // 查询用户 public List<TestUser> search(); }
com.scs.dao.impl.TestUserDaoImpl
package com.scs.dao.impl; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.scs.dao.TestUserDao; import com.scs.pojo.TestUser; public class TestUserDaoImpl extends HibernateDaoSupport implements TestUserDao { @Override public Integer save(TestUser user) { return (Integer) getHibernateTemplate().save(user); } @SuppressWarnings("unchecked") @Override public List<TestUser> search() { return getHibernateTemplate().find("from TestUser"); } }
com.scs.action.AddAction
package com.scs.action; import com.opensymphony.xwork2.ActionSupport; public class AddAction extends ActionSupport{ private static final long serialVersionUID = 1L; @Override public String execute() throws Exception { return "success"; } }
com.scs.action.RegUserAction
package com.scs.action; import com.opensymphony.xwork2.ActionSupport; import com.scs.dao.TestUserDao; import com.scs.pojo.TestUser; public class RegUserAction extends ActionSupport { private static final long serialVersionUID = 1L; private TestUserDao testUserDao; private TestUser testUser; public TestUser getTestUser() { return testUser; } public void setTestUser(TestUser testUser) { this.testUser = testUser; } public TestUserDao getTestUserDao() { return testUserDao; } public void setTestUserDao(TestUserDao testUserDao) { this.testUserDao = testUserDao; } public String execute() { testUserDao.save(testUser); return "success"; } }
com.scs.action.SearchAction
package com.scs.action; import java.util.List; import com.opensymphony.xwork2.ActionSupport; import com.scs.dao.TestUserDao; import com.scs.pojo.TestUser; public class SearchAction extends ActionSupport { private static final long serialVersionUID = 1L; private TestUserDao testUserDao; private List<TestUser> users; public TestUserDao getTestUserDao() { return testUserDao; } public void setTestUserDao(TestUserDao testUserDao) { this.testUserDao = testUserDao; } public List<TestUser> getUsers() { return users; } public void setUsers(List<TestUser> users) { this.users = users; } public String execute() { users = testUserDao.search(); return "success"; } }
WebRoot/add.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="struts2,spring3,hibernate3"> <%@ taglib prefix="s" uri="/struts-tags"%> </head> <body> This is my JSP page. <br><br> <s:form action="regUserAction" method="get" name="testUser" > <table width="230" border="0" align="left" height="35"> <tbody> <tr> <td>姓名:</td> <td><input type="text" name="testUser.name" /></td></tr> <tr align="center"> <td colspan="2"><input type="submit" name="submit" value="提交" /><input type="reset" value="重置" /></td> </tr> </tbody></table> </s:form> </body> </html>
WebRoot/search.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="struts2,spring3,hibernate3"> </head> <body> <table width="230" border="1" align="left" height="35"> <tr align="center"> <td>ID</td> <td>姓名</td> </tr> <s:iterator value="users"> <tr> <td><s:property value="id" /></td> <td><s:property value="name" /></td> </tr> </s:iterator> </table> <dir> <s:a href="addAction" >增加</s:a> </dir> </body> </html>
?
?
1 楼
mtain
2011-11-14
不错
2 楼
sc_1028
2011-11-15
mtain 写道
不错
谢谢支持