整合SSH框架实现简单登录

SSH整合的大体结构:我们将Struts2和hibernate交给spring来管理

  整合SSH框架实现简单登录_第1张图片

 

创建好web项目之后,首先当然是先引入需要的pom节点,需要的pom的节点可以在Maven官方仓库中下载https://mvnrepository.com/

  当然以下是我自己准备好的一份pom节点,复制进去下载完即可使用

  

 <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.11version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-coreartifactId>
            <version>3.5.1-Finalversion>
        dependency>
        
        <dependency>
            <groupId>javax.transactiongroupId>
            <artifactId>jtaartifactId>
            <version>1.1version>
        dependency>
        
        <dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-annotationsartifactId>
            <version>3.4.0.GAversion>
        dependency>
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-nopartifactId>
            <version>1.6.4version>
        dependency>
        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>1.7.25version>
        dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistencegroupId>
            <artifactId>hibernate-jpa-2.0-apiartifactId>
            <version>1.0.1.Finalversion>
        dependency>
        
        <dependency>
            <groupId>net.sf.ehcachegroupId>
            <artifactId>ehcacheartifactId>
            <version>1.2.3version>
        dependency>
        <dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-ehcacheartifactId>
            <version>3.6.10.Finalversion>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>4.2.0.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId> org.aspectjgroupId >
            <artifactId> aspectjweaverartifactId >
            <version> 1.8.7version >
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>4.2.0.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
            <scope>runtimescope>
        dependency>
        
        
        <dependency>
            <groupId>javaxgroupId>
            <artifactId>javaee-apiartifactId>
            <version>6.0version>
            <scope>providedscope>
        dependency>
        
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.1.6version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>4.2.0.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-ormartifactId>
            <version> 4.2.0.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>4.2.0.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.apache.strutsgroupId>
            <artifactId>struts2-convention-pluginartifactId>
            <version>2.3.24version>
            <exclusions>
                <exclusion>
                    <artifactId>commons-lang3artifactId>
                    <groupId>org.apache.commonsgroupId>
                exclusion>
            exclusions>
        dependency>
        
        <dependency>
            <groupId>org.apache.strutsgroupId>
            <artifactId>struts2-spring-pluginartifactId>
            <version>2.3.24version>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>3.1.0version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>asmgroupId>
            <artifactId>asm-commonsartifactId>
            <version>3.3version>
        dependency>
        <dependency>
            <groupId>commons-fileuploadgroupId>
            <artifactId>commons-fileuploadartifactId>
            <version>1.3.1version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.39version>
        dependency>
        <dependency>
            <groupId>commons-iogroupId>
            <artifactId>commons-ioartifactId>
            <version>2.4version>
        dependency>
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-lang3artifactId>
            <version>3.3.1version>
        dependency>
        <dependency>
            <groupId>commons-logginggroupId>
            <artifactId>commons-logging-apiartifactId>
            <version>1.1version>
        dependency>
        <dependency>
            <groupId>org.freemarkergroupId>
            <artifactId>freemarkerartifactId>
            <version>2.3.23version>
        dependency>
        <dependency>
            <groupId>javassistgroupId>
            <artifactId>javassistartifactId>
            <version>3.11.0.GAversion>
        dependency>
        <dependency>
            <groupId>ognlgroupId>
            <artifactId>ognlartifactId>
            <version>3.0.6version>
        dependency>
        <dependency>
            <groupId>org.apache.strutsgroupId>
            <artifactId>struts2-coreartifactId>
            <version>2.3.24version>
        dependency>
        <dependency>
            <groupId>org.apache.struts.xworkgroupId>
            <artifactId>xwork-coreartifactId>
            <version>2.3.24version>
        dependency>
    dependencies>
  <build>
      <resources>
          <resource>
              <directory>src/main/resourcesdirectory>
          resource>
          <resource>
              <directory>src/main/javadirectory>
              <includes>
                  <include>**/*.xmlinclude>
              includes>
          resource>
      resources>
  build>

在src下创建resource目录,并在创建好的目录中创建相应的XML文件

  整合SSH框架实现简单登录_第2张图片

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:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/cache
       http://www.springframework.org/schema/cache/spring-cache.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="cn.ssh"/>
    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql:///hibernate"/>
        <property name="username" value="root"/>
        <property name="password" value="0000"/>
    bean>
    
     <bean id="sessionFactory"
           class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
         
         
         <property name="dataSource" ref="dataSource">property>
         
         <property name="hibernateProperties">
             <props>
                 <prop key="show_sql">trueprop>
                 <prop key="format_sql">trueprop>
                 <prop key="hbm2ddl.auto">updateprop>
             props>
         property>
         
         <property name="mappingResources">
             <list>
                 <value>cn/ssh/pojo/User.hbm.xmlvalue>
             list>
         property>
     bean>
    
    <bean id="userDao" class="cn.ssh.dao.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    bean>

    
    <bean id="userService" class="cn.ssh.service.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    bean>

     
    <bean id="userAction" class="cn.ssh.action.UserAction">
        <property name="userService" ref="userService">property>
    bean>


    
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    bean>
    
    <tx:advice id="txAdvice" transaction-manager="txManager">
        
        <tx:attributes>
            <tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>
        tx:attributes>
    tx:advice>
    
    <aop:config>
        
        <aop:pointcut id="serviceMethod" expression="execution(* *..*.service..*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
    aop:config>
beans>

hibernate.cfg.xml(当applicationtext.xml中出现Hibernate相关配置时,就可以不需要此文件。。当然此时的applicationtext.xml中文件是存在Hibernate配置)

 

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="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
    <property name="hibernate.connection.url">jdbc:mysql:///hibernateproperty>
    <property name="hibernate.connection.username">rootproperty>
    <property name="hibernate.connection.password">0000property>
    
    <property name="show_sql">trueproperty>
    
    <property name="format_sql">trueproperty>

    <property name="hbm2ddl.auto">updateproperty>
    <property name="current_session_context_class">threadproperty>
    
    <mapping resource="cn/ssh/pojo/User.hbm.xml">mapping>
session-factory>
hibernate-configuration>

至于Struts.xml文件我们等到需要的时候在进行配置

web.xml(需要修改web.xml的头部)

xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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">

  <display-name>Archetype Created Web Applicationdisplay-name>
  
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:applicationContext.xmlparam-value>
  context-param>

  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  
  <filter>
    <filter-name>OpenSessionInViewFilterfilter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilterfilter-class>
  filter>
  <filter-mapping>
    <filter-name>OpenSessionInViewFilterfilter-name>
    <url-pattern>*.actionurl-pattern>
  filter-mapping>

  <filter>
    <filter-name>struts2filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
  filter>
  <filter-mapping>
    <filter-name>struts2filter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>
  
指定项目启动后的首页面,如果不是index.jsp页面需要添加 <welcome-file-list> <welcome-file>login.jspwelcome-file> welcome-file-list> web-app>

 

 

创建Java包,创建实体类以及相关的xml文件

  整合SSH框架实现简单登录_第3张图片

Pojo层

package cn.ssh.pojo;


import javax.persistence.*;

public class User {

  private Integer id;

  private String name;

  private String password;

  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;
  }


  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }
}
xml version="1.0" encoding="UTF-8"?>
DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.ssh.pojo">
    <class name="User" table="`User`">
        <id name="id" type="java.lang.Integer" >
            <generator class="identity"/>
        id>
        <property name="name" type="java.lang.String" />
        <property name="password" type="java.lang.String">
            <column name="`password`">column>
        property>
    class>
hibernate-mapping>

dao层

 

public interface UserDao {
   public  User findUser(String name,String password);
}

 在实现类中我们继承HibernateDaoSupport类,这个类主要给我们提供了一些增删改的方法

package cn.ssh.dao;

import cn.ssh.pojo.User;
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 java.util.List;

/**
 * @author: wmj
 * @Data: 2018-10-30 14:44
 **/

