注:针对的是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 源码如下 :
package com.etshop.struts.convention;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.PackageBasedActionConfigBuilder;
import com.opensymphony.xwork2.ObjectFactory;
import com.opensymphony.xwork2.config.Configuration;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.finder.ClassFinder;
import com.opensymphony.xwork2.util.finder.Test;
/**
*
* 解决在JBoss 下不能找到 Action 问题
*
*/
public class MyActionConfigBuilder extends PackageBasedActionConfigBuilder {
private static Log log = LogFactory.getLog(MyActionConfigBuilder.class);
private static final String BASE_FILE = "applicationContext.xml";
private String[] actionPackages;
private String[] packageLocators;
/**
*
* @param configuration
* @param container
* @param objectFactory
* @param redirectToSlash
* @param defaultParentPackage
*/
@Inject
public MyActionConfigBuilder(
Configuration configuration,
Container container,
ObjectFactory objectFactory,
@Inject("struts.convention.redirect.to.slash")
String redirectToSlash,
@Inject("struts.convention.default.parent.package")
String defaultParentPackage) {
super( configuration, container, objectFactory, redirectToSlash, defaultParentPackage );
}
/**
*
* @param actionPackages
* (Optional) An optional list of action packages that this
* should create
*
* configuration for.
*
*/
@Inject(value = "struts.convention.action.packages", required = false)
public void setActionPackages(String actionPackages) {
super.setActionPackages(actionPackages);
if ( !"".equals( actionPackages.trim() ) ) {
this.actionPackages = actionPackages.split( "\\s*[,]\\s*" );
}
}
/**
*
* @param packageLocators
* (Optional) A list of names used to find action packages.
*
*/
@Inject(value = "struts.convention.package.locators", required = false)
public void setPackageLocators(String packageLocators) {
super.setPackageLocators(packageLocators);
this.packageLocators = packageLocators.split("\\s*[,]\\s*");
}
@Override
@SuppressWarnings("unchecked")
protected Set<Class> findActions() {
Set<Class> classes = new HashSet<Class>();
try {
// ClassFinder finder = new ClassFinder(getClassLoaderForFinder(), buildUrls(), true);
ClassFinder finder = new ClassFinder( getClassLoaderInterface(), buildUrls(), true);
if (actionPackages != null) {
for (String packageName : actionPackages) {
Test<ClassFinder.ClassInfo> test = getPackageFinderTest(packageName);
classes.addAll(finder.findClasses(test));
}
}
// package locators
if (packageLocators != null) {
for (String packageLocator : packageLocators) {
Test<ClassFinder.ClassInfo> test = getPackageLocatorTest(packageLocator);
classes.addAll(finder.findClasses(test));
}
}
} catch (Exception ex) {
if (log.isErrorEnabled()) {
log.error("Unable to scan named packages", ex);
}
}
return classes;
}
private List<URL> buildUrls() throws MalformedURLException {
List<URL> urls = new ArrayList<URL>();
URL classesUrl = getClassLoader().getResource( BASE_FILE );
if (classesUrl == null) {
throw new IllegalStateException(
"File " + BASE_FILE + " was not found. The folder WEB-INF/classes discovery base on file classes/" + BASE_FILE + ".");
}
String baseFilePath = classesUrl.getFile();
URL url = new URL( "file", "", baseFilePath.substring( 0, baseFilePath.indexOf( BASE_FILE ) ) );
if (log.isInfoEnabled()) {
log.info("Struts2 ActionConfigBuilder, classes directory: " + url.getFile());
}
urls.add(url);
return urls;
}
/*private ClassLoader getClassLoader() {
return Thread.currentThread().getContextClassLoader();
}*/
}
另注:以上过程都是通过在myEclipse中部署到jboss中的,接下来如果将程序导出为war包直接放到jboss下,启动时会报错,此时应该将war解压,将解压后的目录名后面加上.war,(如etshop.war包,解压后目录为etshop,将目录重命名为etshop.war)然后启动就不会报错了。