HTTP状态404 – 未找到:对于控制器 – 在Idea中找不到视图解析器

huangapple go评论81阅读模式
英文:

HTTP Status 404 – Not Found: for controller - no view resolvers found in Idea

问题

我有以下的Spring MVC代码:

配置文件

public class MainWebAppInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(final ServletContext sc) throws ServletException {

        AnnotationConfigWebApplicationContext root =
                new AnnotationConfigWebApplicationContext();

        root.register(WebConfig.class);
        root.setServletContext(sc);

        root.scan("testspring");
        sc.addListener(new ContextLoaderListener(root));

        ServletRegistration.Dynamic appServlet =
                sc.addServlet("dispatcher", new DispatcherServlet(new GenericWebApplicationContext()));
        appServlet.setLoadOnStartup(1);
        appServlet.addMapping("/");
    }
}
@EnableWebMvc
@Configuration
@ComponentScan("testspring")
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver bean = new InternalResourceViewResolver();

        bean.setViewClass(JstlView.class);
        bean.setPrefix("/WEB-INF/view/");
        bean.setSuffix(".html");

        return bean;
    }
}

控制器

@Controller
public class HomeController {
    @RequestMapping(value = "/")
    public String home() {
        System.out.println("HomeController: Passing through...");
        return "home.html";
    }
}
@RestController
public class TestRestController {
    @RequestMapping(value = "/api")
    public String home() {
        System.out.println("Rest: Passing through...");
        return "some json";
    }
}

当我部署时,我会得到以下错误:

访问/api页面,却可以正常工作。

编辑

在将home.html替换为home之后,错误仍然存在 - 在IDE中和浏览器中都是404错误:

还可能与此相关 - 我在其中一个配置类中收到此弹出窗口:

英文:

I have the following Spring MVC code:

config files:

public class MainWebAppInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(final ServletContext sc) throws ServletException {

        AnnotationConfigWebApplicationContext root =
                new AnnotationConfigWebApplicationContext();

        root.register(WebConfig.class);
        root.setServletContext(sc);

        root.scan("testspring");
        sc.addListener(new ContextLoaderListener(root));

        ServletRegistration.Dynamic appServlet =
                sc.addServlet("dispatcher", new DispatcherServlet(new GenericWebApplicationContext()));
        appServlet.setLoadOnStartup(1);
        appServlet.addMapping("/");
    }
}
@EnableWebMvc
@Configuration
@ComponentScan("testspring")
public class WebConfig extends WebMvcConfigurerAdapter {
/*
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }*/

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver bean = new InternalResourceViewResolver();

        bean.setViewClass(JstlView.class);
        bean.setPrefix("/WEB-INF/view/");
        bean.setSuffix(".html");

        return bean;
    }
}

contollers:

@Controller
public class HomeController {
    @RequestMapping(value = "/")
    public String home() {
        System.out.println("HHomeController: Passing through...");
        return "home.html";
    }
}
@RestController
public class TestRestController {
    @RequestMapping(value = "/api")
    public String home() {
        System.out.println("Rest: Passing through...");
        return "some json";
    }
}

I get the following errors when I deploy:

HTTP状态404 – 未找到:对于控制器 – 在Idea中找不到视图解析器

HTTP状态404 – 未找到:对于控制器 – 在Idea中找不到视图解析器

Accessing the /api page, however, works just fine.

EDIT:

After replacing home.html with home, the error is still present - both in IDE and the 404 in browser:

HTTP状态404 – 未找到:对于控制器 – 在Idea中找不到视图解析器

Also maybe it's relevant - I get this pop up at one of the config classes:

HTTP状态404 – 未找到:对于控制器 – 在Idea中找不到视图解析器

答案1

得分: 0

移除 .html,只需返回引号中的视图,如:“home”。

英文:

remove .html just return the view in quotation like so: "home"

答案2

得分: 0

将home.html更改为home,并确保您在/WEB-INF/view/文件夹内有home.html文件。

英文:

Replace home.html to home & make sure u have home.html file inside /WEB-INF/view/ folder.

答案3

得分: 0

尝试添加请求方法

@Controller
public class HomeController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home() {
        System.out.println("HomeController: 正在通过...");
        return "home";
    }
}
英文:

Try adding request method,

@Controller
public class HomeController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home() {
        System.out.println("HHomeController: Passing through...");
        return "home";
    }
}

答案4

得分: 0

问题是我在想要通过thymeleaf呈现的.html页面中使用了JstlView.classViewResolver。如果我将WebConfig内的代码更改为:

@Bean
public ViewResolver viewResolver() {
    InternalResourceViewResolver bean = new InternalResourceViewResolver();

    bean.setViewClass(JstlView.class);
    bean.setPrefix("/WEB-INF/view/");
    bean.setSuffix(".jsp");

    return bean;
}

并将home.html替换为home.jsp,它可以正常工作。

不确定如何处理thymeleaf。我决定改用REST控制器,但也许有人以后能够发布代码的thymeleaf友好版本。有人向我推荐了这个链接:https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html,可以编写正确的方法。可能以后会回到这个答案进行编辑。

英文:

The problem was me using JstlView.class and ViewResolver with .html pages that I wanted to render via thymeleaf. If I change the code inside WebConfig to:

   @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver bean = new InternalResourceViewResolver();

        bean.setViewClass(JstlView.class);
        bean.setPrefix("/WEB-INF/view/");
        bean.setSuffix(".jsp");

        return bean;
    }

and replace home.html with home.jsp it works just fine.

Not sure how I'd need to do with thymeleaf. I decided to go with rest controllers instead, but maybe someone would be able to post thymeleaf-friendly version of the code later. I was recommended this: https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html for writting a correct method. Might come back to this answer to edit later.

huangapple
  • 本文由 发表于 2020年5月4日 04:05:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/61580907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定