SSHdemo搭建

来源:互联网 发布:淘宝客服是不是店主 编辑:程序博客网 时间:2024/04/30 16:43

1.引入jar包

Struts2: 
struts2-core-2.3.15.3.jar 
asm-3.3jar 
asm-common-3.3jar 
asm-tree-3.3jar 
xwork-core-2.3.15.3.3.jar 
commons-io-2.0.1.jar 
commons-lang-3.3.1.jar 
commons-fileupload-1.3.jar 
commons-logging-1.1.3.jar 
freemarker-2.3.16.jar 
log4j-1.2.17.jar 
ognl-3.0.6.jar 
javassist-3.11.0.GA.jar

spring: 
Spring3.2 开发最基本jar包: 
spring-beans-3.2.0.RELEASE.jar 
spring-context-3.2.0.RELEASE.jar 
spring-core-3.2.0.RELEASE.jar 
spring-expression-3.2.0.RELEASE.jar 
com.springsource.org.apache.commons.logging-1.1.1.jar 
com.springsource.org.apache.log4j-1.2.15.jar 
AOP开发: 
spring-aop-3.2.0.RELEASE.jar 
spring-aspects-3.2.0.RELEASE.jar 
com.springsource.org.aopalliance-1.0.0.jar 
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar 
Spring Jdbc开发: 
spring-jdbc-3.2.0.RELEASE.jar 
spring-tx-3.2.0.RELEASE.jar 
Spring事务管理: 
spring-tx-3.2.0.RELEASE.jar 
Spring整合其他ORM框架: 
spring-orm-3.2.0.RELEASE.jar 
Spring在web中使用: 
spring-web-3.2.0.RELEASE.jar 
Spring整合Junit测试: 
spring-test-3.2.0.RELEASE.jar

hibernate: 
hibernate3.jar 
hibernate-jpa-2.0-api-1.0.1.Final.jar 
antlr-2.7.6.jar 
commons-collections-3.1.jar 
dom4j-1.6.1.jar 
slf4j-api-1.6.1.jar 
javassist-3.12.0.GA.jar 
jta-1.1.jar

2.配置

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"> <!-- 配置Spring的核心监听器 --> <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 配置Struts2的核心过滤器 --> <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>    <dispatcher>REQUEST</dispatcher>    <dispatcher>FORWARD</dispatcher> </filter-mapping>  <display-name></display-name>   <!--设置默认的访问页,访问网址http://localhost:8080/SSHdemo时可以自动跳转到login.jsp页面 -->  <welcome-file-list>    <welcome-file>login.jsp</welcome-file>  </welcome-file-list></web-app>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

注意:在webroot节点下建立一个login.jsp才能找到。

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" />    <!-- package:用来统一管理action -->     <package name="SSHdemo" extends="struts-default" namespace="/">        <!-- 用户模块的Action -->        <action name="user_*" class="userAction" method="{1}">            <result name="loginSuccess">index.jsp</result>                      </action>    </package></struts>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

注意:action标签的class属性,与spring结合后要写成spring中bean的名称name。

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.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context.xsd    http://www.springframework.org/schema/aop    http://www.springframework.org/schema/aop/spring-aop.xsd    http://www.springframework.org/schema/tx     http://www.springframework.org/schema/tx/spring-tx.xsd">    <!-- 配置连接池: -->    <!-- 引入外部属性文件 -->    <context:property-placeholder location="classpath:jdbc.properties"/>    <!-- 配置C3P0连接池: -->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="jdbcUrl" value="${jdbcUrl}"></property>        <property name="driverClass" value="${driverClass}"></property>        <property name="user" value="${user}"></property>        <property name="password" value="${password}"></property>    </bean>    <!-- Hibernate的相关信息 -->    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <!-- 注入连接池 -->        <property name="dataSource" ref="dataSource"/>        <!-- 配置Hibernate的其他的属性 -->        <property name="hibernateProperties">            <props>                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>                <prop key="hibernate.show_sql">true</prop>                <prop key="hibernate.format_sql">true</prop>                <prop key="hibernate.connection.autocommit">false</prop>                <prop key="hibernate.hbm2ddl.auto">update</prop>            </props>        </property>        <!-- 配置Hibernate的映射文件 -->        <property name="mappingResources">            <list>                <value>cn/itcast/shop/user/vo/User.hbm.xml</value>                  </list>        </property>    </bean>    <!-- 事务管理: -->    <!-- 事务管理器 -->    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory"/>    </bean>    <!-- 开启注解事务 -->    <tx:annotation-driven transaction-manager="transactionManager"/>    <!-- Action的配置 ===========================-->    <!-- 用户模块的Action -->    <bean id="userAction" class="cn.itcast.shop.user.action.UserAction" scope="prototype">        <property name="userService" ref="userService"></property>    </bean>    <!-- Service的配置  ===========================-->    <bean id="userService" class="cn.itcast.shop.user.service.UserService">        <property name="userDao" ref="userDao"/>    </bean>    <!-- Dao的配置  ===========================-->    <bean id="userDao" class="cn.itcast.shop.user.dao.UserDao">        <property name="sessionFactory" ref="sessionFactory"/>    </bean></beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76

3.登录实例编写

Action层:UserAction

/** * 用户模块Action的类 *  * @author xiao *  */public class UserAction extends ActionSupport implements ModelDriven<User> {    //模型驱动使用的对象    private User user=new User();    public User getModel(){        return user;    }    // 注入UserService    private UserService userService;    public void setUserService(UserService userService) {        this.userService = userService;    }    /**     * 登陆     */    public String login(){        User existUser=userService.login(user);        if(existUser==null){            //登陆失败            this.addActionError("登陆失败:用户名或密码错误用户未激活!");            return LOGIN;        }else {            //登陆成功            //将用户的信息存入session中            ServletActionContext.getRequest().getSession().setAttribute("existUser", existUser);            //页面跳转            return "loginSuccess";        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

service层:UserService

/*** @ClassName: UserService   * @Description: 登录用户service层 * @author 宋笑  * @date 2015-10-8 下午03:34:45   * */@Transactionalpublic class UserService {    //对UserDao的注入    private UserDao userDao;    /**     * @param userDao the userDao to set     */    public void setUserDao(UserDao userDao) {        this.userDao = userDao;    }    //登录方法    public User login(User user) {        // TODO Auto-generated method stub        return userDao.login(user);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

DAO层:UserDao

/*** @ClassName: UserDao   * @Description: 用户管理持久层   * @author 宋笑  * @date 2015-10-8 下午03:36:03   * */public class UserDao extends HibernateDaoSupport{    //用户登陆的方法    public User login(User user) {        // TODO Auto-generated method stub        String hql="from User where username=? and password=? and state=?";        List<User> list=this.getHibernateTemplate().find(hql,user.getUsername(),user.getPassword(),1);        if(list!=null&&list.size()>0){            return list.get(0);        }        return null;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

vo层:User

/*** @ClassName: User   * @Description: 用户实体层 * @author 宋笑  * @date 2015-10-8 下午03:36:33   * */public class User {    private String username;    private String password;    /**     * @return the username     */    public String getUsername() {        return username;    }    /**     * @param username the username to set     */    public void setUsername(String username) {        this.username = username;    }    /**     * @return the password     */    public String getPassword() {        return password;    }    /**     * @param password the password to set     */    public void setPassword(String password) {        this.password = password;    }}
0 0
原创粉丝点击