`
liudong19870227
  • 浏览: 31833 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

RestEasy+Backbone初试

阅读更多

由于Backbone中的请求是rest风格的,所以后台我选择的RestEasy来提供服务。

前端代码还是采用的我之前的一片文章http://webexpressor.iteye.com/blog/1608830

一、后台代码

主要目的是以json的格式返回一个BookList

1.我的项目是采用的maven来控制jar包的,pom文件为:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test.backbone</groupId>
  <artifactId>backbone</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <dependencies>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>2.3.0.GA</version>
        </dependency>
  	<dependency>
  		<groupId>org.jboss.resteasy</groupId>
  		<artifactId>resteasy-jackson-provider</artifactId>
  		<version>2.1.0.GA</version>
  	</dependency>
  </dependencies>
  
  <build>
		<plugins>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>tomcat-maven-plugin</artifactId>
				<version>1.1</version>
				<configuration>
					<!-- default 8080 -->
					<port>9090</port>
					<path>/</path>
					<uriEncoding>utf-8</uriEncoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

 这时项目所以来的jar包就导入进来了,注意这里使用的是jackson provider来格式化json的,它返回的json中的格式为:[{"author":"zhangsan","isbn":"110123-456-789","title":"linux"},{"author":"lisi","isbn":"110123-456-789","title":"ejb"},{"author":"wangwu","isbn":"110123-456-789","title":"javascript"},{"author":"liudong","isbn":"110123-456-789","title":"java"},{"author":"liudong","isbn":"110123-456-789","title":"mysql"},{"author":"liudong","isbn":"110123-456-789","title":"resteasy"}]

这正是Backbone所需要的格式(Backbone好像不支持{"bookList":[{},{}]}这种格式)。

  2.配置web.xml

<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>Restful Web Application</display-name>
	
 	<context-param>
		<param-name>javax.ws.rs.Application</param-name>
		<param-value>com.backbone.service.MyApplication</param-value>
	</context-param>
 	
	<listener>
		<listener-class>
			org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
		</listener-class>
	</listener>
 
	<servlet>
		<servlet-name>resteasy-servlet</servlet-name>
		<servlet-class>
			org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
		</servlet-class>
	</servlet>
 
	<servlet-mapping>
		<servlet-name>resteasy-servlet</servlet-name>
		<url-pattern>/app/*</url-pattern>
	</servlet-mapping>
 
</web-app>

  3.定义Book类

package com.backbone.model;

public class Book {
	private String author;
	private String ISBN;
	private String title;

	public Book() {
	}

	public Book(String author, String ISBN, String title) {
		this.author = author;
		this.ISBN = ISBN;
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getISBN() {
		return ISBN;
	}

	public void setISBN(String ISBN) {
		this.ISBN = ISBN;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
}

 4.编写bookService

package com.backbone.service;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import com.backbone.model.Book;

@Path("/app/book")
public class BookService {

	@GET
	@Path("/get")
	@Produces("application/json")
	public List<Book> findBook(){
		List<Book> books = new ArrayList<Book>();
		books.add(new Book("zhangsan","110123-456-789","linux"));
		books.add(new Book("lisi","110123-456-789","ejb"));
		books.add(new Book("wangwu","110123-456-789","javascript"));
		books.add(new Book("liudong","110123-456-789","java"));
		books.add(new Book("liudong","110123-456-789","mysql"));
		books.add(new Book("liudong","110123-456-789","resteasy"));
		return books;
	}
}

 这里配置了访问的路径,及返回的格式

5.编写resteasy入口Application

package com.backbone.service;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;

public class MyApplication extends Application{
	private Set<Object> singletons = new HashSet<Object>();
	private Set<Class<?>> empty = new HashSet<Class<?>>();
	
	public MyApplication(){
		singletons.add(new BookService());
	}

	@Override
	public Set<Class<?>> getClasses() {
		return empty;
	}

	@Override
	public Set<Object> getSingletons() {
		return singletons;
	}
	
}

 到这一步服务器已经写好了,启动tomcat,在地址栏里输入http://localhost:9090/app/book/get 就可以看到服务器返回的结果了

 

二、客户端代码

  1.app.js

$(document).ready(function(){
	var Book = Backbone.Model.extend({});

	var BookCollection = Backbone.Collection.extend({
		model: Book
	});

	var BookView = Backbone.View.extend({  
	    tagName:'li',  
	    //_.template是Underscore中的方法  
	    template:_.template($('#item-template').html()),  
	    // **render** is the core function that your view should override, in order  
	    // to populate its element (`this.el`), with the appropriate HTML. The  
	    // convention is for **render** to always return `this`.  
	    render: function() {  
	        $(this.el).html(this.template(this.model.toJSON()));  
	        return this;//always return this  
	    },  
	    events:{  
	        "click":"viewDetail"//绑定单击事件  
	    },  
	    viewDetail:function(){  
	        console.log(this.model.toJSON());  
	        alert('你选择了'+this.model.get('title'));  
	    }  
	});  
	var AppView = Backbone.View.extend({
		el: $('#bookList'),
		initialize: function(){
			var that = this;
			this.books = new BookCollection;
			this.books.fetch({
				method:'get',
				url:'/app/book/get',
				success:function(data){
					console.log(data);
					that.books.add(data.models);
					that.render();
				}
			});
		},
		render: function(){
			console.log(this.books.models);
			var that = this;
			_.each(this.books.models, function(item){
				//动态传入model数据
				var bookView = new BookView({model: item});
				that.$el.append(bookView.render().el);
			},this);
		}
	});

	var app = new AppView;

});
 

2.index.html

<!doctype html>
<html>
	<head>
		<meta charset="utf-8"/>
	</head>
	<body>
		<ul id="bookList"></ul>
		<script src="js/jquery-1.7.1.min.js"></script> 
		<script src="js/underscore-min.js"></script> 
		<script src="js/backbone.0.9.2.js"></script> 
		<script src="js/app.js"></script> 
		<script type="text/template" id="item-template">
			author:<%=author %><br/>
			ISBN:<%= isbn %><br/>
			title:<%= title %><br/>
		</script>
	</body>
</html>

 

整个项目就搭建好了,输入http://localhost:9090/index.html 可以进行测试

 

整个项目代码我放上来了,供参考。

 

1
0
分享到:
评论

相关推荐

    RESTEasy +Jackson 2 进行JSON转换

    RESTEasy和Jackson 2是两个在Java开发中广泛使用的库,它们在构建RESTful Web服务和处理JSON数据方面发挥着重要作用。这篇博文将探讨如何利用RESTEasy和Jackson 2进行JSON转换,以便在Web应用程序中有效地传输和解析...

    Spring整合RestEasy示例工程源码

    **Spring整合RestEasy示例工程源码解析** 在现代Web开发中,Spring框架因其强大的功能和灵活性而被广泛采用,而RESTful API设计已经成为服务端与客户端交互的标准方式。RestEasy是一个优秀的Java RESTful Web ...

    resteasy-guice-mybatis:resteasy + guice + mybatis + mysql演示

    【resteasy-guice-mybatis:resteasy + guice + mybatis + mysql演示】这个项目是一个集成多种技术的Java Web应用程序示例。它展示了如何将RESTful服务(使用RESTEasy框架)、依赖注入(通过Guice实现)、持久层操作...

    resteasy使用netty

    RestEasy与Netty结合使用,可以构建高性能的RESTful服务,摆脱传统的Servlet容器如Tomcat的依赖。RestEasy是JBoss公司开发的一个Java框架,它实现了JSR 311和JSR 339(Java API for RESTful Web Services)标准,...

    resteasy-spring-netty:Resteasy +Spring+净值

    宁静的春天 Resteasy + Spring + Netty示例 作为spring bean注入resteasy提供者/控制器 验证 ==================== 在Main.java中运行 测试

    Spring-Rest:Spring + Resteasy + Maven + AngularJS + Karma + Jasmine + Wildfly Poc

    春假Spring + Resteasy + Maven + JUnit + Mockito + AngularJS + Karma + Jasmine + Wildfly POC 这是一个使用上述技术的 POC。 该项目已使用 Maven 构建,并设置为在 Wildfly 8.x 中运行。 要对其进行测试,请配置...

    Jetty整合RestEasy开发RESTful web service的例程

    在本文中,我们将深入探讨如何将Jetty与RestEasy结合,以开发高效的RESTful Web服务。Jetty是一款轻量级、高性能的Java Servlet容器,而RestEasy是JAX-RS(Java API for RESTful Web Services)的一个实现,使得创建...

    可查询:Quarkus + Resteasy + Hibernate Panache

    Quarkus、Resteasy 和 Hibernate Panache 是三个在 Java 开发领域中非常重要的技术,尤其在构建现代化的、高性能的微服务应用时。本文将详细探讨这三个组件如何协同工作,以及它们各自的关键特性。 首先,Quarkus ...

    restEasy3.0.18+jboss7案例

    案例基于 jboss 7.1.0 和restEasy3.0.18,是目前最新的,因为在网上看到restEasy的例子都比较旧了,所以想到自己写一个案例。直接进入正题。 因为使用的是jboss7,所以restEasy已经集成在里面了,直接用就行,甚至...

    Netty-Resteasy-Spring

    Resteasy + Spring + Netty sample Inject resteasy provider / controllers as spring bean Authentication Run at Main.java Test http://localhost:8082/resteasy/hello/world 教程 jax-rs规范用法: ...

    RESTfulExample

    RESTEasy + Spring integration example http://localhost:8080/RESTfulExample/customer/print JBoss RESTEasy 是一个用来使用Java语言开发 RESTFul Web服务的框架。

    netty-resteasy-spring:Netty 、 RESTEasy 、 Spring 的集成演示

    resteasy-spring-netty Resteasy + Spring + Netty sample Inject resteasy provider / controllers as spring bean Authentication ===================== Run at Main.java Test 教程 jax-rs规范用法: resteasy ...

    SpringMvc+mybatis+fastJson+druid+resteasy...

    开发工具采用IDEA,用maven构建的项目,框架采用SpringMVC,数据库采用mysql与mybatis配合,数据格式采用阿里巴巴工具fastJson,数据源druid,另外还添加了resteasy中的功能

    resteasy手册

    resteasy

    guice + mybatis+ resteasy

    标题 "guice + mybatis+ resteasy" 涉及到三个主要的开源技术,它们在Java开发领域中都有重要应用。以下是对这三个技术的详细解释: 1. Guice(发音同juice): Guice是Google提供的一款轻量级的依赖注入框架,它...

    RESTEasy实现上传下载文件

    在本文中,我们将深入探讨如何使用RESTEasy和Guice框架实现文件的上传与下载功能。RESTEasy是一个基于Java的RESTful Web服务实现,它与Java EE应用服务器集成,而Guice是Google提供的一款轻量级依赖注入框架,帮助...

    resteasy helloworld demo

    【RESTEasy HelloWorld 示例详解】 RESTEasy 是一个开源的 Java 框架,它实现了 JAX-RS(Java API for RESTful Web Services)规范,使得开发者能够轻松地在 Java 应用程序中创建 RESTful 服务。JAX-RS 是一种用于...

    resteasy 需要jar包

    Resteasy 是一个开源的 JAX-RS 实现,它允许开发者构建 RESTful Web 服务,并在 Java 应用程序中轻松地使用这些服务。JAX-RS(Java API for RESTful Web Services)是 Java 中的一个标准,用于创建基于 HTTP 的 REST...

    使用RESTEasy构建WebService简介

    RESTEasy是一个开源的JAX-RS实现,它允许开发者以简单的方式构建RESTful Web服务。JAX-RS是Java API for RESTful Web Services的缩写,是一个Java编程语言的API,用于开发Web服务和基于Web的应用程序。RESTEasy不仅...

Global site tag (gtag.js) - Google Analytics