Tapestry5.3.8 Web Services Development

Source: Internet
Author: User

or directly on the code:

Webservices.tml:

<! DOCTYPE html>

Webservices.java:

public class WebServices {}

Then the page will grow to the death:

Personfind.java (Output object to Tapestry page):

/** * @package:com.changhongit.andy.pages.ws<br> * @author:wanglongjie<br> * @createDate: September 16, 2015 PM 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: September 16, 2015 2:41:16 & lt;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;}}

Dependency conversion to XML entity class Wsperson.java, this guy can't be placed in the pages directory, or he will "hang up"

/** * @package:example.crud.entity<br> * @author:wanglongjie<br> * @createDate: August 17, 2015 morning 9:26:32&LT;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: User entity class <br> * @author: WANGLONGJIE&LT;BR > * @createDate: August 17, 2015 Morning 9:26:32<br> * @updater:<br> * @updateDate:<br> * @updateContent: <b r> */@SuppressWarnings ("Serial") @XmlRootElement (name = "People") @XmlAccessorType (Xmlaccesstype.field) public Class Wspeople implements Serializable {/** * ID good */@XmlAttribute (name = "id") Private Long id;/** * Name */private String n ame;/** * Age */private int age;/** * Birthday */private Date birthday;/** * Sex */private String gender;/** * InterestHobby */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 = Hob by;} 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.) {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 + "]";}}


Through the annotations, this guy is already hundred poison, can easily convert to XML file.

Click "PERSONFIND/1" on the page to jump to the Personfind page, as shown below:

In this context, Tapestry created a standard as a rest-like Web service.

********************************************************************************************************

The point is, as said above: Wsperson.java if placed under the pages package, it will report an exception error: Class Com.changhongit.andy.pages.ws.WsPeople has been transformed and May is directly instantiated.

said that this class has been converted can not be directly instantiated, which is probably why the Tapestry page class are placed in pages under the package, must have done some processing, as to how to deal with, sorry I have not read the source!


Tapestry5.3.8 Web Services Development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.