无法运行Web应用程序(Spring MVC,JPA)

时间:2019-12-10 13:28:10

标签: java spring-boot spring-mvc maven-3

在我的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.

3 个答案:

答案 0 :(得分:0)

在您显示的代码中,没有为/ 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;
    }
}

答案 1 :(得分:0)

在SellerController中,使用@RequestMapping(“ / shop”)代替@RequestMapping(“ /”) 这样,您将使控制器指向“ / shop”而不是“ /”

然后在WebAppInitializer上的onStartup上,添加 dispatcher.addMapping(“ / shop”);

答案 2 :(得分:-1)

可以直接在属性文件中定义

View Resolver,如下所示

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";
    }