当前位置: 首页 > news >正文

天河做网站长沙电商网站制作

天河做网站,长沙电商网站制作,网站后台培训学校,mysql网站开发创建Spring Boot项目#xff1a;使用Spring Initializr创建一个新的Spring Boot项目。依赖配置#xff1a;在pom.xml 文件中(方便起见使用的是thymeleaf模板引擎)#xff1a; dependenciesdependencygroupIdorg.springframework.boot/groupId使用Spring Initializr创建一个新的Spring Boot项目。依赖配置在pom.xml 文件中(方便起见使用的是thymeleaf模板引擎) dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-thymeleaf/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency /dependenciesHTML目录结构在src/main/resources/templates 目录下创建HTML视图。这里提供两个示例视图error-page.html 和 result-page.html error-page.html !DOCTYPE html html xmlns:thhttp://www.thymeleaf.org headtitle错误页面/title /head body h1错误/h1 p th:text${message}/p /body /htmlresult-page.html !DOCTYPE html html xmlns:thhttp://www.thymeleaf.org headtitle结果页面/title /head body h1结果/h1 p th:text结果是 ${result}/p /body /html定义自定义异常类创建一个自定义异常类例如 MyCustomException public class MyCustomException extends Exception {public MyCustomException(String message) {super(message);} }异常处理器类创建一个异常处理器类 MyCustomExceptionHandler并配置多个ExceptionHandler 方法来处理不同类型的异常 import com.lfsun.demolfsunstudythymeleafcustomexception.exception.MyCustomException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.servlet.ModelAndView;ControllerAdvice public class MyCustomExceptionHandler {ExceptionHandler(MyCustomException.class)public ModelAndView handleCustomException(MyCustomException ex) {ModelAndView modelAndView new ModelAndView();modelAndView.addObject(message, 自定义异常发生了 ex.getMessage());modelAndView.setViewName(error-page);return modelAndView;}ExceptionHandler(Exception.class)public ModelAndView handleAllOtherExceptions(Exception ex) {ModelAndView modelAndView new ModelAndView();modelAndView.addObject(message, 发生了其他异常 ex.getMessage());modelAndView.setViewName(error-page);return modelAndView;} }控制器类创建一个控制器类例如 DemoController并在其中抛出自定义异常 import com.lfsun.demolfsunstudythymeleafcustomexception.exception.MyCustomException; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam;Controller public class DemoController {GetMapping(/divide)public String divide(RequestParam int dividend, RequestParam int divisor, Model model) throws MyCustomException {if (divisor 0) {throw new MyCustomException(不允许除以零。);}int result dividend / divisor;model.addAttribute(result, result);return result-page;} }运行项目运行Spring Boot应用程序。访问应用程序在浏览器中访问 http://localhost:8080/divide?dividend10divisor2 这个URL将会执行 DemoController 中的 divide 方法并显示结果5。尝试访问 http://localhost:8080/divide?dividend10divisor0 来看到自定义异常处理器的效果。
http://www.zqtcl.cn/news/388676/

相关文章:

  • 单页网站怎么做外链网站js特效
  • 网站模板 黑白中国域名交易平台
  • 网站高端设计少儿编程加盟十大机构
  • 海尔网站建设的目标是什么财务公司业务范围
  • 天津做艺术品的网站2h1g做视频网站
  • 网站建设项目的预算百度推广登陆首页
  • 网站图片展示方式有哪些深圳做网站比较好天涯
  • 专业长春网站建设工作室安徽省工程建设信息网查询信息
  • 计算机网站开发实现总结关键词优化的作用
  • 网站流量统计模板商务网站安全方案设计
  • 做网站最专业的公司用php做的网站用什么数据库
  • 做网站可以不用框架吗网站301做下
  • 萍乡做网站深圳市福田区住房和建设局官网
  • 网站架构需求wordpress过去指定分类文章
  • 房管局备案查询网站功能型网站开发
  • 聊城手机网站建设服务自己开网站做职称论文可以吗
  • 企业网站禁忌手机端网站开发页
  • 深圳外贸商城网站建设wordpress 空搜索
  • 做微信的网站有哪些shop商城系统
  • 网站落地页如何做优化大师免费下载安装
  • 本地计算机做网站服务器做算命网站
  • 广州网站建设公司万齐网络科技做围棋题网站
  • 运动服装商城网站建设引流推广
  • 武进区城乡建设局网站聊城商城网站建设
  • 做网站开发赚钱吗网站建设电子书资料
  • wordpress 回收站在哪个文件夹建站之星模板好吗
  • 怎么用dw做博客网站天使投资平台官网
  • 淮安市网站建设crm网站
  • 门户网站主要特点和功能深圳地铁优化
  • 银川网站推广方式湖南建工交通建设有限公司网站