public class UserDaoImpl extends HibernateDaoSupport implements  UserDao {
    @Override
    public User findUser(String name, String password) {
        List users = (List) this.getHibernateTemplate()
                .find("from User u WHERE u.name=? AND u.password=?", name, password);
        if (users.size()>0){
            return  users.get(0);
        }else{
            return null;
        }
    }
}

service层

  

整合SSH框架实现简单登录_第4张图片

整合SSH框架实现简单登录_第5张图片

 

 action层

  UserAction继承ActionSupport 

package cn.ssh.action;

import cn.ssh.pojo.User;
import cn.ssh.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import java.util.Map;

/**
 * @author: wmj
 * @Data: 2018-10-30 15:25
 **/
@Controller
public class UserAction extends ActionSupport {
    @Autowired

    private UserService userService;

    private  String name;  //名称
    private  String password;   //密码
    private  String message;   //信息提示  
    public  String login(){
        User user = userService.findUser(name, password);
        ActionContext context=ActionContext.getContext();
      //用户名为空
if (name==null || name.equals("")){ this.setMessage("请输入名称"); return "login"; }         //密码为空 if (password==null || password.equals("")){ this.setMessage("请输入密码"); return "login"; }
      //将登陆的用户保存在session集合中
Map session
= context.getSession(); if (null!=user){ session.put("user",user); return "index"; } this.setMessage("登录失败"); return "login"; } //要有getter和setter方法,否则映射不到 public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }

创建相应的JSP页面

  创建login.jsp 登陆页面

<%--
  Created by IntelliJ IDEA.
  User: wumengjie
  Date: 2018/10/30
  Time: 15:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
    <title>Titletitle>
head>
<body>
<s:form action="login.action" method="post">

    用户名:<s:textfield name="name"/> <br/>
    密码:<s:password name="password"/>
    <s:submit value="提交"/>
s:form>
<s:property value="message">s:property>
body>
html>

这时配置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.ui.theme" value="simple"/>
    <constant name="struts.i18n.encoding" value="UTF-8"/>
  <package name="default" namespace="/" extends="struts-default">
      <action name="login" class="cn.ssh.action.UserAction" method="login">
          <result name="login">/login.jspresult>
          <result name="index">/index.jspresult>
      action>
  package>
struts>

登录成功页面:index.jsp

成功<s:property value="#session.user.name">s:property>

 

配置TomCat

点击Edit Configurations....

  整合SSH框架实现简单登录_第6张图片

 

点击加号,找到Tomcat Server,配置本地tomcat

 

整合SSH框架实现简单登录_第7张图片

 配置相关信息

 整合SSH框架实现简单登录_第8张图片

整合SSH框架实现简单登录_第9张图片

点击带有exploded的即可,把tomcat集成到项目中

整合SSH框架实现简单登录_第10张图片

也可以点击 Deployment------点击加号-----artifact 

整合SSH框架实现简单登录_第11张图片

 

 整合SSH框架实现简单登录_第12张图片

什么都不需要改,点击apply即可

整合SSH框架实现简单登录_第13张图片

 

启动tomcat,测试

整合SSH框架实现简单登录_第14张图片

整合SSH框架实现简单登录_第15张图片

当没有输入密码或输入错误密码会进行提示,因为在UserAction中有message字段,对字段进行赋值,在页面获取即可

整合SSH框架实现简单登录_第16张图片

 

   

转载于:https://www.cnblogs.com/luoxionghenku/p/9915207.html

你可能感兴趣的:(整合SSH框架实现简单登录)