Java – embedded jetty – spring MVC – view Parser – no XML – HTTP error: 404
•
Java
I'm trying to build a simple spring MVC server using embedded jetty I've set up the server, enabled spring, and set up The JSP file configures a view parser The controller gives me 404 the following message:
Problem accessing /jsp/test.jsp. Reason: Not Found
Who knows what the problem is? I've been Googling for two days
The server:
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;
}
Spring configuration / view parser:
@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;
}
controller:
@RequestMapping("/")
public ModelAndView index() {
return new ModelAndView("test");
}
Edit: folder structure:
│ 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. In XML:
<resources>
<resource>
<targetPath>/webapp</targetPath>
<directory>src/main/webapp</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
Solution
Solution:
"MAPPING_URL" to "/". "ServletContextHandler" to "WebAppContext"
Add the missing dependency in POM:
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>9.2.0.M0</version>
</dependency>
You must also configure the IDE to use JDK instead of JRE
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
