SSH-使用注解整合实现简单的登录功能

SSH架构

这里写图片描述


实现思路

1. 添加3个框架所需的jar包
2. 在spring中配置数据源对象和回话工厂
3. 实现并配置DAO
4. 实现并配置Service
5. 为业务层添加事务管理
6. 实现并配置Action
7. 创建JSP测试页面


1.导入基本jar包

链接:http://pan.baidu.com/s/1eSy9SEA 密码:b1b1

2.配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
    <!--扫描注解  -->
    <context:component-scan base-package="cn"></context:component-scan>
    <!--配置数据源  -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
        <property name="url" value="jdbc:oracle:thin:@192.168.208.1:1521:orcl"></property>
        <property name="username" value="bdqn"></property>
        <property name="password" value="bdqn"></property>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <!-- 方法二 -->
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="show_sql">true</prop>
                <prop key="format_sql">true</prop>
                <prop key="javax.persistence.validation.mode">none</prop>
            </props>
        </property>
        <property name="packagesToScan" value="cn.pojo"></property>
    </bean>

    <!-- 事务管理器 -->
    <bean id="txManager" 
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <tx:annotation-driven transaction-manager="txManager"/>

</beans>

3.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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">
    <display-name></display-name>
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>

    <!-- 指定Spring配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- 配置监听器启动Spring -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <!-- 延迟加载 -->
    <filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>sessionFactoryBeanName</param-name>
            <param-value>sessionFactory</param-value>
        </init-param>

    </filter>

    <filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>*.action</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>
</web-app>

4.配置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.enable.DynamicMethodInvocation" value="false" /> -->

    <!-- 
        开发模式(devMode);此时 DevMode=ture;
                     产品模式(proMode);此时  DevMode=false;
                     在devMode被激活的模式下,能够明显的提高开发效率,
                     它会提供更多的日志或着debug信息。当然提高开发效率,在性能方面会付出一定的代价。
                     所以struts默认的是非开发模式。设置为开发模式之后:
     -->
<!--    <constant name="struts.devMode" value="true" />  -->

    <!--是指定response中返回流的编码方式  -->
    <!-- <constant name="struts.i18n.encoding" value="utf-8" />  -->

    <!--掌控整个项目所上传文件的最大的Size  -->
<!--    <constant name="struts.multipart.maxSize" value="5000000"/> -->

    <package name="default" namespace="/" extends="struts-default">
        <action name="login" class="SysEmployeeAction" method="login">
            <result name="login">/index.jsp</result>
            <result name="input">/login.jsp</result>
        </action>

    </package>
</struts>

5.编写实体层
可以用DB Browser工具自动生成

package cn.pojo;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * SysEmployee entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "SYS_EMPLOYEE", schema = "BDQN")
public class SysEmployee implements java.io.Serializable {

    // Fields

    private String sn;
    private SysDepartment sysDepartment;
    private Long positionId;
    private String name;
    private String password;
    private String status;
    private Set<BizClaimVoucher> bizClaimVouchersForNextDealSn = new HashSet<BizClaimVoucher>(
            0);
    private Set<BizClaimVoucher> bizClaimVouchersForCreateSn = new HashSet<BizClaimVoucher>(
            0);

    // Constructors

    /** default constructor */
    public SysEmployee() {
    }

    /** minimal constructor */
    public SysEmployee(String sn, SysDepartment sysDepartment, Long positionId,
            String name, String password, String status) {
        this.sn = sn;
        this.sysDepartment = sysDepartment;
        this.positionId = positionId;
        this.name = name;
        this.password = password;
        this.status = status;
    }

    /** full constructor */
    public SysEmployee(String sn, SysDepartment sysDepartment, Long positionId,
            String name, String password, String status,
            Set<BizClaimVoucher> bizClaimVouchersForNextDealSn,
            Set<BizClaimVoucher> bizClaimVouchersForCreateSn) {
        this.sn = sn;
        this.sysDepartment = sysDepartment;
        this.positionId = positionId;
        this.name = name;
        this.password = password;
        this.status = status;
        this.bizClaimVouchersForNextDealSn = bizClaimVouchersForNextDealSn;
        this.bizClaimVouchersForCreateSn = bizClaimVouchersForCreateSn;
    }

