
一篇博客带你实现一个真正的项目!
先来看看它是什么样式的:
目录:
1、大体步骤練:
1、创建Maven项目秊
2、引入依赖秊
3、创建必要的目录秊
4、编写代码秊
5、打包部署(基于SmartTomcat)秊
6、在浏览器验证秊
2、具体代码实现練:
1、V——用户界面,前端部分秊:
HTML 部分勞:
CSS 部分勞:
JS 部分勞:
要想自己实现一个Web项目,具体步骤如下:
打开 Idea,创建 new project,在新弹窗的左侧选项中,选择 Maven,点击 next;
在新弹窗里写好项目的名称和路径,然后点击 finish,就完成项目创建了。
现在我们进入了代码编辑界面,开始项目的第二步:引入必需的依赖,包括 servlet、mysql、jackson。
先找到自动跳转出来的 pom.xml 文件,文件位置如下:
在这里填上一段
打开Maven中央仓库网站:https://mvnrepository.com/
在最上方搜索 servlet ,点击第一个搜索结果 Java Servlet API,再在下方的版本号里选择 3.1.0 版本,最后复制下面那串代码到之前的 pom.xml 文件的 dependencies 标签中:
mysql 和 jackson 也是一样,先在搜索框搜索 mysql ,点击第一个搜索结果,再找到 5.1.47 版本的,点击之后还是复制那串代码到 dependencies 标签下:
在搜索框搜索 jackson ,点击第一个搜索结果,再点击 2.12.6.1 版本,最后复制代码:
最后引入成功后,就会开始下载依赖,如果你的idea没有显示下载,那么可以自己手动点击刷新下载。
当我们成功引入依赖后,就可以开始创建必需的目录:
1)在 main 目录下创建一个 webapp 目录
2)在 webapp 目录下创建一个 WEB-INF 目录;注意字母为全大写!
3)在 WEB-INF 目录下创建一个文件,命名为 web.xml :
4)最后把下面这串代码复制到 web.xml 文件里:
Archetype Created Web Application
这里的内容是之后我们需要花大时间编写的代码,这里只是先演示一下看看我们之前的步骤有没有成功,例如我们先写一个 servlet :
1)在 main 目录的 java 目录里创建一个类,命名为 HelloServlet:
2)在 HelloServlet 里填入以下代码:
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("hello servlet");
}
}
这里的打包部署我们使用 smart Tomcat 来合并完成;smart Tomcat 是一个 idea 的插件,所以如果你是第一次使用,需要先下载下来。
1)点击左上角的 File > Settings···
2)点击左侧选项的 Plugins > Markplace > 在搜索框搜索 smart tomcat > 点击 Installed
> apply > OK 结束!
3)安装好了以后就可以导入这个插件了!先点击 idea 右侧的 Add Configuration···
4) 在新弹出的弹窗里点击左上角的 + 号,再找到我们下载好的 smart Tomcat ,点击OK:
5)在这个smart tomcat 设置界面,需要设置的有三个,name 可以随意设置,自己顺眼就行;Tomcat Server 刚开始是没有的,需要选择自己安装 tomcat 的路径的文件夹;第三个 Context Path 很重要,设置后一定得记住,当然记不住也没关系,之后可以再打开这个界面看看。
6)设置完之后,界面右上角就会变为下面这样,点击右边的那个绿的小三角就可以启动 tomcat 服务器了,点击后会出现一大堆红色代码,不懂担心,就这样的:
当我们成功启动服务器后,就可以在浏览器里验证以下,看看我们之前所做的准备有没有错误或Bug:
1)在浏览器输入这串地址:127.0.0.1:8080/blog_system/hello ,注意,第一个斜杠和第二个斜杠之间的是我们之前在smart tomcat 里设置的 context path ,第二个斜杠后面的是 HelloServlet上面设置的 servlet path ,当页面出现 “hello servlet” 时,就说明前置准备已经完毕了:
这个项目我们使用前后端分离的方式,使用MVC模式,所以我们在代码实现时也分为M、V、C这三部分。
前端部分主要分为三个板块:html、css 和 javascript 。
在真正写代码之前呢,我们需要在 webapp 目录下创建一个 image 文件夹,里面存放三张图片,可以是 jpg 格式,也可以是png 格式, 第一张图片命名为“系统头像”,这个是在系统导航栏最左侧显示的一个小头像;第二张命名为“用户头像”,这个是登录进页面显示的当前用户头像;第三张命名为“背景”,这个图片是你的博客系统的整个背景图片。上面三张你都可以选择你喜欢的,就像我下面那张图片一样。
1)博客列表页:
在 webapp 下创建一个新的文件,命名为 blog_list.html ,并输入以下代码:
博客列表
我的博客系统
主页
写博客
退出
github 地址
文章
分类
2
1
2)博客详情页:
在 webapp 目录下创建一个文件,命名为 blog_detail.html :
博客详情页
我的博客系统
主页
写博客
退出
夏开开
github 地址
文章
分类
2
1
3)博客编辑页:
在编写博客编辑页之前需要导入editor.md 这个 api ,这个可以自己自行下载,也可以在文章最后进入我的码云下载,里面也有整套代码。
在 webapp 目录下创建一个文件,命名为 blog_edit.html :
博客编辑页
我的博客系统
主页
写博客
退出
4)博客登录页:
在 webapp 目录下创建一个文件,命名为 blog_login.html :
登陆页面
我的博客系统
主页
写博客
这部分代码可以直接在webapp 目录里写,也可以先创建一个 css 目录,在这个目录里写,我选择的是在创建一个 css 目录:
1)博客通用样式:
创建一个文件,命名为 common.css :
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,body{
height: 100%;
}
body{
background-image: url(../image/粉红猪猪背景.jpeg);
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
}
.nav{
width: 100%;
height: 50px;
background-color: rgba(51, 51, 51, 0.5);
color: white;
display: flex;
align-items: center;
}
.nav img{
width: 40px;
height: 40px;
border-radius: 50%;
margin-left: 30px;
margin-right: 10px;
}
.nav .spacer{
width: 78%;
}
.nav a{
color: white;
text-decoration: none;
padding: 0 10px;
}
.container{
width: 1000px;
height: calc(100% - 50px);
margin: 0 auto;
display: flex;
justify-content: space-between;
}
.container .left{
height: 100%;
width: 200px;
}
.container .right{
height: 100%;
width: 790px;
background-color: rgba(255, 255, 255, 0.8);
border-radius: 10px;
overflow: auto;
}
.card{
background-color: rgba(255, 255, 255, 0.8);
border-radius: 10px;
padding: 30px;
}
.card img{
width: 140px;
height: 140px;
border-radius: 50%;
}
.card h3{
text-align: center;
padding: 10px;
}
.card a{
display: block;
text-align: center;
text-decoration: none;
color: grey;
padding: 10px;
}
.card .counter{
display: flex;
justify-content: space-around;
padding: 5px;
}
2)博客列表页样式:
创建一个文件,命名为 blog_list.css :
.blog{
width: 100%;
padding: 20px;
}
.blog .title{
text-align: center;
font-size: 22px;
font-weight: bold;
padding: 10px;
}
.blog .date{
text-align: center;
color: rgb(189, 8, 77);
padding: 10px 0;
}
.blog .desc{
text-indent: 2em;
}
.blog a{
display: block;
width: 140px;
height: 40px;
margin: 13px auto;
border: 2px rgb(238, 87, 87) solid;
border-radius: 20px;
color: rgb(238, 87, 87);
line-height: 40px;
text-align: center;
text-decoration: none;
transition: all 1s;
}
.blog a:hover{
background-color: tomato;
color: white;
}
3)博客详情页样式:
创建一个文件,命名为 blog_detail.css :
.blog-content{
padding: 30px;
}
.blog-content h3{
text-align: center;
}
.blog-content .date{
text-align: center;
color: rgb(189, 8, 77);
padding: 20px 0;
}
.blog-content p{
text-indent: 2em;
padding: 10px 0;
}
4)博客编辑页样式:
创建一个文件,命名为 blog_edit.css :
.blog-edit-container{
width: 1000px;
height: calc(100% - 50px);
margin: 0 auto;
}
.blog-edit-container .title{
width: 100%;
height: 50px;
display: flex;
align-items: center;
justify-content: space-between;
}
.blog-edit-container .title #title{
width: 900px;
height: 40px;
border-radius: 10px;
border: none;
outline: none;
font-size: 22px;
line-height: 40px;
padding-left: 10px;
background-color: rgba(255, 255, 255, 0.8);
}
.blog-edit-container .title #submit{
width: 95px;
height: 40px;
color: white;
background-color: rgb(231, 76, 89);
border-radius: 10px;
border: none;
outline: none;
}
.blog-edit-container .title #submit:active{
background-color: rgb(201, 11, 4);
}
#editor{
border-radius: 10px;
opacity: 90%;
}
5)博客登录页样式:
创建一个文件,命名为 blog_login.css :
.login-container{
width: 100%;
height: calc(100% - 50px);
display: flex;
align-items: center;
justify-content: center;
}
.login-dialog{
width: 400px;
height: 350px;
background-color: rgba(255, 255, 255, 0.9);
border-radius: 10px;
}
.login-dialog h3{
text-align: center;
padding: 50px;
}
.login-dialog .row{
width: 100%;
height: 50px;
display: flex;
align-items: center;
justify-content: center;
}
.login-dialog .row span{
display: block;
width: 100px;
font-weight: 700;
}
#username,#password{
width: 200px;
height: 40px;
font-size: 22px;
line-height: 40px;
padding-left: 10px;
border-radius: 10px;
outline: none;
}
.row #submit{
width: 300px;
height: 50px;
margin-top: 45px;
border-radius: 10px;
border: none;
outline: none;
color: white;
background-color: rgb(212, 53, 67);
font-size: 15px;
}
.row #submit:active{
background-color: rgb(184, 45, 10);
}
js 部分也是一样,可以选择创建一个子目录,也可以不创建直接生产文件,这里我们只需要创建两个文件,
1)创建一个文件,命名为 common.js :
// 这里放置一些检测登录状态的公共代码:
//加上一个逻辑,通过GET /login 这个接口来获取当前的登陆状态:
function getUserInfo(pageName){
$.ajax({
type: 'get',
url: 'login',
success: function(body){
// 判定此处的body是不是一个有效的user对象(userId 是否为非0)
if(body.userId && body.userId > 0){
// 此时登陆成功,不做处理
console.log('当前登陆成功!用户名:'+body.username);
// 根据当前用户登录的情况,把用户名设置到html页面上:
if(pageName == 'blog_list.html'){
changeUserName(body.username);
}
}else{
// 登录失败:
// 让前端页面跳转到login.html
alert('当前尚未登录,请登录后再访问!')
location.assign('blog_login.html');
}
},
error: function(){
alert('当前尚未登录,请登录后再访问!')
location.assign('blog_login.html');
}
});
}
function changeUserName(username){
let h3 = document.querySelector('.card>h3');
h3.innerHTML = username;
}
2)创建一个文件,命名为 jquery.min.js :
!function(e,t){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=e.document?t(e,!0):function(e){if(!e.document)throw new Error("jQuery requires a window with a document");return t(e)}:t(e)}("undefined"!=typeof window?window:this,function(C,e){"use strict";var t=[],r=Object.getPrototypeOf,s=t.slice,g=t.flat?function(e){return t.flat.call(e)}:function(e){return t.concat.apply([],e)},u=t.push,i=t.indexOf,n={},o=n.toString,v=n.hasOwnProperty,a=v.toString,l=a.call(Object),y={},m=function(e){return"function"==typeof e&&"number"!=typeof e.nodeType&&"function"!=typeof e.item},x=function(e){return null!=e&&e===e.window},E=C.document,c={type:!0,src:!0,nonce:!0,noModule:!0};function b(e,t,n){var r,i,o=(n=n||E).createElement("script");if(o.text=e,t)for(r in c)(i=t[r]||t.getAttribute&&t.getAttribute(r))&&o.setAttribute(r,i);n.head.appendChild(o).parentNode.removeChild(o)}function w(e){return null==e?e+"":"object"==typeof e||"function"==typeof e?n[o.call(e)]||"object":typeof e}var f="3.6.0",S=function(e,t){return new S.fn.init(e,t)};function p(e){var t=!!e&&"length"in e&&e.length,n=w(e);return!m(e)&&!x(e)&&("array"===n||0===t||"number"==typeof t&&0