spring + Embedded jetty + <web-app>如何创建上下文

时间:2015-06-15 06:59:01

标签: spring jetty

有一个maven项目,使用/WEB-INF/web.xml:

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

<servlet>
<servlet-name>baseWebApp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>baseWebApp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

并使用applicationContext.xml:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"           xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

       <import resource="classpath:dataSource.xml"/>

       <mvc:annotation-driven/>
       <context:component-scan base-package="com.blogspot.permgen.webapp"/>

       <mvc:resources mapping="/resources/**" location="/resources/"/>

       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
             p:prefix="/WEB-INF/pages/"
         p:suffix=".jsp"/>
</beans>

控制器基于注释。

mvn -Djetty.port = 9999 jetty:run 启动时,应用程序启动良好。我决定在代码中初始化jetty,但未能为它创建上下文。这是主要课程:

public class AppMain {
public static void main(String[] args) throws Exception {

    ServletContextHandler sch = new ServletContextHandler();

/*here must be context initialization ... */

    Server jetty = new Server(9999);
    jetty.setHandler(sch);
    jetty.start();
}
}

所以问题是:如何从web.xml初始化jetty的上下文?
是否有可能,或者我必须使用弹簧以某种方式解析它?

1 个答案:

答案 0 :(得分:0)

如果要使用web.xml配置上下文,则不要使用ServletContextHandler,而是使用从ServletContextHander派生的WebAppContext,并添加使用web.xml配置自身的功能

以下是一个示例:this