Extensions for Struts under Tomcat, and use the useertransaction for transaction operations

zhaozj2021-02-16  54

Struts under Tomcat, and uses UsertransAction for transaction operations. I use Struts1.1. 1: Use the useertransaction to use the JOTM package 1 in jotm-1.4.3.tar when using UserTransaction. About Tomcat Server.xml

TYPE = "javax.sql.datasource" /> factory org.objectWeb.jndi.DataSourceFactory DRIVERCLASSNAME Oracle.jdbc.driver.Orcledriver URL jdbc: Oracle: Thin: @ 1192.168.0.1:1521:Mydb username mystruts password < Value> 888888 maxactive 20 maxidle 20 < / value> maxwait -1

type = "javax.transaction.UserTransaction" /> factory org.objectweb.jotm.UserTransactionFactory jotm.timeout 60 2. Manage Web.xml DataSource Connecton jdbc / mydb javax.sql.datasource < / Res-type> container 3. Extend the Action.java class, and then inherit the MyAction class directly when developing Struts. import org.apache.struts.action.Action; import javax.naming *; import javax.transaction.UserTransaction; public class MyAction extends Action {protected static UserTransaction userTran = null; static String utName; static {utName = "java:. comp / UserTransaction ";} public SmapAction () {try {InitialContext initCtx = new InitialContext (); userTran = (UserTransaction) initCtx.lookup (utName);} catch (Exception e) {System.out.println (" userTran error = " E.getMessage ());

}} 2: Extending an actionMApping.java class, you can set whether log information is logged when developed. 1. Extended ActionMapping.java class import org.apache.struts.Action.ActionMapping;

Public class myActionmapping extends actionmapping {// addressmapping {// addressMAPPING = false;

// Call the an actionMapping's default constructor public saapactionmapping () {super ();}

Public Boolean islogresults () {return logresults;} public void setLogResults (Boolean LogResults) {this.logResults = LogResults;}} 2. Configure struts-config.xml, where This sentence is setting whether or not log record. 3. Specific operation class. When setting-proty's value of STRUTS-Config.xml is false, it is not printed, and the print operates for True PUBLIC CLASS TESTBEAN EXTENDS SMAPACTION {

Public ActionForward Execute (ActionMapping Mapping, Actionform Form, HTTPSERVLETREQUEST

Request, httpservletresponse response) throws exception {myActionMapping mymapping = (myActionmapping) mapping;

IF (mymapping.islogresults ()) {system.out.println ("islogResults =====);} // Transaction operation try {useertran.begin ();

System.out.println ("00000000"); string SQL1 = "INSERT INTO TEST VALUES (21, '21 ', sysdate"; jdbc.executeUpdate (SQL1); System.out.Println ("11111111"); string SQL2 = "INSERT INTO TEST VALUES (33, '33', sysdate"; jdbc.executeUpdate (SQL2); System.out.Println ("2222222"); string sql3 = "Insert Into Test Values ​​(51, '51 ', sysdate) "; jdbc.executeUpdate (SQL3); System.Out.println (" 33333333 "); userTRAN.Commit ();} catch (exception e) {usertran.rollback (); system.out.println (" 44444444 " } Return mapping.findforward ("success");}} 3: Extension to the Struts platform, you can perform permission control on the platform, extend the requestProcessor.java class. In this class, you can use as long as the ProcessPreProcess method is expanded. public class MyRequestProcessor extends RequestProcessor {/ ** *

Process an HttpServletRequest and create the * corresponding HttpServletResponse . * * @param request The servlet request we are processing * @param response The servlet response we are creating * * @exception IOException if an input / output error occurs * @exception ServletException if a processing exception occurs * / public void process (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

// Wrap Multipart Requests with a Special Wrapper Request = ProcessMultipart (Request);

// Identify The Path Component We Will Use to SELECT A MAPPING

String path = processPath (request, response); System.out.println ( "33333333333333333333333 =" path); if (path == null) {return;} if (log.isDebugEnabled ()) {log.debug ( "Processing A '" Request.getMethod () "' for path '" PATH " ");} // select a local for the current user if Requested ProcessLocale (Request, Response);

// set the content type and no-caching headers if Requested processcontent (request, response); processnocache (request, response);

// General Purpose PreProcessing Hook if (! Process, response)) {path = "/ fail"; system.out.println ("1111111111 =" path); // Return;}

// identify the mapping for this request actionMApping mapping = processmapping (required); if (mapping == null) {return;}

// Check for any role required to perform this action if (! Process, response, mapping) {return;}

// Process any ActionForm bean related to this request ActionForm form = processActionForm (request, response, mapping); processPopulate (request, response, form, mapping); (! ProcessValidate (request, response, form, mapping)) if {return; }

// process a forward or include specified by this mappingiff (! Process,}) {return;}}}}}} {Return;}

// Create or acquire the action instance to process this request action action = processActioncreate (request, response, mapping); if (action == null) {return;}

// Call the Action instance itself ActionForward forward = processActionPerform (request, response, action, form, mapping); // Process the returned ActionForward instance processForwardConfig (request, response, forward);

}

/ ** * General-purpose preprocessing hook that can be overridden as required * by subclasses. Return true if you want standard processing * to continue, or false if the response has Already Been * Completed. The default usmentation does Nothing. * @Param Request the servlet request we are processing * @Param response the servlet response we are creating * /

Protected Boolean ProcessPreprocess (httpservletRequest Request,

HttpservletResponse response) {httpsession session = request.getSession (); // string ss = (string) session.getattribute ("ss"); string ss = "sdfds"; system.out.println ("------ --------------- processpreprocess = " ss); if (ss! = null) {Return (TRUE);} else {returnaf false;}

}

}

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

New Post(0)