[置顶] 将项目从tomcat 迁移到JBoss

 

注:针对的是jboss5.0,其它版本没有测试过 ,主要参考了:http://www.diybl.com/course/3_program/java/javajs/20100719/460908.html,在些表示感谢


将项目从tomcat 迁移到JBoss,一般情况下会出现很多问题,下面是相关问题的记录及解决方法。
1. (1)首先要新增两个jar包:jboss-as-sprint-int-5.0.0.GA.jar 和 jboss-spring-int-vfs.jar
(2)然后在web.xml 文件中加上以下配置
  <context-param>
          <param-name>contextClass</param-name>
          <param-value>
              org.jboss.spring.vfs.context.VFSXmlWebApplicationContext
          </param-value>
   </context-param>  

2. 在web.xml 文件中,如果spring 相关文件是按以下方式配置的:
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext*.xml
  </param-value>
 </context-param>
 则要改为如下形式:
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   classpath:applicationContext.xml,
   classpath:applicationContext-compass.xml,
   classpath:applicationContext-security.xml
  </param-value>
 </context-param>
  
 

3. 如果在其它的spring 配置文件(如applicationContext.xml等文件)中引用了xml 文件,如下
 <import resource="quartz.xml"/> 
 则要将改为如下引入方式:
 
 <import resource="classpath:quartz.xml"/>

 

4.启动时如果报类似如下错误,则将jboss common/lib 中 hibernate-validator.jar 删掉
Caused by: org.hibernate.AnnotationException: java.lang.NoSuchMethodException: org.hibernate.validator.ClassValidator.<init>(java.lang.Class, java.util.ResourceBundle, org.hibernate.validator.MessageInterpolator, java.util.Map, org.hibernate.annotations.common.reflection.ReflectionManager)
 at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:353)
 at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1134)
 at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:673)
 at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
 ... 144 more
Caused by: java.lang.NoSuchMethodException: org.hibernate.validator.ClassValidator.<init>(java.lang.Class, java.util.ResourceBundle, org.hibernate.validator.MessageInterpolator, java.util.Map, org.hibernate.annotations.common.reflection.ReflectionManager)
 at java.lang.Class.getConstructor0(Class.java:2706)
 at java.lang.Class.getDeclaredConstructor(Class.java:1985)
 at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:343)
 ... 149 more
5.经过上4个步骤后,启动一般不会报错了,但在访问程序时,可能会报类似以下错误:
Stacktraces
java.lang.NoSuchMethodException: com.opensymphony.xwork2.ActionSupport.login()

    java.lang.Class.getMethod(Class.java:1605)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.getActionMethod(AnnotationValidationInterceptor.java:75)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:47)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    org.springframework.security.util.FilterChainProxy.doFilter(FilterChainProxy.java:169)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:236)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
    org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
    org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
    org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:601)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    java.lang.Thread.run(Thread.java:619)
    
 (注,根据你具体访问的url,报的错可能不是com.opensymphony.xwork2.ActionSupport.login() 方法以,而是其它方法)


这个错误解决方法如下 (这样解决只是权宜之计,如果哪位有更好的解决办法,请告之下):
在classpath 路径下增加一个struts-plugin.xml 文件,内容如下 :


<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
 
 <bean name="myActionConfigBuilder" type="org.apache.struts2.convention.ActionConfigBuilder"
  class="com.etshop.struts.convention.MyActionConfigBuilder"/>
  
 <constant name="struts.convention.actionConfigBuilder" value="myActionConfigBuilder"/>
</struts>  
  
  MActionConfigBuilder 源码如下 :
  

