【发布时间】:2019-12-10 13:28:10
【问题描述】:
在我的java项目中:
在 pom.xml 中
<groupId>shop</groupId>
<artifactId>shop</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
这里是控制器:
@Controller
public class SellerController {
@Autowired
private SellerService sellerService;
// handler methods will go here
@RequestMapping("/")
public ModelAndView home() {
List<Seller> listSeller = sellerService.listAll();
ModelAndView mav = new ModelAndView("index");
mav.addObject("listSeller", listSeller);
return mav;
}
}
public class WebAppInitializer implements WebApplicationInitializer {
// The onStartup() method of this class will be automatically invoked by the
// servlet container when the application is being loaded
public void onStartup(ServletContext servletContext)
throws ServletException {
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(WebMvcConfig.class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
"SpringDispatcher", new DispatcherServlet(appContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
在 Spring 控制器中:
@Configuration
@ComponentScan("com.myproject.shop.seller")
@EnableWebMvc
public class WebMvcConfig {
@Bean(name = "viewResolver")
public InternalResourceViewResolver getViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
我使用 SQLite 数据库。文件所在位置:WebContent/WEB-INF/data/shop.db
在文件夹WebContent/META-INF/persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="shop">
<properties>
<property name="dialect" value="org.hibernate.dialect.SQLiteDialect" />
<property name="javax.persistence.jdbc.url" value="jdbc:sqlite:WEB-INF\data\shop.db" />
<property name="javax.persistence.jdbc.user" value="" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="javax.persistence.jdbc.driver" value="org.sqlite.JDBC" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
WebContent/WEB-INF/views/index.jsp 中的 index.jsp 文件
mvn clean verify 成功构建项目并成功部署到 Tomcat 9 (shop.war)
但是当我尝试打开时
http://localhost:8080/shop
我收到 404 错误。
Type Status Report
Message /shop
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
【问题讨论】:
-
你的控制器在哪里,或者/shop的映射???
-
检查答案,你没有/shop的映射,或者只是尝试“http://localhost:8080/”
-
@billalGHILAS 没有帮助。结果相同
-
您的
WebConfig需要一个@EnableWebMvc注释。您的@ComponentScan中还有一个额外的空格。是您正在扫描的包中的@Controller。最后,您要部署到 tomcat 的 WAR 的名称是什么?战争的名称是根上下文。从你的 pom 来看,我怀疑 url 类似于localhost:8080/shop-0.0.1。此外,如果您真的使用 Spring Boot,您的WebAppInitializer是错误的。 -
@M.Deinum 没有帮助。同样的错误
标签: java spring-boot spring-mvc maven-3