英文:
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:
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:
Also maybe it's relevant - I get this pop up at one of the config classes:
答案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.class
和ViewResolver
。如果我将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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论