日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot(二)——实现一个基本的小网站

發布時間:2025/3/13 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot(二)——实现一个基本的小网站 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 1. 新建一個SPringboot項目,構建web模塊
  • 2. 導入pojo 和 dao 的類
  • 3. 導入靜態資源
  • 4. 所有頁面增加頭文件
  • 5. 所有的資源鏈接修改格式為thymeleaf 語法
  • 6. 首頁實現
  • 7. 設置項目的路徑名
  • 8. 增加登錄和攔截功能

1. 新建一個SPringboot項目,構建web模塊

2. 導入pojo 和 dao 的類

pojo實體層的Department類和Employee類:

package com.zz.pojo;public class Department {private Integer id;private String departmentName;public Department() {}public Department(int i, String string) {this.id = i;this.departmentName = string;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getDepartmentName() {return departmentName;}public void setDepartmentName(String departmentName) {this.departmentName = departmentName;}@Overridepublic String toString() {return "Department [id=" + id + ", departmentName=" + departmentName + "]";} } kage com.zz.pojo;import java.util.Date;public class Employee {private Integer id;private String lastName;private String email;//1 male, 0 femaleprivate Integer gender;private Department department;private Date birth;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public Integer getGender() {return gender;}public void setGender(Integer gender) {this.gender = gender;}public Department getDepartment() {return department;}public void setDepartment(Department department) {this.department = department;}public Date getBirth() {return birth;}public void setBirth(Date birth) {this.birth = birth;}public Employee(Integer id, String lastName, String email, Integer gender,Department department) {super();this.id = id;this.lastName = lastName;this.email = email;this.gender = gender;this.department = department;this.birth = new Date();}public Employee() {}@Overridepublic String toString() {return "Employee{" +"id=" + id +", lastName='" + lastName + '\'' +", email='" + email + '\'' +", gender=" + gender +", department=" + department +", birth=" + birth +'}';} }

dao接口層的Department類和Employee類:

package com.kuang.myproject.dao;import java.util.Collection; import java.util.HashMap; import java.util.Map;import com.kuang.myproject.pojo.Department; import org.springframework.stereotype.Repository;@Repository public class DepartmentDao {private static Map<Integer, Department> departments = null;static{departments = new HashMap<Integer, Department>();departments.put(101, new Department(101, "D-AA"));departments.put(102, new Department(102, "D-BB"));departments.put(103, new Department(103, "D-CC"));departments.put(104, new Department(104, "D-DD"));departments.put(105, new Department(105, "D-EE"));}public Collection<Department> getDepartments(){return departments.values();}public Department getDepartment(Integer id){return departments.get(id);} } package com.zz.dao;import java.util.Collection; import java.util.HashMap; import java.util.Map; import com.zz.pojo.Department; import com.zz.pojo.Employee; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository;@Repository public class EmployeeDao {private static Map<Integer, Employee> employees = null;@Autowiredprivate DepartmentDao departmentDao;static{employees = new HashMap<Integer, Employee>();employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1, new Department(101, "D-AA")));employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1, new Department(102, "D-BB")));employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0, new Department(103, "D-CC")));employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0, new Department(104, "D-DD")));employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1, new Department(105, "D-EE")));}private static Integer initId = 1006;//新增員工實時 id 自增public void save(Employee employee){if(employee.getId() == null){employee.setId(initId++);}employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));employees.put(employee.getId(), employee);}//獲得全部員工public Collection<Employee> getAll(){return employees.values();}//通過id獲得員工public Employee get(Integer id){return employees.get(id);}//通過id刪除員工public void delete(Integer id){employees.remove(id);} }

3. 導入靜態資源

頁面放在 Templeate,資源放在 static目錄下

4. 所有頁面增加頭文件

< html lang=“en” xmlns:th=“http://www.thymeleaf.org”>

5. 所有的資源鏈接修改格式為thymeleaf 語法

th:href/src="@{…}"
如 index.html頁面

6. 首頁實現

方式一:編寫controller控制層

package com.zz.controller;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;@Controller public class IndexController {//首頁映射@RequestMapping({"/","/index.html"})public String index(){return "index";} }

點擊測試首頁index.html

方式二:可以增加一個配置類,配置SpringMVC(了解即可)
config層的MyMvcConfig類

package com.zz.config;import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration //代表這是一個配置類 public class MyMvcConfig implements WebMvcConfigurer {//通過配置類來設置視圖跳轉@Overridepublic void addViewControllers(ViewControllerRegistry registry) {// /請求 走到index頁面registry.addViewController("/").setViewName("index");} }

注解掉controller,點擊測試首頁,依然成功進入

7. 設置項目的路徑名


輸入地址 http://localhost:8080/ 訪問失敗

輸入地址 http://localhost:8080/zz/ 才能訪問成功

8. 增加登錄和攔截功能

步驟 :
(1)前端提交登錄請求 (看表單)
(2)后端處理登錄請求 (登錄成功,跳轉到主頁,顯示用戶的名字,如果沒有成功告訴用戶:用戶名或密碼錯誤)
(3)發現問題:沒有登錄也能進入主頁
(4)增加一個攔截器,判斷用戶是否登錄
(5)測試登錄和攔截是否成功!

代碼 :
MyMvcConfig文件

package com.zz.config;import com.zz.interceptor.LoginHandlerInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration // 代表這是一個配置類 public class MyMvcConfig implements WebMvcConfigurer {// 通過配置類來設置視圖跳轉@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/").setViewName("index");registry.addViewController("/index.html").setViewName("index");registry.addViewController("/main.html").setViewName("dashboard");}// 注冊攔截器@Overridepublic void addInterceptors(InterceptorRegistry registry) {// 選擇要過濾和排除的請求registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html","/","/user/login").excludePathPatterns("/asserts/**");} }

UserController文件

package com.zz.controller;import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam;import javax.servlet.http.HttpSession;@Controller public class UserController {// 登錄實現@RequestMapping("/user/login")public String login(@RequestParam("username") String username,@RequestParam("password") String password,Model model, HttpSession session){if (!StringUtils.isEmpty(username) && "123456".equals(password)){// 登錄成功, 利用重定向可以防止表單重復提交session.setAttribute("loginUser",username);return "redirect:/main.html";}else {model.addAttribute("msg","用戶名或者密碼錯誤");return "index";}} }

攔截器:LoginHandlerInterceptor

package com.zz.controller;import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam;import javax.servlet.http.HttpSession;@Controller public class UserController {// 登錄實現@RequestMapping("/user/login")public String login(@RequestParam("username") String username,@RequestParam("password") String password,Model model, HttpSession session){if (!StringUtils.isEmpty(username) && "123456".equals(password)){// 登錄成功, 利用重定向可以防止表單重復提交session.setAttribute("loginUser",username);return "redirect:/main.html";}else {model.addAttribute("msg","用戶名或者密碼錯誤");return "index";}} }

測試

輸入正確的用戶名和密碼,成功登錄進入主頁


輸入錯誤的用戶名和密碼,顯示用戶名或密碼錯誤

沒有登錄或沒有登錄成功的情況下,直接訪問主頁,被攔截器攔截,顯示沒有權限

整個項目目錄:

總結

以上是生活随笔為你收集整理的SpringBoot(二)——实现一个基本的小网站的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。