基于 Spring mvc Java 的配置不起作用。控制台显示没有错误但我的jsp页面没有显示
Posted
技术标签:
【中文标题】基于 Spring mvc Java 的配置不起作用。控制台显示没有错误但我的jsp页面没有显示【英文标题】:Spring mvc Java based Configuration not working. Console display no error but my jsp page is not showing 【发布时间】:2015-04-27 05:31:07 【问题描述】:您好,我正在将我的简单演示项目从 bean 配置转换为纯 Java 配置。 Bean 配置可以很好地创建表和所有内容。但是我的 java 配置没有显示任何页面。我解决了很多错误,现在控制台显示没有指定问题的错误。这是我的代码,请找出问题所在,或者我错过了配置中的任何内容。我是 spring 新手,对基于 java 的配置相当陌生。这些是我从中获取代码的网站。
http://codehustler.org/blog/spring-security-tutorial-form-login-java-config/
对于休眠,我使用了本教程
http://websystique.com/spring/spring4-hibernate4-mysql-maven-integration-example-using-annotations/
我的课
1. AppConfiguration
package com.kharoud.configuration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@ComponentScan("com.kharoud")
@Import(MvcConfiguraion.class, RepositoryConfiguration.class)
public class AppConfiguration
2.Mvc配置
package com.kharoud.configuration;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@EnableWebMvc
@Configuration
public class MvcConfiguraion extends WebMvcConfigurerAdapter
@Override
public void configureDefaultServletHandling( DefaultServletHandlerConfigurer configurer )
configurer.enable();
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver()
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views");
resolver.setSuffix(".jsp");
return resolver;
3.RepositoryConfiguration 包 com.kharoud.configuration;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@PropertySource( "classpath:hibernate.properties" )
public class RepositoryConfiguration
@Autowired
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory()
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] "com.kharoud.model");
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
@Bean
public Properties hibernateProperties()
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
return properties;
@Bean
public DataSource dataSource()
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s)
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
4.SpringConfigurationInitializer
package com.kharoud.configuration.initilizer;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import com.kharoud.configuration.AppConfiguration;
public class SpringConfigurationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
@Override
protected Class<?>[] getRootConfigClasses()
return new Class[] AppConfiguration.class ;
@Override
protected Class<?>[] getServletConfigClasses()
// TODO Auto-generated method stub
return null;
@Override
protected String[] getServletMappings()
return new String[] "/" ;
只添加了这些新类。我删除了我的 web.xml。
稍后我会添加 Spring Security 配置类
这是我的控制台输出
Feb 25, 2015 2:32:13 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.8.0_25\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Window s;C:/Program Files/Java/jre1.8.0_25/bin/server;C:/Program Files/Java/jre1.8.0_25/bin;C:/Program Files/Java/jre1.8.0_25/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Windows\ system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShe ll\v1.0\;C:\Program Files\Java\jdk1.8.0_25\bin;;C:\ECLIPSE\eclipse;;.
Feb 25, 2015 2:32:14 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]Server/Service/Engine/Host/Context Setting property 'source' to 'org.eclipse.jst.jee.server:ProjectDemo' did not find a matching property.
Feb 25, 2015 2:32:14 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Feb 25, 2015 2:32:14 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Feb 25, 2015 2:32:14 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1063 ms
Feb 25, 2015 2:32:14 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Feb 25, 2015 2:32:14 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
Feb 25, 2015 2:32:15 PM org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [217] milliseconds.
Feb 25, 2015 2:32:18 PM org.apache.catalina.core.ApplicationContext log
INFO: Spring WebApplicationInitializers detected on classpath: [com.kharoud.configuration.initilizer.SpringConfigurationInitializer@389ae113]
Feb 25, 2015 2:32:18 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
Feb 25, 2015 2:32:26 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
Feb 25, 2015 2:32:26 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Feb 25, 2015 2:32:26 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Feb 25, 2015 2:32:26 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 11876 ms
我的家庭控制器
package com.kharoud;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController
@RequestMapping("/")
public String welcome(Model model)
return "index";
Myindex.jsp 文件位于 webapp 文件夹下的 WEB-INF/views 文件夹中
The views were properly resolved with bean configuration.
【问题讨论】:
您的MvcConfiguration
不应由ContextLoaderListener
加载,而应由DispatcherServlet
加载。移除AppConfiguration
类,让正确的组件加载不同的配置。
或者让DispatcherServlet
加载所有配置,而不是ContextLoaderListener
。
我应该删除我的 AppConfiguration 类。 ContextLoaderListener 如何以及从何处加载我的 MvcConfiguration。我应该通过 SpringConfigurationInitializer 初始化所有类吗?您能否提供一些加载 dispatcherServlet 的代码。谢谢
AbstractAnnotationConfigDispatcherServletInitializer
负责您只需传递正确的配置类。让getServletConfigClasses
返回AppConfiguration
而不是getRootConfigClasses
可能会解决您的问题。
我进行了更改,但仍然没有效果。我还在我的 MvcConfiguration 类中将 @Configurable 更正为 -@Configuration 但它也没有让它工作。
【参考方案1】:
感谢您的回答。我发现了问题。当我在我的 MvcConfiguration 类之上编写 @ComponentScan 时,它可以工作并且页面正在显示。
【讨论】:
以上是关于基于 Spring mvc Java 的配置不起作用。控制台显示没有错误但我的jsp页面没有显示的主要内容,如果未能解决你的问题,请参考以下文章
基于 Spring MVC Java 的配置中的 AuthenticationSuccessHandler
企业级信息系统开发学习笔记1.8 基于Java配置方式使用Spring MVC