springboot学习笔记三
来源:互联网 发布:域名 web名词解释 编辑:程序博客网 时间:2024/06/08 08:21
@RestController
package da.test;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;/** * @GetMapping 与 @PostMapping * 为spring4.3的新特性,访问请求必须为get或者post,代替了@RequestMapping后面的method中请求 * @RestController 加了这个就不需要@ResponseBody了 */@RestControllerpublic class UserController { @RequestMapping("/user/home") public String home() { return "user home"; } @GetMapping("/user/show") public String show(@RequestParam("username") String username, @RequestParam("password") String password) { return "user show " + username + ":" + password; } @PostMapping("/user/create") public String create() { return "user create"; } @GetMapping("/user/showid/{id}") public String showid(@PathVariable("id") Integer id) { return "user show id:" + id; }}
springboot使用jsp:
1 在配置文件中加入
#springboot使用jspspring.mvc.view.prefix=/WEB-INF/jsps/spring.mvc.view.suffix=.jsp
2 在pom文件中加入相关jar包
<!-- springboot默认不支持jsp,开启支持jsp的组建 --><dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId></dependency>
3 jsp文件放在src/main/webapp文件夹下面
4 如果要使用jsp则不能使用@RestController
package da.test;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;@Controllerpublic class LoginController { @GetMapping("/login") public String show(@RequestParam("username") String username, @RequestParam("password") String password) { if (username.equals(password)) return "ok"; return "fail"; } @GetMapping("/login2") public String show(Model model) { model.addAttribute("username", "root"); return "login"; }}
springboot使用freemarker:
1 pom文件中加入依赖
<!-- springboot中的freemarker --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId></dependency>
2 Freemarker存放在templates中
package da.test;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.GetMapping;@Controllerpublic class FreemarkerTest { @GetMapping("reg") public String reg() { return "reg"; } @GetMapping("fmlogin") public String fmlogin(Model model) { model.addAttribute("username", "root"); return "login"; }}
springboot使用jetty服务器启动:
需要修改pom依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 排除tomcat依赖 加入jetty --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId></dependency>
拦截器配置:
package da.test;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;public class LoginInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("======preHandle=======" + handler.getClass()); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("======postHandle======="); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("======afterCompletion======="); }}
package da.test;import org.springframework.boot.SpringBootConfiguration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@SpringBootConfigurationpublic class InterceptorConfiguration extends WebMvcConfigurerAdapter { public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginInterceptor()); }}
异常处理:
先要关闭默认错误处理@SpringBootApplication(exclude=ErrorMvcAutoConfiguration.class)
1 错误页面的处理
package da.test;import org.springframework.boot.web.servlet.ErrorPage;import org.springframework.boot.web.servlet.ErrorPageRegistrar;import org.springframework.boot.web.servlet.ErrorPageRegistry;import org.springframework.http.HttpStatus;//@Componentpublic class CommonErrorPageRegistrar implements ErrorPageRegistrar { @Override public void registerErrorPages(ErrorPageRegistry registry) { ErrorPage e404 = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"); ErrorPage e500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html"); registry.addErrorPages(e404, e500); }}
2 控制器中的异常
package da.test;import java.io.FileNotFoundException;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class TestController02 { @RequestMapping("/interceptor/login") public String login() { System.out.println("============login========="); return "login"; } @GetMapping("/user/help") public String help() { throw new IllegalArgumentException("出异常啦!"); } @ExceptionHandler(value = Exception.class) public String error(Exception e) { return "exception:" + e.getMessage(); } @GetMapping("/user/error1") public String error1() throws FileNotFoundException { throw new FileNotFoundException("出异常啦1!"); } @GetMapping("/user/error2") public String error2() throws ClassNotFoundException { throw new ClassNotFoundException("出异常啦2!"); }}
3 全局异常处理
package da.test;import org.springframework.web.bind.annotation.ControllerAdvice;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseBody;@ControllerAdvicepublic class GlobalExceptionHandler { @ExceptionHandler(Exception.class) @ResponseBody public String errorHandler(Exception e) { return "globel exception:" + e.getClass().getName(); }}
Tomcat的一些配置:
#设置访问地址与访问端口#server.address=192.168.1.104#server.port=8888#开启tomcat的日志功能#server.tomcat.accesslog.enabled=true#server.tomcat.accesslog.directory=e:/tmp/logs#server.tomcat...开启其他tomcat的功能
其他内容:
#指定启动端口,默认8080server.port=8888
springboot默认静态资源的路径:
classpath:[/WEB-INF/resources/,/resources/,/static/,/public/]可以通过spring.resources.staticLocations配置项修改默认静态资源的路径
阅读全文
1 0
- springboot学习笔记三
- Springboot学习笔记(三)
- SpringBoot学习笔记(三) SpringBoot 自动配置原理
- Springboot学习笔记(三)JPA-Hibernate
- springboot学习笔记(三) Restful API
- springboot学习笔记(三):基于MySql数据库的JPA操作
- SpringBoot学习笔记(三) 常用注解及开发工具
- SpringBoot学习笔记(三):EnableAutoConfiguration作用和机制
- SpringBoot 学习笔记
- SpringBoot 学习笔记 2
- springboot学习笔记(一)
- SpringBoot学习笔记(一)
- springboot学习笔记
- springboot学习笔记
- springboot学习笔记
- SpringBoot学习笔记一
- SpringBoot学习笔记--HelloWorld
- SpringBoot学习笔记
- 第三周项目1
- 电路板线路板 PCB 开发设计分享教程之( 4) --芯片元件库 的绘制--1.DIP 元件的原理图库
- TCP三次握手和四次挥手内容
- android学习---活动的启动方式
- PostgreSQL SQL的性能调试方法 查看执行计划
- springboot学习笔记三
- IDEA的git提交总是提交多余的东西
- Memcached 分布式缓存实现原理
- 数据对齐
- Tiles框架教程 简单规则 解决匹配问题
- 例题6-15 给任务排序(Ordering Tasks, UVa 10305)
- Intel Complier
- Sublime Text 3 Build 3143 注册码
- 最大平均值子数组