javascript
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類
注解掉controller,點擊測試首頁,依然成功進入
7. 設置項目的路徑名
輸入地址 http://localhost:8080/ 訪問失敗
輸入地址 http://localhost:8080/zz/ 才能訪問成功
8. 增加登錄和攔截功能
步驟 :
(1)前端提交登錄請求 (看表單)
(2)后端處理登錄請求 (登錄成功,跳轉到主頁,顯示用戶的名字,如果沒有成功告訴用戶:用戶名或密碼錯誤)
(3)發現問題:沒有登錄也能進入主頁
(4)增加一個攔截器,判斷用戶是否登錄
(5)測試登錄和攔截是否成功!
代碼 :
MyMvcConfig文件
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(二)——实现一个基本的小网站的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot(一)——快速入门
- 下一篇: SpringMVC(三)——JSON