WEB-INF/folder/.jsp gives 404

2019-07-26 07:16发布

问题:

I have a spring MVC project and bunch of jsp files. When I try to access my jsp files in WEB-INF/views/ it gives me 404.

Screenshot for reference.

回答1:

You can't access jsp page inside WEB-INF folder directly. To access it you will need to create InternalResourceViewResolver bean for resolving path for your jsp pages:

@EnableWebMvc
@Configuration
public class AppConfig extends WebMvcConfigurerAdapter {
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        internalResourceViewResolver.setPrefix("/WEB-INF/views/");
        internalResourceViewResolver.setSuffix(".jsp");
        return internalResourceViewResolver;
    } 

And in your controller just return name of that jsp page:

@Controller
public class EmployeeController {

    @RequestMapping(value = "/add/employee", method = RequestMethod.GET)
    public String addEmployee(Model model) {
        return "addEmployee";
    }
}

Now hit URL: http://localhost:8181/SpringMVC_CRUD/add/employee



回答2:

The stuff in WEB-INF is not exposed to the public URL space. You can only include/access it from other files. This is useful for templates and such (and of course, your compiled code also resides in there).

Move your public JSP outside of WEB-INF.