springboot学习
遇到Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean. 的问题
参考链接https://blog.csdn.net/zhang168/article/details/51423905。
这个问题的原因就出在,加这3个注解的类 @Configuration,@ServletComponentScan,@EnableAutoConfiguration 和 SpringApplication.run(GameServer.class, args); 中的.class类不是同一个类的问题,保持一致就好了。注解加在哪,run方法中传入的class就是谁
添加注解,问题解决
一、处理URl中的参数
Controller的使用
获取参数的方式主要有以下三种
@PathVarible 获取URl中的参数
@RequestParam 获取请求参数的值
@GetMapping 组合注解
1、@PathVarible方式
@RestController
//@Controller
@RequestMapping("/hello")
public class HelloController {
@Autowired
private GirlProperites girlProperites;
@RequestMapping(value = "/{id}/say",method = RequestMethod.GET)
public String say(@PathVariable("id") Integer id){
return "id : "+ id;
}
}
在浏览器中输入http://localhost:8080/hello/100/say,
则会输出“id: ”100
2、@RequestParam方式
@RestController
//@Controller
@RequestMapping("/hello")
public class HelloController {
@Autowired
private GirlProperites girlProperites;
@RequestMapping(value = "/say",method = RequestMethod.GET)
public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer id){
return "id : "+ id;
}
}
其中设置id为不必须,且默认值为“0”
在浏览器中输入http://localhost:8080/hello/say?id=11111
则会输出 id : 11111
3、@GetMapping (value=“/say”)相当于@RequestMapping(value = "/say",method = RequestMethod.GET)
SpringBoot更改静态资源访问路径问题
转自https://blog.csdn.net/catoop/article/details/50501706
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("file:../image/").addResourceLocations("classpath:/static/");
super.addResourceHandlers(registry);
}
}
file:../image/ 是根据项目的所在路径探测的路径,也就是说将图片放在和项目同目录下的image文件夹下,都可以访问到
classpath:/static/ 是项目内的路径,也就是项目内的resource文件下
在HTML中(此处我使用的是thymeleaf)使用如下方式即可访问到“../image/”下的图片
<img th:src="@{static/Hart1.png}">