Using Struts with Java Data Objects

zhaozj2021-02-12  223

Using Struts with JavaTM Data Objects

Persistence / Web Framework

"Learn how to use Struts and JavaTM Data Objects (JDO) to build upgraded, modular web applications."

This article explains one by one from the following aspects:

1. Struts Key Concepts

2. JDO Key Concepts

3. Initialization servlet

4. Ask for persistence layer data in Actions

5. Defects and misunderstandings

6. Summary

7. Original author

The following analysis:

1. Struts Key Concepts

1) Struts Controller (ActionServlet)

l Model-View-Controller mode

l Model = (Persistent Data) can persistent data;

View = Display layer (JSPTM Page)

Controller = Control Layer {Servlet (Struts ActionServlet)}

-Controller accepts HTTP requests

-Controller uses Action Form to encapsulate data from the Client side, and call the validate () method in the FormBean to check the packaged data, if the error is found in the verification, you can write back.

-Controller scheduling action deduces the Form Bean that is verified.

2) Struts Key Concepts

3) Struts Actions Classes

Developers can develop their own ApplicationAction, as long as they inherit struts action clas

-Action processing logical transaction or hand over to a class module that deals with business logic.

-Action returns a business object Continue to process other transactions or directly return to the Client side is typically JSPTM Page.

-

4) SREVET FILTER

- Each request must pass through Filter

- You can customize FILTER to access specific resources

- call other Filter or Struts ActionServlet

- Pay attention to capture exceptions

2. JDO Key Concepts

2.1 JavaTM Data Objects Key Concepts

1) PersistenceManagerFactory

- Factory method to create a PersistenceManager

2) PersistenceManager

-create / delete persistent object

-Query Factory

3) Query

- Get a long-lasting object collection according to different query conditions

4) Transaction

-Begin, Commit, Rollback

5) Query returns a collection of persistent objects

6) Persistent object is the Plain Old JavaTM Objects in your app, such as: Customer, Order, ORDERLINE, PART, etc.

7) Use Methods On Instances WITHOUT Regard for struts, Web Container, HTTP

8) Update of persistent data has JDO product hidden processing

2.2 JDOFilter

Public Class Jdofilter Implements Javax.Servlet.Filter {

Private persistenceManagerFactory PMF;

Public void init (filterconfig fc) {

InitializePMF (FC);

}

Public void Dofilter (...) throws oException, servletexception {PersistenceManager PM = NULL;

Try {

PM = InitializationPM ();

CHAIN.DOFILTER (...);

Finalizepm (PM);

} finally {

AbnormalPM (PM);

}

}

... ..

}

3. Initialize servlet

3.1

l Container call filter.init () to initialize the PersistenceManagerFactory

- Find / create PersistenceManagerFactory

- Through JNDINAME (if the container supports JNDI)

-J2EETM Application Server Environment

- Through ResourceName (if not)

-ResourceName indicates a specific Properties file with configuration information

l Storage PersistenceManagerFactory to the Filter instance

3.2 FILTER configuration information

[Add to Web.xml]

Mediamaniapmf

com.mediamain.appservre.jdofilter

Path

/Web-inf/jdomovies.properties

3.3 jdomovies.properties

[Put it in application package Web-inf]

