SSH(Struts2/Spring/Hibernate)框架搭建

本文主要介绍SSH框架搭建,采用IDEA和Maven工具。项目启动命令:tomcat7:run。
github源码下载:https://github.com/1287642889/SSH
1、项目结构:

SSH(Struts2/Spring/Hibernate)框架搭建_第1张图片
image.png

2、pom.xml代码:



  
    
    
      org.springframework
      spring-context
      5.0.0.RELEASE
    
    
      org.springframework
      spring-core
      5.0.0.RELEASE
    
    
      org.springframework
      spring-beans
      5.0.0.RELEASE
    
    
      org.springframework
      spring-web
      5.0.0.RELEASE
    
    
      org.springframework
      spring-webmvc
      5.0.0.RELEASE
    
    
      org.springframework
      spring-tx
      5.0.0.RELEASE
    
    
      org.springframework
      spring-jdbc
      5.0.0.RELEASE
    
    
      org.springframework
      spring-orm
      5.0.0.RELEASE
    

    
    
      org.apache.struts
      struts2-core
      2.5.14
    
    
      org.apache.struts
      struts2-spring-plugin
      2.5.14
    
    
      org.apache.struts
      struts2-convention-plugin
      2.5.14
    
    
      javax.xml.bind
      jaxb-api
      2.2.2
    

    
    
      org.hibernate
      hibernate-core
      5.2.12.Final
    
    
      org.hibernate.javax.persistence
      hibernate-jpa-2.0-api
      1.0.1.Final
    

    
    
      com.microsoft.sqlserver
      sqljdbc4
      4.0
    

    
    
      com.mchange
      c3p0
      0.9.5.2
    

    
    
      javax.servlet
      jsp-api
      2.0
      provided
    

    
    
      junit
      junit
      4.12
      test
    

  

  
    
      
        
          org.apache.tomcat.maven
          tomcat7-maven-plugin
          2.2
        
      
    
  

3、ssh.xml代码:




    
    

    
    
        
        
        
        
    

    
        
        
        
        
            
                org.hibernate.dialect.SQLServer2008Dialect
                true
                true
                update
            
        
        
    

    
    
        
    

    

    
        
            
            
            
            
            
            
            
            
        
    

    

4、struts.xml代码:




    
    

    
        
        
            /WEB-INF/jsp/success.jsp
        
    

  
        
        
            /WEB-INF/jsp/student.jsp
        
    

5、jdbc.properties代码:

jdbcDriver = com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbcUrl = jdbc:sqlserver://localhost:1433;databaseName = Temp
jdbcUsername = wzf
jdbcPassword = 123456

6、web.xml代码:




  
  
    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
  
  
    encodingFilter
    /*
  

  
  
    struts2
    org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
  
  
    struts2
    /*
  

  
  
    contextConfigLocation
    classpath:config/ssh.xml
  

  
  
    org.springframework.web.context.ContextLoaderListener
  

  
    /WEB-INF/jsp/login.jsp
  


7、LoginAction.java代码:

public class LoginAction extends ActionSupport{
    //设置属性的set方法,才能接受传过来的name值
    //设置属性的get方法,才能在跳转的页面获取该属性值
    private String userName;
    private String password;

    public String login(){
        System.out.println(userName+"----"+password);
        return SUCCESS;
    }

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

8、StudentAction.java代码:

public class StudentAction extends ActionSupport{

    @Autowired
    private StudentService studentService;

    public String getAllStudent() {
        List list = studentService.getAllStudent();
        System.out.println(list.toString());
        return "success";
    }
    

    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }
}

9、StudentDao.java代码:

@Transactional
@Component
public class StudentDao {

    //这里的sessionFactory由Spring进行注入
    @Autowired
    private SessionFactory sessionFactory;


    public List getAllStudent(){
        String hql = " from Student";
        return sessionFactory.getCurrentSession().createQuery(hql).list();
    }
    

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}

10、Student.java代码:

@Entity
public class Student {

    @Id
    private int id;
    private String name;

    public Student(){

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

11、login.jsp代码:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


    
用户名:
密码:

12、success.jsp代码:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>


    Title


    

你好,

13、student.jsp代码:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>


    Title


展示student数据

功能测试:


SSH(Struts2/Spring/Hibernate)框架搭建_第2张图片
image.png

SSH(Struts2/Spring/Hibernate)框架搭建_第3张图片
image.png

SSH(Struts2/Spring/Hibernate)框架搭建_第4张图片
image.png

你可能感兴趣的:(SSH(Struts2/Spring/Hibernate)框架搭建)