5.1 staff import static resource management systems

A. Preparations

1. Step

  1. Creating springboot project, add SpringMVC, lombok, thymeleaf (slightly)

  2. Add config, pojo, dao, controller

  3. Application.properties modify configuration files

  4. Import static resource files, modify the format of the resources introduced into thymeleaf

  5. Run the debugger

2. Add config, pojo, dao, controller

(1) add config folder and add MyMvcConfig.java files for configuring the default home page access

Import org.springframework.context.annotation.Configuration;
 Import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
 Import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 

// custom view resolver
 // If you need to customize a number of customization features, just write components to springboot management, spring boot will help us to automatically assemble 
@Configuration
 public  class MyMvcConfig the implements WebMvcConfigurer { 

    // default home page to access 
    @Override
     public  void addViewControllers (ViewControllerRegistry Registry) { 
        Registry .addViewController ( "/").setViewName("index" );
        registry.addViewController("/index.html").setViewName("index");
    }
}
View Code

(2) adding entity class pojo

Sector categories: Department

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {

    private Integer id;
    private String departmentName;
}
View Code

Staff categories: Employee

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@NoArgsConstructor
public class Employee {

    private Integer id;
    private String lastName;
    private String email;
    private Integer gender; //1 male, 0 female
    private Department department;
    private Date birth;

    public Employee(Integer id, String lastName, String email, Integer gender, Department department) {
        this.id = id;
        this.lastName =lastName;
         the this .email = In Email;
         the this .gender = Gender;
         the this .department = Department; 

        // default creation date of 
        the this .birth = new new a Date (); 
    } 
}
View Code

(3) adding dao layer, used to simulate change to the database search deletions

Departments dao: DepartmentDao 

Import org.springframework.stereotype.Repository;
 Import ustc.wzh.pojo.Department; 

Import java.util.Collection;
 Import the java.util.HashMap;
 Import a java.util.Map; 

@Repository 
public  class DepartmentDao { 

    // for simulating data in the database 
    Private  static the Map <Integer, department> departments = null ; 

    // analog division table to create a 
    static { 
        departments = new new the HashMap <> (); 

        departments.put ( 101, new new department (101, "AA-D" ) );
        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();

    returnDepartment getDepartment (Integer id) {
        publicanalog information sector specified id obtained//
    }
     departments.get(id);
    }

}
View Code

Employees dao: EmployeeDao

Import org.springframework.beans.factory.annotation.Autowired;
 Import org.springframework.stereotype.Repository;
 Import ustc.wzh.pojo.Department;
 Import ustc.wzh.pojo.Employee; 

Import java.util.Collection;
 Import Java. util.HashMap;
 Import a java.util.Map; 

@Repository 
public  class EmployeeDao { 

    // data simulation database 
    Private  static the Map <Integer, the employee> the employees = null ; 

    // sector belongs to employees defined 
    @Autowired
     Private DepartmentDao departmentDao; 

    static {
        employees = new HashMap<>();

        employees.put(1001, new Employee(1001, "E-AA", "[email protected]", 1, new Department(101, "D-AA")));
        employees.put(1002, new Employee(1002, "E-BB", "[email protected]", 1, new Department(102, "D-BB")));
        employees.put(1003, new Employee(1003, "E-CC", "[email protected]", 0, new Department(103, "D-CC")));
        employees.put(1004, new Employee(1004, "E-DD", "[email protected]", 0, newDepartment (104, "DD-D" ))); 
        employees.put ( 1005, new new the Employee (1005, "EE-E", "[email protected]",. 1, new new Department (105, "EE-D" ))); 
    } 

    // primary self-energizing 
    Private  static Integer initId = 1006 ; 

    // add an employee information 
    public  void Save (the employee employee) {
         IF (employee.getId () == null ) { 
            employee.setId (initId ++ ); 
        } 

        employee.setDepartment (departmentDao.getDepartment (employee.getDepartment () getId ().));
        employees.put(employee.getId(), employee);
    } 

    // Search employee information 
    public Collection <the Employee> the getAll () {
         return employees.values (); 
    } 

    // get information by employee ID 
    public the Employee GET (Integer ID) {
         return employees.get (ID); 
    } 

    // employees delete 
    public  void the delete (Integer the above mentioned id) { 
        employees.remove (the above mentioned id); 
    } 
}
View Code

(4) Add the controller layer

IndexController: for jumping to index.html

Import org.springframework.stereotype.Controller;
 Import org.springframework.web.bind.annotation.RequestMapping; 

@Controller 
public  class IndexController { 

//     // resolves to the index.html page in the templates directory
 //     @ RequestMapping ({ " /","/index.html "})
 //     public String index () {
 //         return" index ";
 //     } 
}
View Code

3. Modify the configuration file application.properties

  • Close to update the cache is static resource files for project development

  • Start access path configuration items:  HTTP: // localhost: 8080 / WZH / 

# Close the template engine cache 
spring.thymeleaf.cache = false 

# configuration items start access path 
server.servlet.context-path = / wzh

4. The resource importer static resource file, to modify the format introduced into thymeleaf

(1) placed in a folder in static css, img, js and other folders and files into html file in the templates, the outermost layer to add console's launch chart styles banner.txt

(2) Thymeleaf format

Header information constraints:

xmlns:th="http://www.thymeleaf.org"

  To take over the static resource, as is the use of URL: @ {...}

<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">

5. Run the debugger

  Start the project, visit:  HTTP: // localhost: 8080 / WZH /  enter Home Success

 

 

Guess you like

Origin www.cnblogs.com/zhihaospace/p/12387350.html
Recommended