【问题标题】:Can't run web application (Spring MVC, JPA)无法运行 Web 应用程序(Spring MVC、JPA)
【发布时间】: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


【解决方案1】:

在 SellerController 中,而不是 @RequestMapping("/") 执行 @RequestMapping("/shop") 这样,您将指向控制器以收听“/shop”而不是“/”

在WebAppInitializer上的onStartup,添加dispatcher.addMapping("/shop");

【讨论】:

  • 没有帮助。结果相同
【解决方案2】:

在您显示的代码中,没有为 /shop 端点定义映射。

我看到了你的更新,但 /shop 仍然没有映射。

试试这个:

@Controller
public class SellerController {

    @Autowired
    private SellerService sellerService;

    // handler methods will go here
    @RequestMapping("/shop") //<--HERE!!!
    public ModelAndView home() {
        List<Seller> listSeller = sellerService.listAll();
        ModelAndView mav = new ModelAndView("index");
        mav.addObject("listSeller", listSeller);
        return mav;
    }
}

【讨论】:

    【解决方案3】:

    View Resolver 可以直接在 property 文件中定义,如下所示

    spring.mvc.view.prefix=/WEB-INF/jsp/
    spring.mvc.view.suffix=.jsp
    

    创建一个类并使用 @Controller 对其进行注解,并按照以下示例进行操作

    @RequestMapping(value="/login", method = RequestMethod.GET)
        public String showLoginPage(ModelMap model){
            return "login";
        }
    

    【讨论】:

    • 海报未使用 Spring Boot,您的答案适用于 Spring Boot 而不是普通 Spring。
    • 谢谢@M.Deinum 但是问题没有得到澄清......让我也更新我的答案:)
    猜你喜欢
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多