嵌入式Jetty和Spring MVC没有映射index.jsp

时间:2014-09-02 18:28:42

标签: jsp spring-mvc embedded-jetty

我对Spring MVC比较陌生,并且熟悉Embedded Jetty。我们有一个现有的Spring MVC webapp,我可以编译成一个战争并放入Tomcat,它运行正常。但是我现在想在Embedded Jetty中运行它而不必将其放入Tomcat。

我已经按照本教程进行了操作,我发现这些教程很有用(只做了一些小改动):

http://kielczewski.eu/2013/11/using-embedded-jetty-spring-mvc/

一切似乎都“差不多”,除了我的jsps的映射似乎不起作用。

这是我的主要课程:

public class EmbeddedJetty {

    private static final int DEFAULT_PORT = 8080;
    private static final String CONTEXT_PATH = "/";
    private static final String CONFIG_LOCATION = "/WEB-INF/spring-servlet.xml";
    private static final String MAPPING_URL = "/*";

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

    private static int getPortFromArgs(String[] args) {
        if (args.length > 0) {
            try {
                return Integer.valueOf(args[0]);
            } catch (NumberFormatException ignore) {
            }
        }
        return DEFAULT_PORT;
    }

    private void startJetty(int port) throws Exception {
        Server server = new Server(port);
        WebApplicationContext context = getContext();
        ServletContextHandler servletContextHandler = getServletContextHandler(context);
        server.setHandler(servletContextHandler);

        server.start();
        server.join();

        ResourceHandlerRegistry rhr = new ResourceHandlerRegistry(context, servletContextHandler.getServletContext());
        rhr.addResourceHandler("images/**").addResourceLocations("images/");
        rhr.addResourceHandler("jsp/**").addResourceLocations("jsp/");
        rhr.addResourceHandler("css/**").addResourceLocations("css/");
    }

    private static ServletContextHandler getServletContextHandler(WebApplicationContext context) throws IOException {
        ServletContextHandler contextHandler = new ServletContextHandler();
        contextHandler.setErrorHandler(null);
        contextHandler.setContextPath(CONTEXT_PATH);
        DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
        ServletHolder servletHolder = new ServletHolder(dispatcherServlet);
        contextHandler.addServlet(servletHolder, MAPPING_URL);
        contextHandler.addEventListener(new ContextLoaderListener(context));
        contextHandler.setResourceBase(new ClassPathResource("webapp").getURI().toString());
        return contextHandler;
    }

    private static WebApplicationContext getContext() {
        XmlWebApplicationContext context = new XmlWebApplicationContext();
        context.setConfigLocation(CONFIG_LOCATION);
        return context;
    }
}

这是我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <!-- Welcome file -->
    <display-name>USER Administration</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <!-- This is the servlet that will route to the correct Controller based on annotations -->
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!--Map non-resource URLs to the DispatcherServlet -->
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- Allow access to the resources -->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.png</url-pattern>
        <url-pattern>*.js</url-pattern>
        <url-pattern>*.css</url-pattern>
    </servlet-mapping>

</web-app>

这是我的spring-servlet.xml(相关部分 - 我认为):

    <!-- This is the base package(s) for where spring should scan for @Component, @Service, @Controller, etc -->
    <context:component-scan base-package="com.my.user.admin" />

     <!-- A resolver for the returned ModelAndView objects and other strings, this tells it to append .jsp and look in the /WEB-INF/jsp/ for it -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- one of the properties available; the maximum file size in bytes -->
        <property name="maxUploadSize" value="100000"/>
    </bean>

    <!-- loads in property files for use in this config -->
   <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:Frameworks/hibernate.properties</value>
                <value>classpath:Frameworks/activemq.properties</value>
            </list>
        </property>
    </bean>

    <!-- Hibernate 3 session factory used for session management -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:${hibernate.config.location}" />
    </bean>

然后我使用maven shade插件构建它,并使用以下命令运行它:

java -classpath c:\dev\customclasspath;target\user-admin.jar com.admin.main.EmbeddedJetty

它启动了,并且在日志中看起来都很好,我看到很多行将Java类加载到URL上:

Mapped URL path [/modifyUser] onto handler 'menuController
Mapped URL path [/modifyUser.*] onto handler 'menuControll
Mapped URL path [/modifyUser/] onto handler 'menuControlle
Mapped URL path [/addUser] onto handler 'menuController'
Mapped URL path [/addUser.*] onto handler 'menuController'
Mapped URL path [/addUser/] onto handler 'menuController'

但是当我尝试在“localhost:8080”加载我的应用程序时,我得到一个“Not Found - Powered by Jetty”页面,在日志中我看到以下消息:

No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'org.springframework.web.servlet.DispatcherServlet-6910fe28'

我做错了什么?在我的jar文件中,我可以看到文件/webapp/index.jsp,所以我认为可能是这样,但如果我做“localhost:8080 / webapp /”也不起作用。我不知道现在要改变什么,任何帮助都将不胜感激!

EDIT1

我认为这个问题是因为它实际上并没有加载我的web.xml文件(我定义了servlet-mappings),因为我创建了Jetty Context作为spring上下文,而不是'WebAppContext'。但是,我需要spring-servlet.xml文件,因为它包含我的所有控制器。如何加载它们?

1 个答案:

答案 0 :(得分:0)

只有在没有URI的servlet映射时才使用欢迎文件。在你的情况下有一个:

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

除了.png / .js / .css外,它处理任何