在然而之前已经提出了这样的问题,或者给出的答案对我没有用,或者答案完全回避了问题而支持不同的基本示例。
我试图使用嵌入在Jetty实例中的Spring MVC servlet的简单基本示例。我的控制器正在拾取页面请求并返回正确的视图名称。当jsp页面被查找失败时。我相信,问题在于Servlet Mappings,基于我已经看过的其他答案,但我没有看到解决方案。
Jetty服务器设置:
public JettyServer(int port) throws JettyServerException {
webServerPort = port;
webServer = new Server(webServerPort);
webServer.setStopAtShutdown(true);
try {
webServer.setHandler(getServletContextHandler(getContext()));
} catch (IOException e) {
throw new JettyServerException("Cannot instantiate JettyServer instance", e);
}
}
private ServletContextHandler getServletContextHandler(WebApplicationContext context) throws IOException {
ServletContextHandler contextHandler = new ServletContextHandler();
contextHandler.setContextPath("/");
contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), "/");
contextHandler.addEventListener(new ContextLoaderListener(context));
contextHandler.setResourceBase(new ClassPathResource("webapp").getURI().toString());
return contextHandler;
}
private WebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("com.company.webapptemplate.config");
return context;
}
在包com.company.webapptemplate.config
下找到的Web App配置类:
@Configuration
@EnableWebMvc
@ComponentScan("com.company.webapptemplate.controller")
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Bean
public UrlBasedViewResolver setupViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
}
控制器代码:
@Controller
@RequestMapping("/")
public class ApplicationController {
@RequestMapping(method = RequestMethod.GET)
public String welcome(ModelMap model) {
return "index";
}
}
运行maven / IntelliJ构建后target/classes
下的非java类内容:
|____webapp
| |____WEB-INF
| | |____views
| | | |____index.jsp
| | |____web.xml (unused)
在IntelliJ中运行应用程序后将浏览器指向localhost:8080时的响应:
Problem accessing /WEB-INF/views/index.jsp. Reason:
Not Found
原木中的吸烟枪我不知道该怎么办:
2014-09-11 23:12:54 DEBUG org.springframework.web.servlet.handler.AbstractHandlerMethodMapping:297 - Looking up handler method for path /WEB-INF/views/index.jsp 2014-09-11 23:12:54 DEBUG org.springframework.web.servlet.handler.AbstractHandlerMethodMapping:305 - Did not find handler method for [/WEB-INF/views/index.jsp] 2014-09-11 23:12:54 TRACE org.springframework.web.servlet.DispatcherServlet:1101 - Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@5ddbd0c7] in DispatcherServlet with name 'org.springframework.web.servlet.DispatcherServlet-44175c06' 2014-09-11 23:12:54 TRACE org.springframework.web.servlet.handler.AbstractUrlHandlerMapping:127 - No handler mapping found for [/WEB-INF/views/index.jsp] 2014-09-11 23:12:54 TRACE org.springframework.web.servlet.DispatcherServlet:1101 - Testing handler map [org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping@a67e8f5] in DispatcherServlet with name 'org.springframework.web.servlet.DispatcherServlet-44175c06' 2014-09-11 23:12:54 TRACE org.springframework.web.servlet.DispatcherServlet:1101 - Testing handler map [org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping@2bef3229] in DispatcherServlet with name 'org.springframework.web.servlet.DispatcherServlet-44175c06' 2014-09-11 23:12:54 TRACE org.springframework.web.servlet.DispatcherServlet:1101 - Testing handler map [org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping@64c63847] in DispatcherServlet with name 'org.springframework.web.servlet.DispatcherServlet-44175c06' 2014-09-11 23:12:54 WARN org.springframework.web.servlet.DispatcherServlet:1120 - No mapping found for HTTP request with URI [/WEB-INF/views/index.jsp] in DispatcherServlet with name 'org.springframework.web.servlet.DispatcherServlet-44175c06'
如果有人能告诉我我错过了哪一块拼图,我将非常感激。
感谢。
答案 0 :(得分:3)
Sotirios评论推动了我前进 - 如何让Jsp处理servlet进入嵌入式Jetty ....
在getServletContextHandler
中,我将contextHandler的实例化更改为WebAppContext
。所有相同的方法都适用于此。
然后我创建了这个方法来获取返回的WebAppContext并设置Jsp Handling(从这里获取的片段:https://github.com/jetty-project/embedded-jetty-jsp/blob/master/src/main/java/org/eclipse/jetty/demo/Main.java)
private void setupJspHandler(WebAppContext context) {
//Ensure the jsp engine is initialized correctly
JettyJasperInitializer sci = new JettyJasperInitializer();
ServletContainerInitializersStarter sciStarter = new ServletContainerInitializersStarter(context);
ContainerInitializer initializer = new ContainerInitializer(sci, null);
List<ContainerInitializer> initializers = new ArrayList<ContainerInitializer>();
initializers.add(initializer);
context.setAttribute("org.eclipse.jetty.containerInitializers", initializers);
context.addBean(sciStarter, true);
// Set Classloader of Context to be sane (needed for JSTL)
// JSP requires a non-System classloader, this simply wraps the
// embedded System classloader in a way that makes it suitable
// for JSP to use
ClassLoader jspClassLoader = new URLClassLoader(new URL[0], this.getClass().getClassLoader());
context.setClassLoader(jspClassLoader);
// Add JSP Servlet (must be named "jsp")
ServletHolder holderJsp = new ServletHolder("jsp",JspServlet.class);
holderJsp.setInitOrder(0);
holderJsp.setInitParameter("logVerbosityLevel","INFO");
holderJsp.setInitParameter("fork","false");
holderJsp.setInitParameter("xpoweredBy","false");
holderJsp.setInitParameter("compilerTargetVM","1.7");
holderJsp.setInitParameter("compilerSourceVM","1.7");
holderJsp.setInitParameter("keepgenerated","true");
context.addServlet(holderJsp, "*.jsp");
}
我在我的maven pom中导入了以下内容:
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>apache-jsp</artifactId>
<version>${jetty.version}</version>
</dependency>
并且还必须将我的日志记录关闭,因为它检查WHOLE类路径时,servlet初始化程序的运行转储了WebApplicationInitializer扫描的日志记录,尽管其他问题和页面似乎提供了一些如何处理此问题的提示,例如....
https://jira.codehaus.org/browse/JETTY-1503
现在它就像一个美女'。
由于