Web服务做的事情:
Spring Controller技术的三个核心点:
- Bean的配置:Controller注解运用
- 网络资源的加载:加载网页
- 网址路由的配置:RequestMapping注解的运用
1.Controller注解
Spring Controller本身也是一个Spring Bean,只是它多提供了Web能力,只需要造类上提供@Controller注解即可
import org.springframework.stereotype.Controller;
@Controller
public class HelloControl {
}
2.加载网页
在Spring Boot应用中,一般把网页存放在src/main/resources/static目录下,在controller中,会自动加载static下的html内容
import org.springframework.stereotype.Controller;
@Controller
public class HelloControl {
/**
* 返回类型为String
* return "hello.html"返回的是html文件路径
*/
public String say(){
return "hello.html";
}
}
3.RequestMapping注解
Spring MVC完美的支持了路由能力简化了路由配置,只需要在需要提供Web访问的方法上添加一个@RequestMapping
注解就可以完成配置了
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloControl {
@RequestMapping("/hello")
public String say(){
return "html/hello.html";
}
}
这里的hello.html放在static下的html文件夹中