    // Property accessors
    @Id
    @Column(name = "SN", unique = true, nullable = false, length = 50)
    public String getSn() {
        return this.sn;
    }

    public void setSn(String sn) {
        this.sn = sn;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "DEPARTMENT_ID", nullable = false)
    public SysDepartment getSysDepartment() {
        return this.sysDepartment;
    }

    public void setSysDepartment(SysDepartment sysDepartment) {
        this.sysDepartment = sysDepartment;
    }

    @Column(name = "POSITION_ID", nullable = false, precision = 10, scale = 0)
    public Long getPositionId() {
        return this.positionId;
    }

    public void setPositionId(Long positionId) {
        this.positionId = positionId;
    }

    @Column(name = "NAME", nullable = false, length = 50)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "PASSWORD", nullable = false, length = 50)
    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column(name = "STATUS", nullable = false, length = 20)
    public String getStatus() {
        return this.status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "sysEmployeeByNextDealSn")
    public Set<BizClaimVoucher> getBizClaimVouchersForNextDealSn() {
        return this.bizClaimVouchersForNextDealSn;
    }

    public void setBizClaimVouchersForNextDealSn(
            Set<BizClaimVoucher> bizClaimVouchersForNextDealSn) {
        this.bizClaimVouchersForNextDealSn = bizClaimVouchersForNextDealSn;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "sysEmployeeByCreateSn")
    public Set<BizClaimVoucher> getBizClaimVouchersForCreateSn() {
        return this.bizClaimVouchersForCreateSn;
    }

    public void setBizClaimVouchersForCreateSn(
            Set<BizClaimVoucher> bizClaimVouchersForCreateSn) {
        this.bizClaimVouchersForCreateSn = bizClaimVouchersForCreateSn;
    }

}

6.编写daoImpl

package cn.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import cn.pojo.SysEmployee;

@Repository("SysEmployeeDao")
public class SysEmployeeDaoImpl extends HibernateDaoSupport implements SysEmployeeDao  {

public SysEmployeeDaoImpl(){

    }

    @Autowired
    public SysEmployeeDaoImpl(
            @Qualifier("sessionFactory")
            SessionFactory sessionFactory){
        this.setSessionFactory(sessionFactory);

    }

    @Override
    public SysEmployee getSysEmployee(String sn, String password) {
        // TODO Auto-generated method stub
        List<SysEmployee> emps = this.getHibernateTemplate()
                                    .find("from SysEmployee where sn=? and password=?",sn,password);
        if(emps.size()>0)
            return emps.get(0);
        else
            return null;
    }

}

7.编写ServiceImpl

package cn.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.dao.SysEmployeeDao;
import cn.pojo.SysEmployee;
@Service("SysEmployeeService")
@Transactional
public class SysEmployeeServiceImpl implements SysEmployeeService{
    @Resource
    private SysEmployeeDao empDao;



    @Override
    public SysEmployee login(String sn, String password) {
        // TODO Auto-generated method stub
        return empDao.getSysEmployee(sn, password);
    }

}

8.编写action

package cn.action;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;



import cn.pojo.SysEmployee;
import cn.service.SysEmployeeService;

@Controller("SysEmployeeAction")
public class SysEmployeeAction{

    @Resource
    private SysEmployeeService empService;

    private String sn;
    private String password;



    public SysEmployeeService getEmpService() {
        return empService;
    }
    public void setEmpService(SysEmployeeService empService) {
        this.empService = empService;
    }
    public String getSn() {
        return sn;
    }
    public void setSn(String sn) {
        this.sn = sn;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public String login(){
        SysEmployee emp =empService.login(sn, password);
        Map<String, Object> map =  ActionContext.getContext().getSession();
        String name = emp.getName();
        map.put("emp", emp);
        if(null!=emp){
            return "login";
        }else{
            return "input";
        }

    }

}

10.基本搭建
这里写图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值