Tapestry5.3.8 Web Services开发
2015-09-16 16:09
381 查看
还是直接上代码:
WebServices.tml:
WebServices.java:
然后页面就长成这死样子了:
![](http://static.oschina.net/uploads/space/2015/0916/161204_best_1252840.png)
PersonFind.java (向Tapestry页面输出对象了):
依赖转化为XML的实体类 WsPerson.java , 这家伙不能放在pages目录下,不然他会“挂掉的”
通过注解,这家伙已经百毒不侵了,可以轻松的转化为XML文件。
点击页面上的“PersonFind/1“,跳转到PersonFind 页面,内容如下所示:
![](http://static.oschina.net/uploads/space/2015/0916/161233_vBZ7_1252840.png)
就此,Tapestry 就创建了一个标准的 as a REST-like web service.
********************************************************************************************************
重点来了,正如上边所说:WsPerson.java 如果放在pages包下,就会报异常错误:Class com.changhongit.andy.pages.ws.WsPeople has been transformed and may not be directly instantiated.
![](http://static.oschina.net/uploads/space/2015/0916/160648_4WlE_1252840.png)
说这个类已经被转化不能直接实例化,这大概就是为什么Tapestry的页面类都要放在pages包下,肯定是已经做过一些处理,至于怎么处理的,抱歉我也没看过过源码!
WebServices.tml:
<!DOCTYPE html> <html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd" xmlns:p="tapestry:parameter"> <head> <meta charset="UTF-8" /> <title>学习Tapestry5.3.8</title> <link rel="stylesheet" type="text/css" href="${context:css/examples/examples.css}" /> </head> <body> <h1>Web Services</h1> Here we have a link to a Tapestry page that acts as a REST-like web service. <br /> <br /> <div class="eg"> <a t:type="pagelink" t:page="ws/PersonFind" t:context="literal:1" href="#">PersonFind/1</a> </div> </body> </html>
WebServices.java:
public class WebServices { }
然后页面就长成这死样子了:
![](http://static.oschina.net/uploads/space/2015/0916/161204_best_1252840.png)
PersonFind.java (向Tapestry页面输出对象了):
/** * @package :com.changhongit.andy.pages.ws<br> * @author :wanglongjie<br> * @createDate :2015年9月16日下午2:41:16<br> */ package com.changhongit.andy.pages.ws; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import javax.servlet.http.HttpServletResponse; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import org.apache.tapestry5.StreamResponse; import org.apache.tapestry5.ioc.annotations.Inject; import org.apache.tapestry5.services.Response; import com.changhongit.andy.dao.PeopleDao; import com.changhongit.andy.entity.People; import com.changhongit.andy.entity.ws.WsPeople; /** * @package :com.changhongit.andy.pages.ws<br> * @file :PersonFind.java<br> * @describe :<br> * @author :wanglongjie<br> * @createDate :2015年9月16日下午2:41:16<br> * @updater :<br> * @updateDate :<br> * @updateContent :<br> */ public class PersonFind { @Inject private PeopleDao peopleDao; Object onActivate(final Long personId) { System.out.println("onActivate..." + personId); return new StreamResponse() { private InputStream inputStream; @Override public void prepareResponse(Response response) { // TODO Auto-generated method stub WsPeople wsPeople = findPerson(personId); if (wsPeople == null) { try { response.sendError(HttpServletResponse.SC_NOT_FOUND, "Resource not found"); } catch (IOException e) { // TODO: handle exception throw new RuntimeException("failed to redirect?", e); } } else { try { JAXBContext context = JAXBContext.newInstance(wsPeople .getClass()); Marshaller marshaller = context.createMarshaller(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); marshaller.marshal(wsPeople, outputStream); inputStream = new ByteArrayInputStream( outputStream.toByteArray()); response.setHeader("Content-Length", outputStream.size() + ""); } catch (JAXBException e) { // TODO: handle exception throw new IllegalStateException("write failure!", e); } } } @Override public InputStream getStream() throws IOException { // TODO Auto-generated method stub return inputStream; } @Override public String getContentType() { // TODO Auto-generated method stub return "text/xml"; } }; } public WsPeople findPerson(Long personId) { WsPeople wsPeople = null; People people = peopleDao.findById(personId); System.out.println(people); if (people != null) { wsPeople = new WsPeople(people.getId(), people.getName(), people.getAge(), people.getBirthday(), people.getGender(), people.getHobby()); } return wsPeople; } }
依赖转化为XML的实体类 WsPerson.java , 这家伙不能放在pages目录下,不然他会“挂掉的”
/** * @package :example.crud.entity<br> * @author :wanglongjie<br> * @createDate :2015年8月17日上午9:26:32<br> */ package com.changhongit.andy.entity.ws; import java.io.Serializable; import java.util.Date; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; /** * @package :example.crud.entity<br> * @file :People.java<br> * @describe :用户 实体类<br> * @author :wanglongjie<br> * @createDate :2015年8月17日上午9:26:32<br> * @updater :<br> * @updateDate :<br> * @updateContent :<br> */ @SuppressWarnings("serial") @XmlRootElement(name = "people") @XmlAccessorType(XmlAccessType.FIELD) public class WsPeople implements Serializable { /** * ID 好 */ @XmlAttribute(name = "id") private Long id; /** * 姓名 */ private String name; /** * 年龄 */ private int age; /** * 生日 */ private Date birthday; /** * 性别 */ private String gender; /** * 兴趣爱好 */ private String hobby; public WsPeople() { } public WsPeople(Long id, String name, int age, Date birthday, String gender, String hobby) { this.id = id; this.name = name; this.age = age; this.birthday = birthday; this.gender = gender; this.hobby = hobby; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; } @Override public String toString() { return "People [id=" + id + ", name=" + name + ", age=" + age + ", birthday=" + birthday + ", gender=" + gender + ", hobby=" + hobby + "]"; } }
通过注解,这家伙已经百毒不侵了,可以轻松的转化为XML文件。
点击页面上的“PersonFind/1“,跳转到PersonFind 页面,内容如下所示:
![](http://static.oschina.net/uploads/space/2015/0916/161233_vBZ7_1252840.png)
就此,Tapestry 就创建了一个标准的 as a REST-like web service.
********************************************************************************************************
重点来了,正如上边所说:WsPerson.java 如果放在pages包下,就会报异常错误:Class com.changhongit.andy.pages.ws.WsPeople has been transformed and may not be directly instantiated.
![](http://static.oschina.net/uploads/space/2015/0916/160648_4WlE_1252840.png)
说这个类已经被转化不能直接实例化,这大概就是为什么Tapestry的页面类都要放在pages包下,肯定是已经做过一些处理,至于怎么处理的,抱歉我也没看过过源码!
相关文章推荐
- Android学习笔记之——DatePicker&TimePicker
- jsp的四大作用域
- 理解linux 的 inode][解决inode耗尽问题
- 博客
- vi编辑器的使用(3)
- Android DiskLruCache完全解析,硬盘缓存的最佳方案
- JSP 9大内置对象及4大作用域
- CentOS 7 64位 安装 PostgreSQL 9.2整理
- js给文本框赋值 value与innerHTML
- 什么是PRD? 什么是MRD? 什么是BRD?
- HBase入门
- 【hihoCoder】#1086: Browser Caching (微软笔试题)
- js解决浏览器自动填充账号密码
- View(一)
- 类模板的定义和使用方法
- hive之HQL用法案例
- AVFoundation总结
- poi实现数据的导出
- Android Canvas Layers
- vi编辑器的使用(2)