Extension of Struts under Tomcat. UserTransaction

zhaozj2021-02-12  196

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. Configuration for Server.xml under Tomcat

< ResourceParams name = "jdbc / mydb"> factory org.objectWeb.jndi.DataSourceFactory driverclassname < Value> Oracle.jdbc.driver.racledriver URL jdbc: Oracle: Thin: @ 1192.168.0.1: 1521: MyDB < / parameter> username mystruts password 88888 < Parameter> maxactive 20 maxidle 20 maxwait -1 factory org.objectweb.jotm.UserTransactionFactory

jotm.timeout 60 2. Manage configuration within Web.xml

DataSource Connecton jdbc / mydb javax.sql.datasource 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. Configuration in Web.xml

action org.apache.struts.Action.ActionServlet config /Web-inf/struts-config.xml mapping com.mystruts.smapactionmapping 1

4. 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. 1. In this class, you can use the ProcessPreProcess method. 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;}

}

}2. Configuration in Struts-Config.xml

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

New Post(0)