NoClass:org/apache/juli/logging/LogFactory

SpringMvc+Spring+Mybatis,内嵌Jetty服务器,jdk1.8java

main方法以下:web

package com.config;

import java.io.IOException;

import org.apache.jasper.servlet.JspServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class ComMain {

	private static final Logger LOGGER = LoggerFactory.getLogger(ComMain.class);

	private static final int PORT = 8888;

	private static final String CONTEXT_PATH = "/";
	private static final String CONFIG_LOCATION_PACKAGE = "com.config";
	//	private static final String MAPPING_URL = "/rest/*";
	private static final String MAPPING_URL = "/";
	/**
	 * 若是是/,则全部路径都会被mvc-dispatcher拦截,默认静态页面是不能访问的,
	 * 这时要在WebMvcConfig里将这些静态页面设为不被spring拦截器处理
	 */
	private static final String WEBAPP_DIRECTORY = "src/main/webapp";

	public static void main(String[] args) throws Exception {
		new ComMain().startJetty(PORT);
	}

	private void startJetty(int port) throws Exception {
		LOGGER.debug("Starting server at port {}", port);
		Server server = new Server(port);

		//		server.setHandler(getWebAppContext());
		server.setHandler(getServletContextHandler());
		addRuntimeShutdownHook(server);
		server.start();
		LOGGER.info("Server started at port {}", port);
		server.join();
	}

	private static ServletContextHandler getServletContextHandler() throws IOException {
		ServletContextHandler contextHandler = new ServletContextHandler(
				ServletContextHandler.SESSIONS); // SESSIONS requerido para JSP 
		contextHandler.setErrorHandler(null);

		contextHandler.setResourceBase(WEBAPP_DIRECTORY);
		contextHandler.setContextPath(CONTEXT_PATH);

		// JSP
		contextHandler.setClassLoader(Thread.currentThread().getContextClassLoader()); // Necesario para cargar JspServlet
		contextHandler.addServlet(JspServlet.class, "*.jsp");
		//不加jspServlet,.jsp文件访问时会报404

		// Spring
		WebApplicationContext webAppContext = getWebApplicationContext();
		DispatcherServlet dispatcherServlet = new DispatcherServlet(webAppContext);
		ServletHolder springServletHolder = new ServletHolder("mvc-dispatcher",
				dispatcherServlet);
		contextHandler.addServlet(springServletHolder, MAPPING_URL);
		contextHandler.addEventListener(new ContextLoaderListener(webAppContext));

		return contextHandler;
	}

	private static WebApplicationContext getWebApplicationContext() {
		AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
		context.setConfigLocation(CONFIG_LOCATION_PACKAGE);
		return context;
	}

	private static void addRuntimeShutdownHook(final Server server) {
		Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
			@Override
			public void run() {
				if (server.isStarted()) {
					server.setStopAtShutdown(true);
					try {
						server.stop();
					} catch (Exception e) {
						System.out.println(
								"Error while stopping jetty server: " + e.getMessage());
						LOGGER.error(
								"Error while stopping jetty server: " + e.getMessage(),
								e);
					}
				}
			}
		}));
	}

}

运行后报错:spring

java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactorapache

按提示加上相应jar包:tomcat

<dependency>
            <groupId >org.apache.tomcat </groupId >
            <artifactId >juli </artifactId >
            <version >6.0.45 </version >
 </dependency >

再次运行又报错以下:服务器

org.apache.jasper.JasperException: java.lang.IllegalStateException: No org.apache.tomcat.InstanceManager set in ServletContextmvc

这时我就以为奇怪了,我明明是用的Jetty服务器,怎么连续两个错都是关于Tomcat的,后来在网上相关查询,折腾了一下午,原来问题出在该项目的运行环境选择了Tomcat,解决方法是:app

右键项目-->Properties-->Project facet -->Runtimes,将选中的Apache Tomcat 7.0给去掉。eclipse

相关文章
相关标签/搜索
本站公众号
   欢迎关注本站公众号,获取更多信息