Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 在Spring启动应用程序中找不到URI为[/WEB-INF/jsp/index.jsp]的HTTP请求的映射_Spring Mvc_Spring Boot - Fatal编程技术网

Spring mvc 在Spring启动应用程序中找不到URI为[/WEB-INF/jsp/index.jsp]的HTTP请求的映射

Spring mvc 在Spring启动应用程序中找不到URI为[/WEB-INF/jsp/index.jsp]的HTTP请求的映射,spring-mvc,spring-boot,Spring Mvc,Spring Boot,请帮忙。我正在开发一个使用SpringJPA、Hateoas和SpringDataRESTMVC的SpringBoot应用程序。该应用程序可以分为两个主要部分:RESTFul API和Web应用程序。Restful API工作得非常好。现在唯一的问题是让我的JSP页面正常工作。尝试时,我收到以下日志消息 Application.java import org.springframework.boot.SpringApplication; import org.springframework.bo

请帮忙。我正在开发一个使用SpringJPA、Hateoas和SpringDataRESTMVC的SpringBoot应用程序。该应用程序可以分为两个主要部分:RESTFul API和Web应用程序。Restful API工作得非常好。现在唯一的问题是让我的JSP页面正常工作。尝试时,我收到以下日志消息

Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@SpringBootApplication
@EnableJpaRepositories
@EnableWebMvc

