1. Jar包
2. web.xml配置
3. struts.xml配置
4. hibernate.cfg.xml配置
5. applicationContext.xml配置
6. log4j.properties配置
7. Dao层
8. Service层
9. Action层
1. Jar包
1) Struts 2.3.4.1
下载地址:
http://struts.apache.org/download
- /lib/commons-fileupload-1.2.2.jar
- /lib/commons-io-2.0.1.jar
- /lib/commons-lang3-3.1.jar
- /lib/freemarker-2.3.19.jar
- /lib/javassist-3.11.0.GA.jar
- /lib/ognl-3.0.5.jar
- /lib/struts2-core-2.3.4.1.jar
- /lib/struts2-spring-plugin-2.3.4.1.jar
- /lib/xwork-core-2.3.4.1.jar
2) Spring 3.1.2
下载地址:
http://www.springsource.org/download
3) Hibernate 4.1.6
下载地址:
http://sourceforge.net/projects/hibernate/files/hibernate4
- /lib/required/*
- /lib/envers/hibernate-envers-4.1.6.Final.jar
- /lib/jpa/hibernate-entitymanager-4.1.6.Final.jar
- /lib/optional/c3p0/c3p0-0.9.1.jar
- /lib/optional/c3p0/hibernate-c3p0-4.1.6.Final.jar
4) Aopalliance 1.0
下载地址:
http://sourceforge.net/projects/aopalliance
5) Aspectj 1.7.0
下载地址:
http://www.eclipse.org/aspectj/downloads.php
- aspectjrt.jar
- aspectjweaver.jar
6) Cglib 2.2.3
下载地址:
http://sourceforge.net/projects/cglib/files
7) Asm 3.3
下载地址:
http://forge.ow2.org/projects/asm
8) Log4j 1.2.17
下载地址:
http://logging.apache.org/log4j/1.2/download.html
9) MySQL Connector Java 5.1.21
下载地址:
http://dev.mysql.com/downloads/connector/j
- mysql-connector-java-5.1.21-bin.jar
10)Commons Logging 1.1.1
下载地址:
http://commons.apache.org/logging
- commons-logging-1.1.1.jar
2. web.xml配置
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app 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_3_0.xsd"
- version="3.0">
-
-
- <welcome-file-list>
- <welcome-file>login.html</welcome-file>
- </welcome-file-list>
-
-
- <context-param>
- <param-name>webAppRootKey</param-name>
- <param-value>webapp.root</param-value>
- </context-param>
-
-
- <filter>
- <filter-name>encodingFilter</filter-name>
- <filter-class>
- org.springframework.web.filter.CharacterEncodingFilter
- </filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
-
-
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
-
-
- <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>
-
-
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>classpath:log4j.properties</param-value>
- </context-param>
-
-
- <listener>
- <listener-class>
- org.springframework.web.util.Log4jConfigListener
- </listener-class>
- </listener>
-
-
- <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>
-
-
- <listener>
- <listener-class>
- org.springframework.web.context.request.RequestContextListener
- </listener-class>
- </listener>
-
-
- <listener>
- <listener-class>
- org.springframework.web.util.IntrospectorCleanupListener
- </listener-class>
- </listener>
-
- </web-app>
3. struts.xml配置
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
-
- <struts>
-
- <constant name="struts.devMode" value="false" />
- <constant name="struts.i18n.encoding" value="UTF-8" />
-
- <package name="default" namespace="/" extends="struts-default">
-
- <action name="login" method="login" class="loginAction">
- <result name="input">/login.html</result>
- <result name="success">/home.jsp</result>
- </action>
-
- </package>
-
- </struts>
4. hibernate.cfg.xml配置
- <?xml version=‘1.0‘ encoding=‘UTF-8‘?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
-
- <hibernate-configuration>
-
- <session-factory>
- <property name="dialect">
- org.hibernate.dialect.MySQLDialect
- </property>
-
- <property name="show_sql">true</property>
- <property name="format_sql">true</property>
- <property name="hbm2ddl.auto">update</property>
-
- <mapping class="com.txazo.domain.User" />
- </session-factory>
-
- </hibernate-configuration>
5. 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:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/
- spring-context-3.1.xsd">
-
-
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
- destroy-method="close">
- <property name="driverClass" value="com.mysql.jdbc.Driver" />
- <property name="jdbcUrl"
- value="jdbc:mysql://127.0.0.1:3306/txazo" />
- <property name="user" value="root" />
- <property name="password" value="root" />
- </bean>
-
-
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
- p:dataSource-ref="dataSource"
- p:configLocation="classpath:hibernate.cfg.xml" />
-
-
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate4.HibernateTransactionManager"
- p:sessionFactory-ref="sessionFactory" />
-
-
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="get*" read-only="true"></tx:method>
- <tx:method name="*"></tx:method>
- </tx:attributes>
- </tx:advice>
-
-
- <aop:config>
- <aop:pointcut id="pointcut"
- expression="
- execution(* com.txazo.service.impl.*Impl.*(..))" />
- <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
- </aop:config>
-
-
- <bean id="baseDao" class="com.txazo.dao.impl.BaseDaoImpl">
- <property name="sessionFactory">
- <ref bean="sessionFactory" />
- </property>
- </bean>
-
-
- <bean id="userService" class="com.txazo.service.impl.UserServiceImpl">
- <property name="baseDao">
- <ref bean="baseDao" />
- </property>
- </bean>
-
-
- <bean id="loginAction" class="com.txazo.action.LoginAction"
- scope="session">
- <property name="userService">
- <ref bean="userService" />
- </property>
- </bean>
-
- </beans>
6. log4j.properties配置
- # Set The RootLogger
- log4j.rootLogger=warn, console
-
- # Direct Log Messages To Console
- log4j.appender.console=org.apache.log4j.ConsoleAppender
- log4j.appender.console.Target=System.out
- log4j.appender.console.layout=org.apache.log4j.PatternLayout
- log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p %c:%L - %m%n
-
- # Log Hibernate
- log4j.logger.org.hibernate=error
-
- # Log Just The SQL
- log4j.logger.org.hibernate.SQL=debug
-
- # Log Schema Export Update
- log4j.logger.org.hibernate.tool.hbm2ddl=debug
7. Dao层
- public interface BaseDao {
-
- public <T> void save(T t);
-
- public <T> void delete(T t);
-
- public <T> void delete(Class<T> entityClass, Integer id);
-
- public <T> void update(T t);
-
- public <T> T get(Class<T> entityClass, Integer id);
-
- public <T> List<T> findAll(String hql, Class<T> entityClass);
-
- public <T> List<T> findAll(String hql, Class<T> entityClass,
- Object param);
-
- public <T> List<T> findAll(String hql, Class<T> entityClass,
- Object[] params);
-
- public <T> List<T> findByPage(final String hql, final Class<T>
- entityClass, final int firstResult, final int maxResult);
-
- public <T> List<T> findByPage(final String hql, final Class<T>
- entityClass, final Object param, final int firstResult,
- final int maxResult);
-
- public <T> List<T> findByPage(final String hql, final Class<T>
- entityClass, final Object[] params,
- final int firstResult, final int maxResult);
-
- }
- public class BaseDaoImpl implements BaseDao {
-
- private SessionFactory sessionFactory;
-
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
-
- public Session getSession() {
- return sessionFactory.getCurrentSession();
- }
-
- @Override
- public <T> void save(T t) {
- getSession().save(t);
- }
-
- @Override
- public <T> void delete(T t) {
- getSession().delete(t);
- }
-
- @Override
- public <T> void delete(Class<T> entityClass, Integer id) {
- getSession().delete(get(entityClass, id));
- }
-
- @Override
- public <T> void update(T t) {
- getSession().update(t);
- }
-
- @Override
- public <T> T get(Class<T> entityClass, Integer id) {
- return (T) getSession().get(entityClass, id);
- }
-
- @Override
- public <T> List<T> findAll(String hql, Class<T> entityClass) {
- return findAll(hql, entityClass, new Object[] {});
- }
-
- @Override
- public <T> List<T> findAll(String hql, Class<T> entityClass, Object param) {
- return findAll(hql, entityClass, new Object[] { param });
- }
-
- @Override
- public <T> List<T> findAll(String hql, Class<T> entityClass,
- Object[] params) {
- Query query = getSession().createQuery(hql);
- for (int i = 0; i < params.length; i++) {
- query.setParameter(i, params[i]);
- }
- return (List<T>) query.list();
- }
-
- @Override
- public <T> List<T> findByPage(final String hql, Class<T> entityClass,
- final int firstResult, final int maxResult) {
- return findByPage(hql, entityClass, new Object[] {}, firstResult,
- maxResult);
- }
-
- @Override
- public <T> List<T> findByPage(final String hql, Class<T> entityClass,
- final Object param, final int firstResult, final int maxResult) {
- return findByPage(hql, entityClass, new Object[] { param },
- firstResult, maxResult);
- }
-
- @Override
- public <T> List<T> findByPage(final String hql, Class<T>
- entityClass, final Object[] params, final int firstResult,
- final int maxResult) {
- Query query = getSession().createQuery(hql);
- for (int i = 0; i < params.length; i++) {
- query.setParameter(i, params[i]);
- }
- query.setFirstResult(firstResult);
- query.setMaxResults(maxResult);
- return (List<T>) query.list();
- }
-
- }
8. Service层
- public interface UserService {
-
- public User login(User user);
-
- }
- public class UserServiceImpl implements UserService {
-
- private BaseDao baseDao;
-
- public void setBaseDao(BaseDao baseDao) {
- this.baseDao = baseDao;
- }
-
- @Override
- public User login(User user) {
- List<User> list = baseDao.findAll(
- "from User where username = ? and password = ?", User.class,
- new Object[] { user.getUsername(), user.getPassword() });
- if (list.size() == 1) {
- return list.get(0);
- }
- return null;
- }
-
- }
9. Action层
- public class ActionBase extends ActionSupport implements RequestAware,
- SessionAware, ApplicationAware {
-
- private static final long serialVersionUID = 1L;
-
- protected Map<String, Object> request;
- protected Map<String, Object> session;
- protected Map<String, Object> application;
-
- public Map<String, Object> getRequest() {
- return request;
- }
-
- public Map<String, Object> getSession() {
- return session;
- }
-
- public Map<String, Object> getApplication() {
- return application;
- }
-
- @Override
- public void setRequest(Map<String, Object> request) {
- this.request = request;
- }
-
- @Override
- public void setSession(Map<String, Object> session) {
- this.session = session;
- }
-
- @Override
- public void setApplication(Map<String, Object> application) {
- this.application = application;
- }
-
- }
- public class LoginAction extends ActionBase {
-
- private static final long serialVersionUID = 1L;
-
- private String username;
- private String password;
-
- private UserService userService;
-
- public void setUserService(UserService userService) {
- this.userService = userService;
- }
-
- public String login() throws Exception {
- User user = new User(username, password);
- User login = userService.login(user);
- if (login != null) {
- session.put("user", login);
- return SUCCESS;
- }
- return INPUT;
- }
-
- 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;
- }
-
- }
相关Jar包下载如下: