JavaWeb 总纲

目录

系统架构

技术选型

数据库设计

开发步骤

用户登录和注销


在这篇博客中,我将介绍如何使用 JavaWeb 技术开发一个简单的博客系统。本系统将包含用户登录、文章发布、评论和分类等基本功能。我们将使用 Java 语言、Spring 框架、MyBatis 框架和 MySQL 数据库。

  1. 系统架构

我们将使用经典的三层架构来开发我们的博客系统。这个架构将系统分为表示层、业务逻辑层和数据访问层。如下图:

  1. 技术选型

  • Java:Java 是一种跨平台的编程语言,可以在各种不同的操作系统上运行。
  • Spring:Spring 是一个轻量级的框架,提供了很多有用的功能,如 IoC、AOP、事务管理等。
  • MyBatis:MyBatis 是一个优秀的持久层框架,可以简化数据库访问的开发。
  • MySQL:MySQL 是一个流行的关系型数据库管理系统。
  1. 数据库设计

我们将使用 MySQL 数据库来存储博客系统数据。下面是我们的数据库设计:

  • 用户表(user):存储用户信息,如用户名、密码、邮箱等。
  • 文章表(article):存储文章信息,如标题、内容、作者等。
  • 评论表(comment):存储评论信息,如评论内容、评论人等。
  • 分类表(category):存储文章分类信息。
  1. 开发步骤

下面是我们的开发步骤:

  • 创建 Maven 项目并添加所需的依赖。
  • 创建数据库并设计数据表。
  • 创建实体类和 DAO 接口。
  • 创建 Service 层和 Controller 层。
  • 创建视图页面(JSP)。
  • 配置 Spring 和 MyBatis。
  • 测试系统功能。
  1. 案例

在这个案例中,我们将实现以下功能:

  • 用户登录和注销。
  • 文章发布和编辑。
  • 文章分类管理。
  • 文章评论和回复。

下面是我们的代码实现和页面截图。

  • 用户登录和注销

我们将创建一个 LoginController 类来处理用户登录和注销请求。下面是代码实现:

@Controller
public class LoginController {
    
    @Autowired
    private UserService userService;
    
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "login";
    }
    
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(String username, String password, HttpSession session) {
        User user = userService.login(username, password);
        if (user != null) {
            session.setAttribute("user", user);
            return "redirect:/";
        } else {
            return "login";
        }
    }
    
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/";
    }
}

我们通过 @Controller 注解将 LoginController 类声明为 Spring MVC 的控制器,并使用 @Autowired 注解注入 UserService 对象。在 login() 方法中,我们返回一个名为 “login” 的视图页面。在 login(String username, String password, HttpSession session) 方法中,我们通过 UserService 对象验证用户的用户名和密码是否正确。如果验证通过,我们将用户对象存储在 HttpSession 中,并重定向到首页。否则,我们将返回登录视图。

下面是我们的登录页面:

我们将创建一个 ArticleController 类来处理文章发布和编辑请求。下面是代码实现:

@Controller
@RequestMapping("/article")
public class ArticleController {
    
    @Autowired
    private ArticleService articleService;
    
    @Autowired
    private CategoryService categoryService;
    
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model) {
        List<Category> categories = categoryService.getAll();
        model.addAttribute("categories", categories);
        return "article/add";
    }
    
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(Article article) {
        articleService.add(article);
        return "redirect:/";
    }
    
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable("id") int id, Model model) {
        Article article = articleService.getById(id);
        List<Category> categories = categoryService.getAll();
        model.addAttribute("article", article);
        model.addAttribute("categories", categories);
        return "article/edit";
    }
    
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String edit(Article article) {
        articleService.update(article);
        return "redirect:/";
    }
}
  • 文章发布
  • 我们使用 @Controller 和 @RequestMapping 注解将 ArticleController 类声明为 Spring MVC 的控制器,并使用 @Autowired 注解注入 ArticleService 和 CategoryService 对象。在 add(Model model) 方法中,我们获取所有分类并将它们添加到 Model 中,然后返回一个名为 “article/add” 的视图页面。在 add(Article article) 方法中,我们通过 ArticleService 对象将文章对象保存到数据库中,并重定向到首页。在 edit(@PathVariable(“id”) int id, Model model) 方法中,我们通过 ArticleService 对象获取指定 ID 的文章对象和所有分类,并将它们添加到 Model 中,然后返回一个名为 “article/edit” 的视图页面。在 edit(Article article) 方法中,我们通过 ArticleService 对象更新文章对象,并重定向到首页。

    下面是我们的文章发布页面:

  • 文章分类管理
  • 我们将创建一个 CategoryController 类来处理文章分类管理请求。下面是代码实现:

    @Controller
    @RequestMapping("/category")
    public class CategoryController {
        
        @Autowired
        private CategoryService categoryService;
        
        @RequestMapping(value = "/list", method = RequestMethod.GET)
        public String list(Model model) {
            List<Category> categories = categoryService.getAll();
            model.addAttribute("categories", categories);
            return "category/list";
        }
        
        @RequestMapping(value = "/add", method = RequestMethod.GET)
        public String add() {
            return "category/add";
        }
        
        @RequestMapping(value = "/add", method = RequestMethod.POST)
        public String add(Category category) {
            categoryService.add(category);
            return "redirect:/category/list";
        }
        
        @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
        public String edit(@PathVariable("id") int id, Model model) {
            Category category = categoryService.getById(id);
            model.addAttribute("category", category);
            return "category/edit";
        }
        
        @RequestMapping(value = "/edit", method = RequestMethod.POST)
        public String edit(Category category) {
            categoryService.update(category);
            return "redirect:/category/list";
        }
        
        @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
        public String delete(@PathVariable("id") int id) {
            categoryService.delete(id);
            return "redirect:/category/list";
        }
    }

    我们使用 @Controller 和 @RequestMapping 注解将 CategoryController 类声明为 Spring MVC 的控制器,并使用 @Autowired 注解注入 CategoryService 对象。在 list(Model model) 方法中,我们获取所有分类并将它们添加到 Model 中,然后返回一个名为 “category/list” 的视图页面。在 add() 方法中,我们返回一个名为 “category/add” 的视图页面。在 add(Category category) 方法中,我们通过 CategoryService 对象将分类对象保存到数据库中,并重定

  • 定向到分类列表页面。在 edit(@PathVariable(“id”) int id, Model model) 方法中,我们通过 CategoryService 对象获取指定 ID 的分类对象,并将它们添加到 Model 中,然后返回一个名为 “category/edit” 的视图页面。在 edit(Category category) 方法中,我们通过 CategoryService 对象更新分类对象,并重定向到分类列表页面。在 delete(@PathVariable(“id”) int id) 方法中,我们通过 CategoryService 对象删除指定 ID 的分类对象,并重定向到分类列表页面。

    下面是我们的文章分类列表页面:

  • 文章评论和回复
  • 我们将创建一个 CommentController 类来处理文章评论和回复请求。下面是代码实现:

    @Controller
    @RequestMapping("/comment")
    public class CommentController {
        
        @Autowired
        private CommentService commentService;
        
        @RequestMapping(value = "/add", method = RequestMethod.POST)
        public String add(Comment comment) {
            commentService.add(comment);
            return "redirect:/article/detail/" + comment.getArticleId();
        }
        
        @RequestMapping(value = "/reply/{id}", method = RequestMethod.GET)
        public String reply(@PathVariable("id") int id, Model model) {
            Comment comment = commentService.getById(id);
            model.addAttribute("comment", comment);
            return "comment/reply";
        }
        
        @RequestMapping(value = "/reply", method = RequestMethod.POST)
        public String reply(Comment comment) {
            commentService.add(comment);
            return "redirect:/article/detail/" + comment.getArticleId();
        }
    }

    我们使用 @Controller 和 @RequestMapping 注解将 CommentController 类声明为 Spring MVC 的控制器,并使用 @Autowired 注解注入 CommentService 对象。在 add(Comment comment) 方法中,我们通过 CommentService 对象将评论对象保存到数据库中,并重定向到文章详情页面。在 reply(@PathVariable(“id”) int id, Model model) 方法中,我们通过 CommentService 对象获取指定 ID 的评论对象,并将它们添加到 Model 中,然后返回一个名为 “comment/reply” 的视图页面。在 reply(Comment comment) 方法中,我们通过 CommentService 对象将回复评论对象保存到数据库中,并重定向到文章详情页面。

    下面是我们的文章评论页面:

  • 总结
  • 在本文中,我们使用 JavaWeb 技术开发了一个简单的博客系统。我们了解了 JavaWeb 的三层架构、Spring 框架、MyBatis 框架和 MySQL 数据库。我们实现了用户登录、文章发布、分类管理和评论回复等功能。这个博客系统只是一个简单的示例,你可以在此基础上继续扩展和完善它。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值