Tapestry Getting Started and Advanced One

zhaozj2021-02-17  48

To develop a Web Application, it is a bit difficult for a novice, Tapestry is different from

The previous Web Presentation Framework, so it is not possible, and the learning curve is longer. This is a fact.

Let me talk about a general structure of a web application:

With jbuider9 as a development tool, you must build a project first, for example, the name is TapestryExmap, it

Workspace is F: / myProject / TapeStryExmaple. The subdirectory and files below it are

BAK - this is a BACKUP directory of JBuider.

Build - My Ant's Work Contents, my Ant will use this catalog to compile packaging.

Classes - JBuilder Compiles Class Directory

Configs - where some of my configuration files are placed

Lib - the directory where I have to use the library file

Context - my web application's context directory

DOC - my documentation catalog

SRC - Directory of my Java Source

Build.properties - Attribute settings file for Ant

BUILD.XML - Definition file for Ant work

After you build this project, you need to build a Web Application, click File -> New -> Web -> Web

Application, because we need to use Tomcat4.1 debugging in JBuilder, use jboss3.x or not, but you

To download a plugin called entwizard_jboss3x_jb9_v3-1-5.zip, JBuiderx provides JB

OSS support, but JBuiderx often has some inexplicable blame, and is slower, I am not used.

When you build a web application, you have to enter both Name and Directory, name

PatientRecord, pay attention to the Name you entered here, will appear in the URL you will test in the future:

Your URL may be like this:

Http: // localhost: 8080 / patientRecord / app

The next app is configured in Web.xml, then say it.

Your web application's Directory value is the same as our scheduled, that is, the context directory,

Here is f: / myproject / tapestryexmaple / context, this context directory is Web Applicatio

n

The core directory, which will be explained in detail below.

For example, there are three directories like my context directory.

Home - My WEB Applicattion Submodule Home's Context

There is a subdirectory CSS - placed the CSS file used by this submodule

Images - Place the image of this submodule (Icon)

Document: Home.html, Register.xml ...

These are HTML Template corresponding to the page in my word module.

PatientRecord - a child module of my web applicattion PatientRecord's Context

The directory structure is the same as the Home submodule

WEB-INF - the most core directory

The subdirectories and files below have

Classes - Your Java Classes will have a copy here.

LIB - Your engineering referenced by Lib here with a copy

PatientRecord.Application - Your TapeStry core profile

Web.xml - Web Application Core Profile

Let's take the two files of patientRecord.Application and Web.xml, and my sample file is as follows:

Web.xml

http://java.sun.com/dtd/web-app_2_3.dtd>

Patient Record System

redirect

org.apache.tapestry.redirectfilter

redirect

/

patienceRecord

com.ht.web.patientRecordServlet

patienceRecord

/ app

15

This property has

Display-name - not important, just display name

Filter and filter-mapping configuration - do not move, use the default configuration,

Servlet configuration - this is very important, my servle name is PatientRecord, the corresponding class is

com.ht.web.patientRecordServlet, this is a class I developed by my own:

This class looks very simple:

Package com.ht.Web;

Import org.apache.tapestry.ApplicationServlet;

/ **

* @Version $ ID: SimpleServlet.java, V 1.9 2002/05/04 12:43:31 HSHIP EXP $

* @Author Howard Lewis SHIP

*

* /

Public Class PatienceRecordServlet Extends ApplicationServlet

{

}

You usually define a class extends org.apache.tapestry.applicationServlet.

Of course, if you have any special requirements, you can strengthen this class. Let me talk about this core class: This is actually a dispatcher, it accepts the outside world.

The HTTP Request request, and then send the request processing to the TapeStry Engine processing.

legend:

http://bbs1.nju.edu.cn/file/high-level-component-Request.p

Servlet-maping - this is the setting of the URL mapping, which is generally set to App

15

This is the configuration of the Timeout for HTTP Session, here is 15 minutes.

Let's take another PatientRecord.Application file, which is also an XML file:

DOCTYPE Application Publication PUBLIC

"- // Apache Software Foundation // TapeStry Specification 3.0 // en"

"

http://jakarta.apache.org/tapestry/dtd/tapestry_3_0.dtd>

->

We can see this document.

My Application Name = "Patient Record System" My Engine-Class is com.ht.web.patientRecopengine

My org.apache.tapestry.visit-class is com.ht.web.visitorState

The above two is the default implementation of Override Tapestry

This is my HOME-PAGE page pointing to my WEB Application Submodule Home HOME Page

->

These two items are me setting an error handling page

Where com.ht.Exception-Page is to handle ordinary org.apache.tapestry.ApplicationException,

It is now annotated, because the current development phase, I want to check the detailed error, don't have to be handled.

com.ht.security-exception-page is to process java.lang.securityException,

Because a website that needs to be logged in, a user who is logged in is not accessible to most resources.

If he visited the unacceptable resource, I will throw SECURITYEXCEPTION to the ENGINE process.

TapeStry Engine can define some service providing services, I define an Home service here is

I want to decide my home page, not the default configuration page of Tapestry.

These two items are explained that my Application contains two library, which is actually a Web Application Split.

The means of submodules, such as /com/ht/home/home.library"/ corresponds to my Home submodule,

/Com/ht/patientRecord/PatientRecord.library corresponds to my PatientRecord submodule

