我在使用 SpringMVC 中的 AbstractAnnotationConfigDispatcherServletInitializer
来实现使用 java 配置 servlet。
我在访问 localhost:8080/
出现的是 404 错误,下面是相关代码
WebAppInitializer
public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{ WebConfig.class };
}
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{ RootConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
WebConfig
@Configuration
@EnableWebMvc
@ComponentScan("spittr.web")
public class WebConfig extends WebMvcConfigurerAdapter{
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
HomeController
@Controller
public class HomeController {
@RequestMapping(value = "/",method = GET)
public String home(){
return "home";
}
}
home.jsp
文件在 WEB-INF/views/
下
Maven
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version> 4.1.5.RELEASE</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
...
</dependencies>
请问一下,为什么我在访问 `localhost:8080/` 出现的是 404 错误
ringa_lee2017-04-18 10:30:47
I found the answer... When Intellij IDEA
在导出 artfacts
, I derived this structure:
artfacts/
--/ META-INF
--/ WEB-INF
--/ veiws
--/ home.jsp
--/ classes
--/(class文件)
Refer to Tomcat 7.0 Standard Directory Layout to find out.
Not here /WEB-INF/lib
This folder is used to contain dependency packages required for compilation... The project is not automatically configured for you...
And the exported artfacts
has not yet reported an error message...
Go to Project Structure
to change the content of out_put
yourself, and put the dependency package under Project Structure
内去改变 out_put
的内容,将依赖包放入 /WEB-INF/lib
...