1.配置靜態(tài)資源映射
配置文件使用的都是配置類方式
創(chuàng)建配置類WebMvcConfig,設(shè)置靜態(tài)資源映射
用于在Springboot項(xiàng)目中, 默認(rèn)靜態(tài)資源的存放目錄為 : "classpath:/resources/", "classpath:/static/", "classpath:/public/" ; 而在我們的項(xiàng)目中靜態(tài)資源存放在 backend, front 目錄中, 那么這個(gè)時(shí)候要想訪問到靜態(tài)資源, 就需要設(shè)置靜態(tài)資源映射
@Slf4j @Configuration public class WebMvcConfig extends WebMvcConfigurationSupport { /** * 設(shè)置靜態(tài)資源映射 * @param registry */ @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { log.info("開始進(jìn)行靜態(tài)資源映射..."); registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/back end/"); registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/ "); } }
2.登錄后臺(tái)代碼驗(yàn)證
@Slf4j
@RestController
//@Controller + @ResponseBody = @RestController
//@ResponseBody 注解是將返回的數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換為 Json 格式
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@PostMapping("/login")
/*@RequestBody 注解用于接收前端傳來的實(shí)體,接收參數(shù)也是對(duì)應(yīng)的實(shí)*/
public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
//1.加密密碼
String password = employee.getPassword();
password = DigestUtils.md5DigestAsHex(password.getBytes());
//2.數(shù)據(jù)庫查詢
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Employee::getUsername, employee.getUsername());
Employee emp = employeeService.getOne(queryWrapper);
//3.如果沒有查詢到則返回登錄失敗結(jié)果
if (emp == null) {
return R.error("登錄失敗");
}
//4、密碼比對(duì),如果不一致則返回登錄失敗結(jié)果
if (!emp.getPassword().equals(password)) {
return R.error("登錄失敗");
}
//5、查看員工狀態(tài),如果為已禁用狀態(tài),則返回員工已禁用結(jié)果
if (emp.getStatus() == 0) {
return R.error("賬號(hào)已禁用");
}
//6、登錄成功,將員工id存入Session并返回登錄成功結(jié)果
request.getSession().setAttribute("employee", emp.getId());
return R.success(emp);
}
3.通用結(jié)構(gòu)類R
此類是一個(gè)通用結(jié)果類,服務(wù)端響應(yīng)的所有結(jié)果最終都會(huì)包裝成此種類型返回給前端頁面文章來源:http://www.zghlxwxcb.cn/news/detail-704055.html
。文章來源地址http://www.zghlxwxcb.cn/news/detail-704055.html
@Data public class R<T> { private Integer code; //編碼:1成功,0和其它數(shù)字為失敗 private String msg; //錯(cuò)誤信息 private T data; //數(shù)據(jù) private Map map = new HashMap(); //動(dòng)態(tài)數(shù)據(jù) public static <T> R<T> success(T object) { R<T> r = new R<T>(); r.data = object; r.code = 1; return r; } public static <T> R<T> error(String msg) { R r = new R(); r.msg = msg; r.code = 0; return r; } public R<T> add(String key, Object value) { this.map.put(key, value); return this; }
到了這里,關(guān)于第一天 關(guān)于項(xiàng)目遇到的問題和缺少的知識(shí)點(diǎn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!