The following content is different from the original text, here I use another JDO product: jdogenie (http://www.hemtech ...co.za)

The content of jdomovies.properties is as follows:

# Server Name (Must Be Unique ON A Machine)

Server = jdogenie1

REMOTE.ACCESS = FALSE

Remote.pm = false

HyperDrive = true

# JDO Genie Version

Version = 1.4.10 (04 JUL 2003)

# Data Stores

StoreCount = 1

Store0.name = main

Store0.Type = JDBC

Store0.driver = com.microsoft.jdbc.sqlserver.sqlserdriver

Store0.url = JDBC /: Microsoft /: SQLServer /: // localhost

Store0.db = MSSQL

Store0.User = Song

Store0.password = Song

Store0.Properties = DatabaseName / = jdotest; selectMethod / = Cursor

Store0.maxactive = 5

Store0.maxidle = 2

Store0.init.sql =

Store0.Wait.For.con.on.Startup = false

Store0.Validate.sql =

Store0.test.on.alloc = false

Store0.test.on.Release = false

Store0.Validate.mapping.on.startup = falsestore0.test.on.exception = true

Store0.Retry.Interval.ms = 1000

Store0.Retry.count = 10

Store0.pscache.max =

Store0.ext.jdbc-key-generator = -

Store0.jdbc.namegen = -

# .jdo resources

JDOFILECOUNT = 1

JDO0 = system.jdo

# Properties for jdohelper.getPersistenceManagerFactory (...)

Javax.jdo.PersistenceManagerFactoryClass = za.co.hemtech.jdo.client.bootstrappmf

Javax.jdo.option.optimistic = true

Javax.jdo.Option.retainvalues ​​= false

Javax.jdo.opption.restorevalues ​​= false

Javax.jdo.Option.Ignorecache = false

Javax.jdo.Option.nontransactionalRead = false

Javax.jdo.option.nontransactionalwrite = false

Javax.jdo.Option.multithreaded = false

# EVENT logging

Event.logging = -

# Cache settings

Cache.enabled = true

Cache.maxObjects = 10000

Cache.listener = -

Query.cache.enabled = true

Query.cache.max.queries = 10000

# Data store 0 mappings

Store0.jdbc.Type.count = 0

Store0.jdbc.javatype.count = 0

# JDO GENIE META DATA Editor Properties (Not Used At Runtime)

mdedit.classpathcount = 4

Mdedit.cp0 = E /: / RMS15 / LIB / MSSQLSERVER.JAR

mdedit.cp1 = classes

Mdedit.cp2 = E /: / RMS15 / LIB / MSBASE.JAR

Mdedit.cp3 = E /: / RMS15 / LIB / MSUTIL.JAR

# JDO Genie Class Diagrams (Not Used At Runtime)

DIAGRAM.COUNT = 1

Diagram0.name = diagram 1

Diagram0.legend = 600, 460

Diagram0.General = 13, y, y

Diagram0.class = Y, N, Y, N, N, Y, N, Y, N, N, N, N, N, N, N, N, N

Diagram0.field = Y, Y, N, N, N, N

Diagram0.class.count = 0

{Don't be afraid of his long, JDogenie comes with visualization tools can help you complete}

3.4 PersistenceManagerFactory Initialization

...... Connect to the JDOFILTER code ...

Protected Void InitializationPMF (FilterConfig FC) {

String propsname = (string) fc.getinitParameter ("path");

Properties PROPS = loadingProperties; PMF = JDOHELPER.GETPERSISTENCEMANAGERFAACTORY (PROPS);

}

Protected Properties LoadProperties (String Pathname) {

.......

}

3.5 PersistenceManager Initialization

...... Connect to the JDOFILTER code ...

Protected persistencemanager initializationpm () {

PersistenceManager PM = PMF.GetPersistenceManager ();

Request.setttribute ("JDO.PM", PM);

Return PM;

}

Protected Void FinalizePM (PersistenceManager PM) {

PM.Close ();

Request.Removettribute ("jdo.pm");

}

Protected void abnormalpm (PersistenceManager PM) {

IF (pm.currenttransaction (). isactive ()) {

Pm.currentTransaction.rollback ();

}

PM.Close ();

Request.Removettribute ("jdo.pm");

}

4 .. Ask for persistence layer data in Actions

l Action or JSPTM Page Access Persistence Data

l Get PersistenceManager from Request

l Use the PersistenceManager. Like two layers: Action integrate Query, then authorize the corresponding business component to process the application's business logic.

l P persresser data passed through request

l "A PersistenceManager" mode

4.1 Action processing instance

Class lookupmovieaction extends action {

Protected actionforward execute (....) {

PersistenceManager PM = (PersistenceManager) Request.getaTribute ("jdo.pm");

Query Q = PM.NewQuery (Movie.class, "Genre == Param1");

Q.DeclareParameters ("String Param1");

String moviename = (string) Request.getParameter ("Moviegenre");

Object movies = q.execute (moviename);

Request.setttribute ("Movies", Movies;

Return (Mapping, FindFoward ("DisplayMovies");

}

}

4.2 transaction

l Most Actions do not need doping.

-Insert, delete, Update needs transaction processing

- Others use Nontransaction to handle

l JSPTM Pages usually does not require transaction processing

l If transaction is required, begin () and commit () will need to call in action execute ()

l If you want to use multiple persistenceManagers, then transactions will not be adjusted

- If you must control your transaction, consider using the EJB architecture.

5. Defects and misunderstandings

l Do not turn off the PersistenceManager - Persistent objects in Action / JSPTM Page is "life", close the PersistenceManager equivalent to "killing" them.

l If you use a transaction, you must use TRY / CATCH in your business method and call Rollback ().

l Don't turn on a transaction in an action and close it in another action.

l If you want to use multiple PersistenceManagers, you must define multiple JDOFILtes

6.

7. original author:

Craig Russell: The Specification Lead for JavaTM Data Objects (JSR 12) AT Sun Microsystems, one of the latest version of "Java Data Objects"

Craig McClanahan: The Author and Chief Evangelist for the struts framework (pick us the most familiar say, and and)

Amy Roh Is A Developer on The JSPTM / Servlet Team Working on Tomcat

Original connection:

Http://servlet.java.sun.com/javaone/Resources/content/sf2003/conf/SESSIONS/PDFS/3368.pdf

You can't be translated. If you can read it, please read the original text.

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

New Post(0)