在上一篇教程中代码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