您的位置:首页 > 编程语言 > Java开发

spring实战-将模型传递到视图中渲染

2017-08-14 22:50 204 查看
第二篇:构建基本数据模型,并将模型传递到视图渲染

在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...

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: