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

第二篇构建基本数据模型,并将模型传递到视图渲染

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

	/**
	 * 不带域接口地址,必须以"/"开头
	 */
	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...

}






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值