一.初识SpringBoot(入门级02)

发布时间:2020-07-12 10:10:50 作者:wx5995bc765bd70
来源:网络 阅读:213

1.1 Freemarker 其他用法


@RequestMapping("/freemarkerIndex")
public String index(Map result) {

  result.put("name", "yushengjun");
  result.put("sex", "0");
  List listResult = new ArrayList();
  listResult.add("zhangsan");
  listResult.add("lisi");
  listResult.add("itmayiedu");
  result.put("listResult", listResult);
  return "index";

}

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title>首页</title>
</head>
<body>
${name}
<#if sex=="1">

<#elseif sex=="2">

<#else>
其他

  </#if>      
 <#list userlist as user>
   ${user}
 </#list>

</body>
</html>

1.2 Freemarker配置
新建application.properties文件

spring.freemarker.allow-request-override=false
    spring.freemarker.cache=true
    spring.freemarker.check-template-location=true
    spring.freemarker.charset=UTF-8
    spring.freemarker.content-type=text/html
    spring.freemarker.expose-request-attributes=false
    spring.freemarker.expose-session-attributes=false
    spring.freemarker.expose-spring-macro-helpers=false
    #spring.freemarker.prefix=
    #spring.freemarker.request-context-attribute=
    #spring.freemarker.settings.*=
    spring.freemarker.suffix=.ftl
    spring.freemarker.template-loader-path=classpath:/templates/
    #comma-separated list
    #spring.freemarker.view-names= # whitelist of view names that can be resolved

1.3 使用JSP渲染Web视图
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<!-- SpringBoot 核心组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>

1.4 在application.properties创建以下配置
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

1.5 后台代码@Controller
br/>@Controller

   @RequestMapping("/index")
   public String index() {
         return "index";
    }

}

注意:创建SpringBoot整合JSP,一定要为war类型,否则会找不到页面.
不要把JSP页面存放在resources// jsp 不能被访问到

1.6 全局捕获异常
@ExceptionHandler 表示拦截异常
• @ControllerAdvice 是 controller 的一个辅助类,最常用的就是作为全局异常处理的切面类
• @ControllerAdvice 可以指定扫描范围
• @ControllerAdvice 约定了几种可行的返回值,如果是直接返回 model 类的话,需要使用 @ResponseBody 进行 json 转换
o 返回 String,表示跳到某个 view
o 返回 modelAndView
o 返回 model + @ResponseBody

@ControllerAdvice
public class GlobalExceptionHandler {

        @ExceptionHandler(RuntimeException.class)
        @ResponseBody
        public Map<String, Object> exceptionHandler() {
            //开发中,一般都会讲错误日志记录在日志中,全局捕获异常使用AOP技术,采用异常通知
            //如果每个方法都可能发生异常,每个方法上都加上try
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("errorCode", "500");
                    map.put("errorMsg", "全局捕获系統错误!");
                    return map;
        }

}

推荐阅读:
  1. 初识恶意代码之------网络蠕虫
  2. puppet 初识

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java编程 bo 入门级

上一篇:容器控件ScrollViewer控件

下一篇:无法定位程序输入点_Core_TestDevTransportAbility@4于动态链接库HCCore.dll上

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》