WEB-INF/folder/.jsp gives 404

2019-07-26 06:58发布

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.

enter image description here

2条回答
Root(大扎)
2楼-- · 2019-07-26 07:03

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.

查看更多
Root(大扎)
3楼-- · 2019-07-26 07:15

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

查看更多
登录 后发表回答