標籤:
還是直接上代碼:
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 webservice.<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 {}
然後頁面就長成這死樣子了:
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 {@Injectprivate PeopleDao peopleDao;Object onActivate(final Long personId) {System.out.println("onActivate..." + personId);return new StreamResponse() {private InputStream inputStream;@Overridepublic void prepareResponse(Response response) {// TODO Auto-generated method stubWsPeople wsPeople = findPerson(personId);if (wsPeople == null) {try {response.sendError(HttpServletResponse.SC_NOT_FOUND,"Resource not found");} catch (IOException e) {// TODO: handle exceptionthrow 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 exceptionthrow new IllegalStateException("write failure!", e);}}}@Overridepublic InputStream getStream() throws IOException {// TODO Auto-generated method stubreturn inputStream;}@Overridepublic String getContentType() {// TODO Auto-generated method stubreturn "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;}@Overridepublic String toString() {return "People [id=" + id + ", name=" + name + ", age=" + age+ ", birthday=" + birthday + ", gender=" + gender + ", hobby="+ hobby + "]";}}
通過註解,這傢伙已經百毒不侵了,可以輕鬆的轉化為XML檔案。
點擊頁面上的“PersonFind/1“,跳轉到PersonFind 頁面,內容如下所示:
就此,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.
說這個類已經被轉化不能直接執行個體化,這大概就是為什麼Tapestry的頁面類都要放在pages包下,肯定是已經做過一些處理,至於怎麼處理的,抱歉我也沒看過過源碼!
Tapestry5.3.8 Web Services開發