[java] view plaincopy
 
  1. <span style="font-size:18px;">package com.etshop.struts.convention;  
  2.   
  3. import java.net.MalformedURLException;  
  4. import java.net.URL;  
  5. import java.util.ArrayList;  
  6. import java.util.HashSet;  
  7. import java.util.List;  
  8. import java.util.Set;  
  9. import org.apache.commons.logging.Log;  
  10. import org.apache.commons.logging.LogFactory;  
  11. import org.apache.struts2.convention.PackageBasedActionConfigBuilder;  
  12. import com.opensymphony.xwork2.ObjectFactory;  
  13. import com.opensymphony.xwork2.config.Configuration;  
  14. import com.opensymphony.xwork2.inject.Container;  
  15. import com.opensymphony.xwork2.inject.Inject;  
  16. import com.opensymphony.xwork2.util.finder.ClassFinder;  
  17. import com.opensymphony.xwork2.util.finder.Test;  
  18.   
  19. /** 
  20.  *  
  21.  * 解决在JBoss 下不能找到 Action 问题 
  22.  *  
  23.  */  
  24.   
  25. public class MyActionConfigBuilder extends PackageBasedActionConfigBuilder {  
  26.   
  27.     private static Log log = LogFactory.getLog(MyActionConfigBuilder.class);  
  28.     private static final String BASE_FILE = "applicationContext.xml";  
  29.       
  30.     private String[] actionPackages;  
  31.     private String[] packageLocators;  
  32.   
  33.     /** 
  34.      *  
  35.      * @param configuration 
  36.      * @param container 
  37.      * @param objectFactory 
  38.      * @param redirectToSlash 
  39.      * @param defaultParentPackage 
  40.      */  
  41.   
  42.     @Inject  
  43.     public MyActionConfigBuilder(  
  44.         Configuration configuration,  
  45.         Container container,  
  46.         ObjectFactory objectFactory,  
  47.         @Inject("struts.convention.redirect.to.slash")  
  48.         String redirectToSlash,  
  49.         @Inject("struts.convention.default.parent.package")  
  50.         String defaultParentPackage) {  
  51.           
  52.         super( configuration, container, objectFactory, redirectToSlash, defaultParentPackage );  
  53.   
  54.     }  
  55.   
  56.     /** 
  57.      *  
  58.      * @param actionPackages 
  59.      *            (Optional) An optional list of action packages that this 
  60.      *            should create 
  61.      *  
  62.      * configuration for. 
  63.      *  
  64.      */  
  65.     @Inject(value = "struts.convention.action.packages", required = false)  
  66.     public void setActionPackages(String actionPackages) {  
  67.         super.setActionPackages(actionPackages);  
  68.           
  69.         if ( !"".equals( actionPackages.trim() ) ) {  
  70.             this.actionPackages = actionPackages.split( "\\s*[,]\\s*" );  
  71.         }  
  72.     }  
  73.   
  74.     /** 
  75.      *  
  76.      * @param packageLocators 
  77.      *            (Optional) A list of names used to find action packages. 
  78.      *  
  79.      */  
  80.   
  81.     @Inject(value = "struts.convention.package.locators", required = false)  
  82.     public void setPackageLocators(String packageLocators) {  
  83.         super.setPackageLocators(packageLocators);  
  84.         this.packageLocators = packageLocators.split("\\s*[,]\\s*");  
  85.     }  
  86.   
  87.     @Override  
  88.     @SuppressWarnings("unchecked")  
  89.     protected Set<Class> findActions() {  
  90.       
  91.         Set<Class> classes = new HashSet<Class>();  
  92.   
  93.         try {  
  94. //          ClassFinder finder = new ClassFinder(getClassLoaderForFinder(), buildUrls(), true);  
  95.             ClassFinder finder = new ClassFinder( getClassLoaderInterface(), buildUrls(), true);  
  96.   
  97.             if (actionPackages != null) {  
  98.   
  99.                 for (String packageName : actionPackages) {  
  100.                     Test<ClassFinder.ClassInfo> test = getPackageFinderTest(packageName);  
  101.                     classes.addAll(finder.findClasses(test));  
  102.                 }  
  103.             }  
  104.   
  105.             // package locators  
  106.             if (packageLocators != null) {  
  107.                   
  108.                 for (String packageLocator : packageLocators) {  
  109.                     Test<ClassFinder.ClassInfo> test = getPackageLocatorTest(packageLocator);  
  110.                     classes.addAll(finder.findClasses(test));  
  111.                 }  
  112.             }  
  113.   
  114.         } catch (Exception ex) {  
  115.             if (log.isErrorEnabled()) {  
  116.                 log.error("Unable to scan named packages", ex);  
  117.             }  
  118.         }  
  119.   
  120.         return classes;  
  121.     }  
  122.   
  123.     private List<URL> buildUrls() throws MalformedURLException {  
  124.   
  125.         List<URL> urls = new ArrayList<URL>();  
  126.         URL classesUrl = getClassLoader().getResource( BASE_FILE );  
  127.   
  128.         if (classesUrl == null) {  
  129.             throw new IllegalStateException(  
  130.                     "File " + BASE_FILE + " was not found. The folder WEB-INF/classes discovery base on file classes/" + BASE_FILE + ".");  
  131.         }  
  132.   
  133.         String baseFilePath = classesUrl.getFile();  
  134.         URL url = new URL( "file", "", baseFilePath.substring( 0, baseFilePath.indexOf( BASE_FILE ) ) );  
  135.   
  136.         if (log.isInfoEnabled()) {  
  137.             log.info("Struts2 ActionConfigBuilder, classes directory: " + url.getFile());  
  138.         }  
  139.   
  140.         urls.add(url);  
  141.   
  142.         return urls;  
  143.   
  144.     }  
  145.   
  146.     /*private ClassLoader getClassLoader() { 
  147.  
  148.         return Thread.currentThread().getContextClassLoader(); 
  149.  
  150.     }*/  
  151.   
  152. }  
  153. </span>  



  
  另注:以上过程都是通过在myEclipse中部署到jboss中的,接下来如果将程序导出为war包直接放到jboss下,启动时会报错,此时应该将war解压,将解压后的目录名后面加上.war,(如etshop.war包,解压后目录为etshop,将目录重命名为etshop.war)然后启动就不会报错了。

posted @ 2015-08-31 00:23  zfswff  阅读(558)  评论(0编辑  收藏  举报