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

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息