先贴我的项目路径
1、查找问题
前几天写了一个controller,一直没法通过地址跳转到course.html页面,报错,我查百度都说可能application启动类位置的问题,但我看了下我的项目结构,application启动类的确和我的controller包在同一级目录
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class PageController {
@RequestMapping("/courseadd")
public String addCourse(){
return "admin/course.html";
}
@GetMapping("/index")
public String addIndex(){
return "admin/index.html";
}
}
然后又看到可能没扫描到controller包的答案,于是我又顺带看了下我的启动类,嗯,也没问题
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"com.laoxu.spbone.entity","com.laoxu.spbone.dao","com.laoxu.spbone.bll","com.laoxu.spbone.controller"})
public class SpboneApplication {
public static void main(String[] args) {
SpringApplication.run(SpboneApplication.class, args);
}
}
于是我把目光放在 application.properties文件,woc,我自己配置了上下文路径我自己给忘了,真是降智
2、解决问题
ok,有了,但是现在resource下的js,css等文件不能加载,并没有被成功访问
问了某位老哥,才知道改资源引用地址就OK,因为通过springmvc调用的页面都是从static包下开始引用的,所以不用返回admin上一级目录再调用其他资源
<head>
<meta charset="UTF-8">
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="js/jquery-1.10.2.min.js"></script>
<script src="js/project.js"></script>
<script src="js/public.js"></script>
<link rel="stylesheet" type="text/css" href="css/course.css"/>
<!--修改前-->
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/reset.min.css">
<link rel="stylesheet" href="css/styleRain.css">
<!--修改后-->
<script src="../js/jquery-1.10.2.min.js"></script>
<script src="../js/project.js"></script>
<script src="../js/public.js"></script>
</head>
3、完美解决!!!
警告!!!
警告自己不能再犯这么蠢的错误了!!!!!
4.26更新补充
如果还是扫不到resource下的静态文件,就在pom下面加这些代码
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<!--加载模板文件-->
<include>**/*.html</include>
<include>**/*.js</include>
<include>**/*.css</include>
<include>**/*.png</include>
<include>**/*.gif</include>
<!--加载静态文件-->
<include>/static/</include>
</includes>
<filtering>false</filtering>
</resource>