前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Jboot教程(二)

Jboot教程(二)

作者头像
南风
发布2019-04-22 16:39:17
6170
发布2019-04-22 16:39:17
举报
文章被收录于专栏:Java大联盟Java大联盟Java大联盟

Java大联盟

致力于最高效的Java学习

在上一篇教程中代码Demo中在使用了renderText方法,它是渲染器的一种方式,JBoot中有很多种渲染器,如下:

指令

描述

render("test.html")

渲染名为test.html的试图,该试图的全路径为"/path/test.html"。

render("/other_path/test.html")

渲染名为test.html的视图,该试图的全路径为"other_path/test.html",即当参数以"/"开头时将采用绝对路径。

renderTemplate("test.html")

渲染名为test.html的视图,且视图类型为JFinalTemplate。

renderFreeMarker("test.html")

渲染名为test.html的视图,且视图类型为FreeMarker。

renderJsp("test.jsp")

渲染名为test.jsp 的视图,且视图类型为 Jsp。

renderVelocity("test.html")

渲染名为 test.html 的视图,且视图类型为 Velocity。

renderJson()

将所有通过 Controller.setAttr(String, Object)设置的变量转换成 json 数据并渲染。

renderJson("users",userList)

以"users"为根,仅将 userList 中的数据转换成 json数据并渲染。

renderJson(user)

将 user 对象转换成 json 数据并渲染。

renderJson("{\" age\":18}")

直接渲染 json 字符串。

renderJson(new String[]{"user","blog"})

仅将 setAttr("user", user)与 setAttr("blog", blog)设置的属性转换成 json 并渲染。使用 setAttr 设置的其它属性并不转换为 json。

renderFile("test.zip")

渲染名为 test.zip 的文件,一般用于文件下载。

renderText("Hello Jboot")

渲染纯文本内容"Hello Jboot"。

renderHtml("Hello Html")

渲染 Html 内容"Hello Html"。

renderError(404,"test.html")

渲染名为 test.html 的文件,且状态为 404。

renderError(500,"test.html")

渲染名为 test.html 的文件,且状态为 500。

renderNull()

不渲染,即不向客户端返回数据。

render(new MyRender())

使用自定义渲染器 MyRender 来渲染。

getPara系列方法:

Controller提供了getPara系列方法用来从请求中获取参数。getPara系列方法分为两类。第一种类型为第一个形参为String的getPara系列方法。该系列方法是对HttpServletRequest.getParameter(String name)的封装,这类方法都是转调了HttpServletRequest.getParameter(String name).。第二种类型为第一个形参为int或无参的getPara系列方法。该系列方法是去获取urlPara中所带的参数值。getParaMap与getParaNames分别对应HttpServletRequest的getParameterMap与getParameterNames。

方法调用

返回值

GetPara("title")

返回页面表单域名为"title"参数值。

getParaToInt("age")

返回页面表单域名为"age"的参数值并转为int型。

getPara(0)

返回url请求中的urlPara参数的第一个值。

getParaToInt(1)

返回url请求中的urlPara参数的第二个值并转换成int型。

getPara()

返回url请求中的urlPara参数的整体值。

在这里我们用登录的方式做一个前后台交互的过程:

1. 新建一个maven项目并在pom文件中添加jboot依赖。

2. 创建jboot服务的启动。

3. 在maven项目中的resources中新建一个登录页面login.html并添加相关的css与js。

并使用ajax向后台传输json数据。

4. 在后台的Controller中使用getPara("")的方式接受参数。

在接受前台参数时也可以使用注解@Para的方式。

5.启动项目,在登录页面中输入用户名admin和密码123456,登录后会发现控制台也打印了结果。

6.页面渲染使用了renderJson,在页面的ajax中将结果console出来。

这就是前后台交互的简单过程,其他方法大家可以试试,这里就不一一举例了。

源码:

github

https://github.com/southwind9801/Jboot.git

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-06-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Java大联盟 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档