2014-05-04 8 views
7

Próbuję skonfigurować prosty serwer Spring MVC za pomocą wbudowanego Jetty. Skonfigurowałem serwer, włączono sprężynę i skonfigurowałem przelicznik widoku dla plików .jsp. Sterownik daje mi 404 z następującym komunikatem:Osadzone Molo - Wiosna MVC - Rzutnik widoku - NO XML - HTTP BŁĄD: 404

Problem accessing /jsp/test.jsp. Reason: 
Not Found 

Czy ktoś wie w czym jest problem? Prowadzę google przez dwa dni.

Serwer:

private static final int DEFAULT_PORT = 8080; 
private static final String CONTEXT_PATH = "/"; 
private static final String CONFIG_LOCATION = "spring.config"; 
private static final String MAPPING_URL = "/*"; 

private EmbeddedJettyServer(){ 
} 

public static void main(String[] args) throws Exception { 
    Server server = new Server(DEFAULT_PORT); 
    server.setHandler(getServletContextHandler(getContext())); 
    server.start(); 
    server.join(); 
} 

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

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

Wiosna config/Widok rezolwer:

@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages="controller") 
public class WebMvcConfig extends WebMvcConfigurerAdapter { 

@Override 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
    super.addResourceHandlers(registry); 
    registry.addResourceHandler("/html/**").addResourceLocations("/resources/html/"); 
} 

@Bean 
public InternalResourceViewResolver viewResolver() { 
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 
    viewResolver.setViewClass(JstlView.class); 
    viewResolver.setPrefix("/jsp/"); 
    viewResolver.setContentType("text/html; charset=UTF-8"); 
    viewResolver.setSuffix(".jsp"); 
    return viewResolver; 
} 

Kontroler:

@RequestMapping("/") 
public ModelAndView index() { 
    return new ModelAndView("test"); 
} 

Edit: struktura Folder:

│ pom.xml 
│ 
├───src 
│ ├───main 
│ │ ├───java 
│ │ │ ├───controller 
│ │ │ │  Ping.java 
│ │ │ │  Prime.java 
│ │ │ │ 
│ │ │ ├───run 
│ │ │ │  EmbeddedJettyServer.java 
│ │ │ │ 
│ │ │ └───spring 
│ │ │  └───config 
│ │ │    WebMvcConfig.java 
│ │ │ 
│ │ ├───resources 
│ │ │ ├───html 
│ │ │ │  test.html 
│ │ │ │ 
│ │ │ └───jsp 
│ │ │   test.jsp 
│ │ │ 
│ │ └───webapp 
│ │  └───jsp 
│ │    test.jsp 
│ │ 
│ └───test 
│  └───java 
└───target 

pom.xml:

<resources> 
     <resource> 
      <targetPath>/webapp</targetPath> 
      <directory>src/main/webapp</directory> 
     </resource> 
     <resource> 
      <directory>src/main/resources</directory> 
     </resource> 
    </resources> 
+0

Czy plik jsp/test.jsp istnieje w twoim projekcie? –

+0

Tak. Doda strukturę folderów do oryginalnego postu. – PalSivertsen

Odpowiedz

2

Rozwiązanie: W "EmbeddedJettyServer" Zmieniłem

"MAPPING_URL" to "/". 
"ServletContextHandler" to "WebAppContext" 

Dodano missin zależność w pom:

<dependency> 
     <groupId>org.eclipse.jetty</groupId> 
     <artifactId>jetty-jsp</artifactId> 
     <version>9.2.0.M0</version> 
    </dependency> 

miał również skonfigurować do korzystania IDE JDK zamiast JRE

+0

Pracował jak urok! Dzięki! – Jagger

+0

Jaką wersję serwletu użyłeś? Otrzymuję wyjątek. "Zatrzymany przez: java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus() I'' – zhaoyou