SSH 环境搭建及注解实现登录功能 DEMO
前言
最近通过网上的视频学习了一下 SSH 三大框架的整合应用,熟悉一下流程,视频其实三四天就可以看完了。比较基础的内容,更多的是需要自己去尝试,离开视频后一步步去操作,去实践。
环境
Eclipse 4.7.1
Windows 7
Spring 4.3.9 版本
Struts 2.5 版本
Hibernate 5.2.12 版本
C3P0 0.9.5.2 版本
MySQL 驱动 5.1.41
struts2-spring-plugin-2.5.13.jar
说明版本只是因为,不同版本配置可能有所不同,自己配置的时候,因为 jar 包都是从官网下载,所以版本都比较新。配置起来有所不同,同时有些使用的方法也可能被废弃了。
配置
新建 web 项目
导入相关的 jar 包
配置文件的配置
编写后台代码
前台页面
测试
新建 web 项目
打开 Eclipse,File —— New —— Dynamic Web project,输入项目名称,然后 Finally
导入相关的 jar 包
导入最基础的 jar 包即可,缺包的再导入相关的 jar 包
Struts2 的官网有提供必须的最小 jar 包
Hibernate 则导入 required 文件夹下的 jar 包和 jpa 文件夹下的 jar 包
Spring 的 jar 包(需要导入 spring-orm-4.3.9.RELEASE.jar 与 Hibernate 整合)
mysql 驱动的 jar 包
导入 Struts2 与 Spring 整合的插件包 struts2-spring-plugin-2.5.13.jar
导入 c3p0-0.9.5.2.jar
我的导入如下:
配置文件的配置
web.xml 配置
在 /ssh_anno(项目名)/WebContent/WEB-INF 下创建一个 web.xml 文件。配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>ssh_anno</display-name>
<welcome-file-list>
<welcome-file>/WEB-INF/login.jsp</welcome-file>
</welcome-file-list>
<!-- 配置监听器 -->
<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.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
applicationContext.xml 配置
在 src 下创建 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-bean