1导入jar包
struts2 jar包
ognl-2.6.11.jar ,struts2-core-2.0.14.jar,xwork-2.0.7.jar,commons-logging-1.0.4.jar,freemarker-2.3.8.jar,struts2-tiles-plugin-2.0.14.jar
hibernate 3.2 jar包
antlr-2.7.6.jar,commons-collections-2.1.1.jar,dom4j-1.6.1.jar,
hibernate3.jar,javassist.jar,jta.jar
spring 2.5.6 jar包
spring.jar,aspectjweaver.jar,aspectjrt.jar,cglib-nodep-2.1_3.jar
数据库连接jar包
mysql-connector-java-5.1.12-bin.jar
2 src/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> <package name="struts2" extends="struts-default"> <action name="login" class="com.test.action.LoginAction"> <result name="success">welcome.jsp</result> <result name="input">login.jsp</result> <result name="error">error.jsp</result> </action> </package> </struts>
3 web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app 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 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <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> <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>hibernateFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
4 src/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> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="hibernate.connection.url"> jdbc:mysql://localhost/test </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="hibernate.show_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="com/test/model/User.hbm.xml" /> </session-factory> </hibernate-configuration>
5 在src目录下建立三个文件
applicationContext-actions.xml
applicationContext-beans.xml
applicationContext-common.xml
applicationContext-actions.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: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/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 id="loginaction" class="com.test.action.LoginAction"> <property name="loginManager" ref="loginManager"></property> </bean> </beans>
applicationContext-beans.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: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/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 id="loginManager" class="com.test.manager.impl.HibernateDaoSupport"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
applicationContext-common.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: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/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"> <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!-- 那些类的哪些方法参与事务 --> <aop:config> <aop:advisor pointcut="execution(* com.test.manager.*.*(..))" advice-ref="txAdvice" /> </aop:config> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="modify*" propagation="REQUIRED" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> </beans>
6 LoginAction.java
package com.test.action; import com.opensymphony.xwork2.ActionSupport; import com.test.manager.LoginManager; public class LoginAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private LoginManager loginManager; private String username; private String password; public LoginManager getLoginManager() { return loginManager; } public void setLoginManager(LoginManager loginManager) { this.loginManager = loginManager; } 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; } public static long getSerialVersionUID() { return serialVersionUID; } public String execute() throws Exception { if(loginManager.isLogin(username, password)) { return SUCCESS; } return INPUT; } }
LoginManager.java
package com.test.manager; public interface LoginManager { public boolean isLogin(String username, String password); }
....................................................................
package com.test.manager.impl; import java.util.List; import com.test.manager.LoginManager; import com.test.model.User; public class HibernateDaoSupport extends org.springframework.orm.hibernate3.support.HibernateDaoSupport implements LoginManager { public boolean isLogin(String username, String password) { Object[] s = new Object[2]; s[0] = username; s[1] = password; List<User> users = this.getHibernateTemplate().find( "from User user where user.username=? and password=?", s); if (users.size() == 0) { return false; } return true; } }
7 User.java
package com.test.model; public class User { private int userid; private String username; private String password; public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } 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; } }
src/com/test/model
<?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 table="t_user" name="com.test.model.User"> <id access="field" name="userid"> <generator class="native"/> </id> <property name="username" access="field"/> <property name="password" access="field"/> </class> </hibernate-mapping>
8 sql语句:
CREATE TABLE `t_user` (
`username` varchar(10) DEFAULT NULL,
`password` varchar(10) DEFAULT NULL,
`userid` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`userid`)
)