在tomcat下对struts进行扩展,同时用到了usertransaction进行事务操作。我用的是struts1.1。
一:使用usertransaction时要用到jotm-1.4.3.tar里的jotm包
1。关于tomcat下server.xml的配置
<Context path="/mystruts" docBase="smap" debug="0" reloadable="true">
<Resource name="jdbc/myDB" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/myDB">
<parameter>
<name>factory</name>
<value>org.objectweb.jndi.DataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:oracle:thin:@1192.168.0.1:1521:myDB</value>
</parameter>
<parameter>
<name>username</name>
<value>mystruts</value>
</parameter>
<parameter>
<name>password</name>
<value>888888</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>20</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>-1</value>
</parameter>
</ResourceParams>
<Resource name="UserTransaction" auth="Container" type="javax.transaction.UserTransaction"/>
<ResourceParams name="UserTransaction">
<parameter>
<name>factory</name>
<value>org.objectweb.jotm.UserTransactionFactory</value>
</parameter>
<parameter>
<name>jotm.timeout</name>
<value>60</value>
</parameter>
</ResourceParams>
</Context>
2。管理web.xml内的配置
<resource-ref>
<description>Datasource Connecton</description>
<res-ref-name>jdbc/myDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3。扩展Action.java类,后边开发struts时,就直接继承MyAction类。
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());
}
}
}
二:扩展ActionMapping.java类,在开发时就可以设置是否记录日志信息。
1。扩展ActionMapping.java类
import org.apache.struts.action.ActionMapping;
public class MyActionMapping extends ActionMapping{
// add the new properties
protected boolean logResults = false;
//call the ActionMapping's default constructor
public SmapActionMapping() {
super();
}
public boolean isLogResults() {
return logResults;
}
public void setLogResults(boolean logResults) {
this.logResults = logResults;
}
}
2。配置struts-config.xml,
其中<set-property property="logResults" value="false"/>这句话是设置是否进行日志记录的。
<action path="/logon"
type="com.mystruts.LogonAction"
name="userForm"
scope="request"
validate="false"
input="/index.jsp"
>
<set-property property="logResults" value="false"/>
<forward name="success" path="/main.jsp"/>
</action>
3。在web.xml中的配置
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>mapping</param-name>
<param-value>com.mystruts.SmapActionMapping</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
4。具体的操作类。
当struts-config.xml中的set-property 的value为false时,不打印,为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====");
}
//进行事务操作
try{
userTran.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");
}
}
三:对struts平台的扩展,可以在平台上进行权限控制等操作,扩展RequestProcessor.java类。
1.在这个类中,只要对processPreprocess方法进行相对应的扩充,就可以了。
public class MyRequestProcessor extends RequestProcessor{
/**
* <p>Process an <code>HttpServletRequest</code> and create the
* corresponding <code>HttpServletResponse</code>.</p>
*
* @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 Locale 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 (!processPreprocess(request, response)) {
path="/fail";
System.out.println("11111111111="+path);
//return;
}
// Identify the mapping for this request
ActionMapping mapping = processMapping(request, response, path);
if (mapping == null) {
return;
}
// Check for any role required to perform this action
if (!processRoles(request, response, mapping)) {
return;
}
// Process any ActionForm bean related to this request
ActionForm form = processActionForm(request, response, mapping);
processPopulate(request, response, form, mapping);
if (!processValidate(request, response, form, mapping)) {
return;
}
// Process a forward or include specified by this mapping
if (!processForward(request, response, mapping)) {
return;
}
if (!processInclude(request, response, mapping)) {
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 <code>true</code> if you want standard processing
* to continue, or <code>false</code> if the response has already been
* completed. The default implementation 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 {
return false;
}
}
}
2。在struts-config.xml中的配置
<controller processorClass="com.mystruts.MyRequestProcessor" />