本来想早点完成这篇文章的,奈何一方面使用spring boot建立的项目要么是访问不到静态资源,要么就访问不到html页面,完不成demo,另一方面就是这段时间工作比较忙,来不及继续探究资源访问不到的问题。终于在今晚闲下来了,完成这边文章。最初的立意是为了通过实现简单的登录功能展示如何使用springboot来开发web项目的,但是最后不得不变为重点放在解决如何访问静态资源的问题。




springboot多个module找不到service springboot找不到controller_springboot登录


标题项目文件结构


springboot多个module找不到service springboot找不到controller_springboot登录_02


标题数据库准备

简单地在MySQL上建立了一个h_user的表,用于存储用户账号和密码


springboot多个module找不到service springboot找不到controller_springboot登录_03


SET FOREIGN_KEY_CHECKS=0;CREATE TABLE `h_user` (`pk_id` int(11) NOT NULL AUTO_INCREMENT,`user_name` varchar(50) NOT NULL,`login_name` varchar(50) NOT NULL,`password` varchar(50) NOT NULL,PRIMARY KEY (`pk_id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;INSERT INTO `h_user` VALUES ('1', '张三', 'zhangsan', '123');INSERT INTO `h_user` VALUES ('2', '李四', 'lisi', '123');

标题Java实现代码

1、controller层代码

package com.dulucy.springboot.login.controller;import com.dulucy.springboot.login.service.ILoginService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.util.Map;@Controller@RequestMapping("/Login")public class LoginController {    @Autowired    private ILoginService loginService;    @RequestMapping("/seletePasswordByName.do")    @ResponseBody    public Map seletePasswordByName(HttpServletRequest request, HttpServletResponse response) {        Map map=loginService.seletePasswordByName(request);        return map;    }}

2、service层代码

package com.dulucy.springboot.login.service;import javax.servlet.http.HttpServletRequest;import java.util.Map;public interface ILoginService {    Map seletePasswordByName(HttpServletRequest request);}

3、serviceimpl层代码

package com.dulucy.springboot.login.serviceimpl;import com.dulucy.springboot.login.dao.ILoginDao;import com.dulucy.springboot.login.service.ILoginService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import javax.servlet.http.HttpServletRequest;import java.util.HashMap;import java.util.Map;@Servicepublic class LoginServiceImpl implements ILoginService {    @Autowired    private ILoginDao loginDao;    @Override    public Map seletePasswordByName(HttpServletRequest request) {        String login_name=request.getParameter("login_name");        String pwd =request.getParameter("password");        String password=loginDao.seletePasswordByName(login_name);        Map map=new HashMap(); //用于包装返回结果        if (pwd.equals(password)){            map.put("code",1);            map.put("msg","账号/密码正确,登录成功");            map.put("data","");        }else{            map.put("code",0);            map.put("msg","账号/密码错误");            map.put("data","");        }        return map;    }}

4、dao层代码(使用注解开发)

package com.dulucy.springboot.login.dao;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.Select;import org.springframework.stereotype.Component;@Component(value = "apiCategoryMapper")public interface ILoginDao {    @Select("select password from h_user where login_name=#{login_name}")    String seletePasswordByName(@Param("login_name") String login_name);}

标题配置文件

1、application.yml文件配置项。这里我是采用的yml文件作为配置文件,结构比properties文件更加清晰。配置了端口和项目名‘/dulucy’,需要注意的是mvc和resources的配置,不然可能会出现无法访问静态资源的问题。

#服务器端口和项目名称配置server:  port: 8080  servlet:    context-path: /dulucy#数据库配置spring:  datasource:    name: springboot    driver-class-name: com.mysql.jdbc.Driver    url: jdbc:mysql://localhost:3306/test    username: dulucy    password: 123456  mvc:    view:      suffix: .html    static-path-pattern: /**  resources:    static-locations: classpath:/templates/,classpath:/static/  devtools:    restart:      enabled: true  #设置开启热部署      additional-paths: src/main/java #重启目录      exclude: static/**    freemarker:      cache: false    #页面不加载缓存,修改即时生效

2、配置daoceng扫描

这个是在启动类上使用注解配置的。

package com.dulucy.springboot;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.transaction.annotation.EnableTransactionManagement;@SpringBootApplication(scanBasePackages = "com.dulucy.springboot")@EnableTransactionManagement@MapperScan("com.dulucy.springboot.login.dao")public class SpringbootApplication {    public static void main(String[] args) {        SpringApplication.run(SpringbootApplication.class, args);    }}


springboot多个module找不到service springboot找不到controller_springboot登录_04


3、pom.xml配置

虽然使用了springboot来使得jar包导入更加简便,我这里还是手动添加了几个jar包。

<?xml version="1.0" encoding="UTF-8"?>4.0.0org.springframework.boot        spring-boot-starter-parent        2.3.3.RELEASEcom.dulucy    springboot    0.0.1-SNAPSHOTspringbootDemo project for Spring Boot1.8org.springframework.boot            spring-boot-starter-web        org.mybatis.spring.boot            mybatis-spring-boot-starter            2.1.3mysql            mysql-connector-java            runtimeorg.projectlombok            lombok            trueorg.springframework.boot            spring-boot-starter-test            testorg.junit.vintage                    junit-vintage-engine                org.springframework.boot            spring-boot-devtools            truetruemysql            mysql-connector-java            5.1.47org.springframework.boot            spring-boot-starter-thymeleaf        org.springframework.boot                spring-boot-maven-plugin


springboot多个module找不到service springboot找不到controller_springboot登录_05


标题编写html页面

1、登录页面

前台是基于layui做的,切换成其他框架是一样的。

登录页面


欢迎登陆


username


password


登 入


2、登录成功页面

登陆成功页面登陆成功

标题遇到的问题及解决方案

1、无法访问静态资源问题

这篇文章最主要卡的地方就是这里,从开发来说,这其实就是一个很小的问题,但是就是没弄明白怎么回事。刚入门学习springboot,和之前的配置相比简化了很多,遇到相同的问题,反而不知道怎么处理。最开始参考了很多博客主的教程,都说添加

mvc:    view:      suffix: .html    static-path-pattern: /**  resources:    static-locations: classpath:/templates/,classpath:/static/123456

就可以访问,实际上可以分开访问到html或者静态图片,但是在html中引用静态资源就报404。

本想着直接用html开发,不使用springboot官方推荐的thymeleaf模板开发,实在进行不下去了,在pom.xml引入thymeleaf后,居然能够访问html和静态资源了。具体的底层的不是很懂,但就是这样解决问题了。

推荐文章:

SpringBoot系列入门——搭建SpringBoot项目