public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public ViewResolver viewResolver() {
       InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
       viewResolver.setPrefix("/WEB-INF/jsp/");
       viewResolver.setSuffix(".jsp");
       return viewResolver;
   }

}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
WebController.java

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class WebController {

    @RequestMapping(value = "/home", method=RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public ModelAndView index() {
        return new ModelAndView("index");
    }

}
import com.stalinkay.rentadvd.domain.DVD;
import com.stalinkay.rentadvd.service.DVDService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/dvd")
public class DVDController {

    private final DVDService dvdService;

    @Autowired
    public DVDController(DVDService dvdService) {
        this.dvdService = dvdService;
    }

    /**
     * Create DVD
     *
     * @param requestDVD Spring uses the RequestBody to create a new DVD
     * to save to the database
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.CREATED)
    HttpHeaders create(@RequestBody DVD requestDVD) {
        return dvdService.create(requestDVD);
    }

    /**
     * Retrieve DVD
     *
     * @param dvdid
     * @return
     */
    @RequestMapping(value = "/{dvdid}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    Resource<DVD> retrieve(@PathVariable Long dvdid) {
        return dvdService.retrieve(dvdid);
    }

    /**
     * Retrieve All DVDs
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    List<Resource<DVD>> retrieveAll() {
        return dvdService.retrieveAll();
    }

    /**
     * Update DVD
     *
     * @param requestDVD Spring uses the RequestBody to create a new DVD
     * to save to the database
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    HttpHeaders update(@RequestBody DVD requestDVD) {
        return dvdService.update(requestDVD);
    }

    /**
     * Delete DVD
     *
     * @param dvdid
     * @return
     */
    @RequestMapping(value = "/{dvdid}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    void delete(@PathVariable Long dvdid) {
        dvdService.delete(dvdid);
    }
}
DVDController.java

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class WebController {

    @RequestMapping(value = "/home", method=RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public ModelAndView index() {
        return new ModelAndView("index");
    }

}
import com.stalinkay.rentadvd.domain.DVD;
import com.stalinkay.rentadvd.service.DVDService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/dvd")
public class DVDController {

    private final DVDService dvdService;

    @Autowired
    public DVDController(DVDService dvdService) {
        this.dvdService = dvdService;
    }

    /**
     * Create DVD
     *
     * @param requestDVD Spring uses the RequestBody to create a new DVD
     * to save to the database
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.CREATED)
    HttpHeaders create(@RequestBody DVD requestDVD) {
        return dvdService.create(requestDVD);
    }

    /**
     * Retrieve DVD
     *
     * @param dvdid
     * @return
     */
    @RequestMapping(value = "/{dvdid}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    Resource<DVD> retrieve(@PathVariable Long dvdid) {
        return dvdService.retrieve(dvdid);
    }

    /**
     * Retrieve All DVDs
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    List<Resource<DVD>> retrieveAll() {
        return dvdService.retrieveAll();
    }

    /**
     * Update DVD
     *
     * @param requestDVD Spring uses the RequestBody to create a new DVD
     * to save to the database
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    HttpHeaders update(@RequestBody DVD requestDVD) {
        return dvdService.update(requestDVD);
    }

    /**
     * Delete DVD
     *
     * @param dvdid
     * @return
     */
    @RequestMapping(value = "/{dvdid}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    void delete(@PathVariable Long dvdid) {
        dvdService.delete(dvdid);
    }
}
导入com.stalinkay.rentavd.domain.DVD;
导入com.stalinkay.rentavd.service.DVDService;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.hateoas.Resource;
导入org.springframework.http.HttpHeaders;
导入org.springframework.http.HttpStatus;
导入org.springframework.http.MediaType;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.RequestBody;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.ResponseStatus;
导入org.springframework.web.bind.annotation.RestController;
@RestController
@请求映射(“/dvd”)
公共类DVD控制器{
私人最终DVD服务;
@自动连线
公共DVD控制器(DVD服务DVD服务){
this.dvdService=dvdService;
}
/**
*制作DVD
*
*@param requestDVD Spring使用RequestBody创建新的DVD
*保存到数据库
*@返回
*/
@RequestMapping(value=”“,method=RequestMethod.POST,consumes=MediaType.APPLICATION\u JSON\u value,products=MediaType.APPLICATION\u JSON\u value)
@ResponseStatus(HttpStatus.CREATED)
HttpHeaders创建(@RequestBody DVD requestDVD){
返回DVD服务。创建(请求DVD);
}
/**
*检索DVD
*
*@param dvdid
*@返回
*/
@RequestMapping(value=“/{dvdid}”,method=RequestMethod.GET,products=MediaType.APPLICATION\u JSON\u value)
@ResponseStatus(HttpStatus.OK)
资源检索(@PathVariable Long dvdid){
返回dvdService.retrieve(dvdid);
}
/**
*检索所有DVD
*
*@返回
*/
@RequestMapping(value=”“,method=RequestMethod.GET,products=MediaType.APPLICATION\u JSON\u value)
@ResponseStatus(HttpStatus.OK)
列表检索all(){
返回dvdService.retrieveAll();
}
/**
*更新DVD
*
*@param requestDVD Spring使用RequestBody创建新的DVD
*保存到数据库
*@返回
*/
@RequestMapping(value=”“,method=RequestMethod.PUT,consumes=MediaType.APPLICATION\u JSON\u value,products=MediaType.APPLICATION\u JSON\u value)
@ResponseStatus(HttpStatus.OK)
HttpHeaders更新(@RequestBody DVD requestDVD){
返回DVD服务。更新(请求DVD);
}
/**
*删除DVD
*
*@param dvdid
*@返回
*/
@RequestMapping(value=“/{dvdid}”,method=RequestMethod.DELETE,products=MediaType.APPLICATION\u JSON\u value)
@ResponseStatus(HttpStatus.OK)
void delete(@PathVariable Long dvdid){
dvdService.delete(dvdid);
}
}
我包含了我的DVDController.java,只是为了表明我在RESTful API中使用@RestController,在Web应用中使用@Controller

如何让Spring为我的JSP页面提供服务?我不想使用xml。我希望在一个项目中同时使用RESTful API和Web应用程序


提前谢谢你。

这就是我的结局

Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@SpringBootApplication
@EnableJpaRepositories
@EnableWebMvc

public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public ViewResolver viewResolver() {
       InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
       viewResolver.setPrefix("/WEB-INF/jsp/");
       viewResolver.setSuffix(".jsp");
       return viewResolver;
   }

}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
已将application.properties更新为:

spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp
我有@EnableWebMvc,这也导致了视图解析问题


我无意中发现了这一点。

您是否实现了WebApplicationInitializer否。Spring Boot不解决这个问题吗?@faljbour,ViewResolver不应该足够吗?不,我不这么认为,您需要创建一个dispatcher servlet并注册它。除非您在web.xml文件中定义了它,否则您所指的链接会断开。您提出的解决方案在我的情况下不起作用!:(