Java web从入门到放弃(6)

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}">

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值