知识点回顾
开始整合(使用Maven)
使用Maven新建一个项目:(关于Eclipse中使用Maven可参看这篇文章)
新建好一个空的Maven项目结构是这样的
接下来你需要在pom.xml中配置项目需要的jar包,eclipse会为你下载,以后开发时,你可以(到Maven的官网查找需要的包进行下载)
pom.xml1
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>SSH2LoginDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SSH2LoginDemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- 导入junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- 导入基本的javax.servlet -->
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!-- 导入Mysql的连接驱动包 -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-spring-plugin -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5</version>
</dependency>
<!-- http://projects.spring.io/spring-framework/ -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<!-- http://projects.spring.io/spring-framework/ -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.9.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.javassist/javassist -->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.22.0-CR1</version>
</dependency>
<!-- druid连接池 -->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.26</version>
</dependency>
<!-- j2ee的注解 -->
<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>SSH2LoginDemo</finalName>
</build>
</project>
新建四个文件放在src/main/resource中
- applicationContext.xml(配置spring)
- jdbc.properties(数据库连接的属性文件)
- log4j.properties(日志记录属性文件)
- struts.xml(struts2的配置文件)
先写上日志记录需要的属性文件,其他三个文件具体内容在编码时给出
log4j.properties
1 | ### direct log messages to stdout ### |
之后你需要在src/main/webapp/WEB-INF/web.xml中添加Spring的核心监听器和Struts2的过滤器
web.xml
1 | <?xml version="1.0" encoding="UTF-8"?> |
具体编码前,我们先创建包和文件结构,结构图如下:
开始编码:
一. 视图层(登录:login.jsp ,登录成功:loginSuccess.jsp ,登录失败:回到login.jsp)
login.jsp1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%"s" uri="/struts-tags" %> prefix=
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>
</head>
<body>
<!-- 如果登录失败,显示登录失败信息 -->
<h1><s:property value="fieldErrors.loginFail"/></h1>
<form action="user_login" method="post">
用户名:<input type="text" name="username">
密码:<input type="password" name="password">
<input type="submit" value="登录">
</form>
</body>
</html>
loginSuccess.jsp
1 | <%@ page language="java" contentType="text/html; charset=UTF-8" |
二.控制层(UserAction),通过Struts.xml配置请求的Action
Struts.xml
1 | <!DOCTYPE struts PUBLIC |
UserAction.java
1 | package com.action; |
三.业务逻辑层(UserService),UserServiceImpl实现UserService中的方法
UserService.java
1 | package com.service; |
UserServiceImpl.java
1 | package com.service.impl; |
四.数据库操作层(UserDao),UserDaoImpl实现UserDao中的方法
UserDao.java
1 | package com.dao; |
UserDaoImpl.java
1 | package com.dao.impl; |
五.实体层(User)
User.java
1 | package com.domain; |
六.建立数据库关联
在数据库中创建一个名字为_ssh的数据库
jjdbc.properties
1 | jdbc.driver=com.mysql.cj.jdbc.Driver |
将数据库相关的全部配置作为Bean交给Spring处理,applicationContext.xml
1 | <?xml version="1.0" encoding="UTF-8"?> |
测试:
总结:
MVC思想是贯穿WEB开发的,回想自己当初刚刚接触WEB开发时,学习thinkphp5框架一脸懵。当时刚刚大一结束,对编程的接触就是用C语言完成了一个通讯录管理程序。用C语言实现了简单的增删改查。接触WEB开发,先学html+css,学得不怎么样,连盒子模型都没有理解透,就开始接触Boostrap框架。后来隔了一个寒假,回去开始学习thinkphp5,连框架的概念都没有的我每天学习十分得痛苦。现在,离我离开实验室应该是4个月。我自己从零开始接触了java,四个月时间,我的进步还是可观的,回想大学这一年,发生的事情真的好多。