Let's talk about Tapestry Engine, for example my Engine implementation is as follows:

Package com.ht.Web;

Import java.io. *;

Import java.net.urlencoder;

Import javax.servlet.servletException;

Import javax.servlet.http.httpservletRequest; import javax.servlet.http.httpservletResponse;

Import javax.servlet.http.httpsession;

Import org.apache.tapestry. *;

Import org.apache.tapeengine;

Import org.apache.tapestry.Engine.iPropertySource;

Import org.apache.tapestry.Request.ResponseoutputStream;

Import org.apache.commons.lang.StringUtils;

/ **

* @Author Hery Tang

*

* Creation Date 2003-9-18

* Version 1.0

* /

Public class patientRecordengine Extends BaseEngine {

Public static final string home_page_name = "com.ht.home-page";

Public static final string exception_page_name = "com.ht.exception-page";

Public static final string security_exception_page_name = "com.ht.security-exception-page";

PRIVATE TRANSIENT BOOLEAN KILLSession;

Protected Void CleanuPafterRequest (Irequestcycle Cycle)

{

Super.cleanupafterRequest (cycle);

IF (KillSession)

{

Try

{

HttpSession session = cycle.getRequestContext (). Getsession ();

IF (session! = NULL)

{

session.INVALIDATE ();

}

}

Catch (IllegalStateException EX)

{

// ignore.

}

}

}

Public void logout ()

{

VisitorState Visit = (VisitorState) getvisit ();

IF (Visit! = NULL)

{

Visit.Setuser (NULL);

Visit = NULL;

}

Killsession = true;

}

/ **

* This methods read the home page name which defined by the user by using

* Propetry com.ht.home-page in Application Specification

*

* @Return the name of home page.

* /

Public string gethomepagename () {

Return getPropertySource (). getPropertyValue (Home_page_name);

}

/ **

* Return The security exception page name. If user does not defined this

* Page Name, The Normal Exception Page Will Be Return.

* @Return the name of the security exception page. * /

Public string getsecurityExceptionPageName () {

String result =

GetPropertySource (). getpropertyvalue

Security_exception_page_name);

IF (stringutils.isempty (result) {

Result = getExceptionPageName ();

}

Return Result;

}

/ **

* Return The Exception Page Name. If User Does Not Defeed This Page Name,

* The Default Exception page will be return.

* @Return the name of the normal exception page.

* /

Public String getExceptionPageName () {

String result =

GetPropertySource (). getPropertyValue (Exception_page_name);

IF (stringutils.isempty (result) {

Result = super.getExceptionPageName ();

}

Return Result;

}

/ **

* Overide The Method To Support Security Exception.

* /

Protected Void ActivateExceptionPage

Irequestcycle Cycle,

ResponseoutputStream Output,

Throwable cause

Throws servletexception {

// Print It to console first

PrintExceptions (Cause);

Try {

String ExceptionPageName;

Throwable throwable = cause;

Throwable securityException = null;

Boolean ISSecurityException = false;

IF (throwable

InstanceOf org.apache.tapestry.applicationRuntimeException) {

ApplicationRuntimeException Exception =

ApplicationRunTimeException) throwable;

IF (Exception.getrootcause () instanceof securityException) {

SecurityException = Exception.getcause ();

ISsecurityException = true;

}

}

IF (ISSecurityException) {

ExceptionPageName = GetSecurityExceptionPageName ();

} else {

ExceptionPageName = getExceptionPageName ();

}

IPage ExceptionPage = Cycle.getPage (ExceptionPageName);

IF (SecurityException == NULL) {

ExceptionPage.SetProperty ("Exception", Cause);

} else {

ExceptionPage.SetProperty ("Exception", SecurityException;

CYCLE.Activate (ExceptionPage);

RenderResponse (Cycle, Output);

} catch (throwable ex) {

// Worst Case Scenario. The Exception Page Itself Is Broken, Leaving

// US with no option but to write the cause to the output.

REPORTEXCEPTION

TapeStry.getMessage

"Abstractenne.unable-to-process-client-request"),

Cause);

// Also, Write the Exception thrown when redendering the Exception

// page, so that can get fixed as well.

REPORTEXCEPTION

TapeStry.getMessage

"Abstract-Exception-Page",

EX);

// and throw the Exception.

Throw new servletexception (ex.getMessage (), EX);

}

}

Private Void PrintExceptions (Throwable throwable) {

IF (throwable == null) {

Return;

}

Throwable.printStackTrace ();

PrintExceptions (throwable.getcause ());

}

}

Public void logout ()

A user logs out, empties a state in the VisitorState object representing the user state,

Killsession flag is TRUE

Protected Void CleanuPafterRequest (Irequestcycle Cycle)

Overrride Parent class BaseEngine implementation, first call the parent class implementation and then look at the KillSession flag, if true, INVALIDATE SESSION.

Public string gethomePageName ()

Public string getsecurityExceptionPageName ()

Public String getExceptionPageName ()

It is to take out I am in the configuration of PatientRecord.Application.

Protected Void ActivateExceptionPage

Irequestcycle Cycle,

ResponseoutputStream Output,

Throwable cause

Override AbstractEngine is an implementation, handling various Exception in my own way.

转载请注明原文地址:https://www.9cbs.com/read-28584.html

New Post(0)