我对Spring MVC比较陌生,并且具有Embedded Jetty的工作知识。我们有一个现有的Spring MVC Web应用程序,我可以编译成一个战争并投入到Tomcat中,并且它工作正常。不过,我现在想要在Embedded Jetty中运行它,而不必将其放到Tomcat中。嵌入式Jetty和Spring MVC未映射index.jsp
我按照这个教程中,我发现有帮助的(有一些小的改动):
http://kielczewski.eu/2013/11/using-embedded-jetty-spring-mvc/
,一切似乎是“几乎” OK,但似乎对我的JSP的映射不上班。
这里是我的主类:
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遮罩插件构建它,并使用以下命令运行它:
java -classpath c:\dev\customclasspath;target\user-admin.jar com.admin.main.EmbeddedJetty
它启动时,所有在日志中看起来不错,我看到很多线在那里装载我的Java类到网址:
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'
但是当我尝试加载了我的应用程序在“本地主机:8080”,我得到一个“未找到 - 技术码头”页面,在我看到以下消息日志:
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的映射),因为我创造了码头上下文作为Spring上下文,而不是'WebAppContext'。不过,我需要spring-servlet.xml文件,因为它包含我所有的控制器。我如何加载?
这是否意味着我的webapp的主页面不能再是index.jsp了,并且必须是弹簧控制器?当作为war文件部署到现有的Tomcat实例中时,index.jsp方法可以正常工作 – Matt 2014-09-02 22:41:15
@Matt您还可以将其添加到您的'spring-servlet.xml':' '。那么jsp必须位于'jsp'文件夹中。 –
zeroflagL
2014-09-03 07:18:21
谢谢,我尝试了建议的更改,但仍然当我在日志中访问“localhost:8080 /”时,我看到“没有找到具有URI [/ jsp/index的HTTP请求的映射。jsp] in DispatcherServlet“:( – Matt 2014-09-03 09:26:42