spring实战-将模型传递到视图中渲染
2017-08-14 22:50
204 查看
第二篇:构建基本数据模型,并将模型传递到视图渲染
在SpringMVC中将模型传递到前端有多种方式,可以通过Model,将数据传到前端,也可以通过控制器返回值将数据传递到前端
在web层我们主要通过Model将数据传到前端,在后面Service层我们会主要通过返回值+@ResponseBody注解的方式,将数据回传客户端
IdatController
在SpringMVC中将模型传递到前端有多种方式,可以通过Model,将数据传到前端,也可以通过控制器返回值将数据传递到前端
在web层我们主要通过Model将数据传到前端,在后面Service层我们会主要通过返回值+@ResponseBody注解的方式,将数据回传客户端
IdatController
package com.halfworlders.idat.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.halfworlders.idat.dto.Interface; import com.halfworlders.idat.service.IdatService; /** * Idat控制器 * * @createTime 2017-08-14 * @version 0.0.1 * @author lllhappy * */ @Controller @RequestMapping("/idat") public class IdatController { @Autowired private IdatService idatService; /** * * @param model * @return */ @RequestMapping(value = "/interfaces", method=RequestMethod.GET) public String interfaces(Model model){ List<Interface> interfaces = idatService.listAll(); // model实际上就是一个Map,会将数据传递给视图,这样就可以将数据渲染到客户端了 model.addAttribute("interfaces",interfaces); return "interfaces"; } }IdatService
package com.halfworlders.idat.service; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Service; import com.halfworlders.idat.dto.Interface; import com.halfworlders.idat.dto.Parameter; @Service public class IdatService { public List<Interface> listAll() { List<Interface> interfaces = new ArrayList<>(); List<Parameter> parameters = new ArrayList<>(); parameters.add(new Parameter("name","String", true, 32, "姓名", "lllhappy")); parameters.add(new Parameter("sex","int", true, 1, "姓别", "lllhappy")); interfaces.add(new Interface("用户查询", "http://halfworlders.com","/getAllUser",parameters,"查询所有用户信息", "lllhappy")); return interfaces; } }interfaces.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <style> table{ background:#000} table td{ background:#FFF} </style> <body> <h2>Interface</h2> <table> <thead> <tr> <td>名称</td> <td>地址</td> <td>描述</td> </tr> </thead> <tbody> <c:forEach items="${interfaces}" var="intf"> <tr> <td><c:out value="${intf.name }"/></td> <td><c:out value="${intf.domain }"/><c:out value="${intf.url }"/></td> <td><c:out value="${intf.description }"/></td> </tr> </c:forEach> </tbody> </table> </body> </html>Interface
package com.halfworlders.idat.dto; import java.util.Date; import java.util.List; public class Interface { /** * id */ private String id; /** * 接口名称 */ private String name; /** * 域,必须以http或者https开头 */ private String domain; /** * 不带域接口地址,必须以"/"开头 * a9bb / private String url; /** * 参数列表 */ private List<Parameter> params; ... public Interface() { } public Interface(String name, String domain, String url, List<Parameter> params, String description, String operator) { this(null, name, domain, url, params, description, operator, operator, new Date(), new Date(), 1); } public Interface(String id, String name, String domain, String url, List<Parameter> params, String description, String creater, String lastUpdater, Date createTime, Date lastUpdateTime, Integer version) { super(); this.id = id; this.name = name; this.domain = domain; this.url = url; this.params = params; this.description = description; this.creater = creater; this.lastUpdater = lastUpdater; this.createTime = createTime; this.lastUpdateTime = lastUpdateTime; this.version = version; } get...set... }Parameter
package com.halfworlders.idat.dto; import java.util.Date; public class Parameter { /** * id */ private String id; /** * 参数名 */ private String name; /** * 参数类型 */ private String type; /** * 是否必填参数 */ private Boolean required; /** * 参数最大长度 */ private Integer length; ... public Parameter() { } public Parameter(String name, String type, Boolean required, Integer length, String description, String operator) { this(null, name, type, required, length, description, operator, operator, new Date(), new Date(), 1); } public Parameter(String id, String name, String type, Boolean required, Integer length, String description, String creater, String lastUpdater, Date createTime, Date lastUpdateTime, Integer version) { super(); this.id = id; this.name = name; this.type = type; this.required = required; this.length = length; this.description = description; this.creater = creater; this.lastUpdater = lastUpdater; this.createTime = createTime; this.lastUpdateTime = lastUpdateTime; this.version = version; } get...set... }
相关文章推荐
- 一天学会MVC3之传递模型数据到视图
- opengl es 渲染方式与 纹理坐标设定 ,OpenGL ES 模型视图之缩放操作
- ASP.NET MVC从视图传递多个模型到Controller
- ASP.NET MVC从视图传递多个模型到Controller
- Spring MVC 传递模型数据到视图中方法总结
- ASP.NET MVC ---模型在视图中的传递
- 渲染管线——视图坐标与相机模型
- ASP.NET MVC从视图传递多个模型到Controller
- ASP.NET MVC从视图传递多个模型到Controller
- Spring MVC 之传递模型数据到视图中
- Spring实战-读书笔记(六)-渲染Web视图
- ASP.NET MVC从视图传递多个模型到Controller
- 在ASP.NET MVC 模型中 选择最好的方法将多个model(数据模型)传递到视图
- MVC控制器中传递模型到视图中进行绑定
- asp.net mvc(2013-4-25)——使用模型为视图传递信息
- ASP.NET MVC从视图传递多个模型到Controller
- 【转】模型视图矩阵和投影矩阵(webgl笔记)
- 模板渲染和参数传递.
- 【J2EE设计模式】模型-视图-控制器模式(MVC模式)
- 在PowerDesigner中设计物理模型3——视图、存储过程和函数