Java Spring框架模型和视图:未找到404页,请参考名为';索引';;模型为空
我是Spring框架的新手,在学习Spring3.2.3教程时遇到了问题。我试图使用ModelAndView返回一个jsp页面,但当我传递页面名称(index.jsp)时,它返回对索引的引用为空 我已检查index.jsp是否位于正确的目录(“src/main/webapp/WEB-INF/pages/index.jsp”) 以下是配置:Java Spring框架模型和视图:未找到404页,请参考名为';索引';;模型为空,java,spring,jsp,spring-mvc,tomcat,Java,Spring,Jsp,Spring Mvc,Tomcat,我是Spring框架的新手,在学习Spring3.2.3教程时遇到了问题。我试图使用ModelAndView返回一个jsp页面,但当我传递页面名称(index.jsp)时,它返回对索引的引用为空 我已检查index.jsp是否位于正确的目录(“src/main/webapp/WEB-INF/pages/index.jsp”) 以下是配置: @Controller public class NavigationController { @RequestMapping(value={"/"
@Controller
public class NavigationController {
@RequestMapping(value={"/", "index"}, method=RequestMethod.GET)
public ModelAndView homePage() {
ModelAndView _result= new ModelAndView("index");
return _result;
}
}
_结果是“对名为'index'的视图的引用;模型为空”
有关初始化器类的更多详细信息
public class Initializer implements WebApplicationInitializer {
private static final String DISPATCHER_SERVLET_NAME = "dispatcher";
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(WebAppConfig.class);
ctx.setServletContext(servletContext);
registerHiddenHttpMethodFilter(servletContext);
Dynamic servlet = servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
private void registerHiddenHttpMethodFilter(ServletContext servletContext) {
FilterRegistration.Dynamic fr = servletContext
.addFilter("hiddenHttpMethodFilter", HiddenHttpMethodFilter.class);
fr.addMappingForServletNames(
EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD),
false,
DISPATCHER_SERVLET_NAME);
}
然后是WebAppConfig类
@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("com.eko")
@PropertySource("classpath:application.properties")
@EnableJpaRepositories("com.eko.repository")
public class WebAppConfig extends WebMvcConfigurerAdapter {
private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";
private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";
@Resource
private Environment env;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistence.class);
entityManagerFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
entityManagerFactoryBean.setJpaProperties(hibProperties());
return entityManagerFactoryBean;
}
private Properties hibProperties() {
Properties properties = new Properties();
properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
return properties;
}
@Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(true)
.useJaf(false)
.ignoreAcceptHeader(true)
.mediaType("html", MediaType.TEXT_HTML)
.mediaType("json", MediaType.APPLICATION_JSON)
.defaultContentType(MediaType.TEXT_HTML);
}
@Bean
public ViewResolver contentNegotiatingViewResolver(
ContentNegotiationManager manager) {
List<ViewResolver> resolvers = new ArrayList<ViewResolver>();
InternalResourceViewResolver r1 = new InternalResourceViewResolver();
r1.setPrefix("/WEB-INF/pages/");
r1.setSuffix(".jsp");
r1.setViewClass(JstlView.class);
resolvers.add(r1);
JsonViewResolver r2 = new JsonViewResolver();
resolvers.add(r2);
ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
resolver.setViewResolvers(resolvers);
resolver.setContentNegotiationManager(manager);
return resolver;
}
/**
* View resolver for returning JSON in a view-based system. Always returns a
* {@link MappingJacksonJsonView}.
*/
public class JsonViewResolver implements ViewResolver {
public View resolveViewName(String viewName, Locale locale)
throws Exception {
MappingJacksonJsonView view = new MappingJacksonJsonView();
view.setPrettyPrint(true);
return view;
}
}
但是我不明白为什么会这样生成它尝试删除视图名称中的.jsp部分。将其保留在
index
尝试传递“index”而不是“index.jsp”。您已经在配置中管理了后缀。完成后,将其更改为仅索引,但仍然不起作用。顺便说一句,我添加了我拥有的JSP页面。在语句ModelAndView中,没有生成任何异常_result=new ModelAndView(“index”)_结果是“对名为“index”的视图的引用;模型为空”
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<P> HELLO WORLD </P>
</body>
</html>
--MVCSpring-0.0.1-SNAPSHOT
----META-INF
----WEB-INF
------classes
--------com
--------META-INF
--------WEB-INF
----------pages
------------index.jsp
----------web.xml
--------application.properties
------lib