您的位置:首页技术文章
文章详情页

基于Spring MVC Java的配置无法正常工作控制台显示无错误,但我的jsp页面未显示

浏览:41日期:2024-05-13 13:56:48
(adsbygoogle = window.adsbygoogle || []).push({}); 如何解决基于Spring MVC Java的配置无法正常工作控制台显示无错误,但我的jsp页面未显示?

谢谢您的回答。我发现了问题。当我在MvcConfiguration类的顶部编写@ComponentScan时,它起作用了,并且正在显示页面。

解决方法

您好,我正在将我的简单演示项目从Bean配置转换为基于纯Java的配置。Bean配置可以很好地创建表和所有表。但是我的Java配置未显示任何页面。我解决了许多错误bur,现在控制台显示指定问题没有错误。这是我的代码,请查找出什么问题,或者我错过了配置中的任何内容。我是spring的新手,也是基于java的配置的新手。这些是我从中获取代码的网站。

http://codehustler.org/blog/spring-security-tutorial-form-login-java-config/

对于hibernate,我使用本教程

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@Configurationpublic class MvcConfiguraion extends WebMvcConfigurerAdapter{@Overridepublic void configureDefaultServletHandling( DefaultServletHandlerConfigurer configurer ){ configurer.enable();}@Beanpublic 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 {@Autowiredprivate Environment environment;@Beanpublic LocalSessionFactoryBean sessionFactory(){ LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); sessionFactory.setPackagesToScan(new String[] {'com.kharoud.model'}); sessionFactory.setHibernateProperties(hibernateProperties()); return sessionFactory;}@Beanpublic 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;}@Beanpublic 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@Autowiredpublic 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{@Overrideprotected Class<?>[] getRootConfigClasses() { return new Class[] { AppConfiguration.class };}@Overrideprotected Class<?>[] getServletConfigClasses() { // TODO Auto-generated method stub return null;}@Overrideprotected String[] getServletMappings() { return new String[] { '/' };}}

只添加了这些新类。我删除了我的web.xml。

稍后我将添加Spring Security配置类

这是我的控制台输出

Feb 25,2015 2:32:13 PM org.apache.catalina.core.AprLifecycleListener initINFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:Program FilesJavajdk1.8.0_25bin;C:WindowsSunJavabin;C:Windowssystem32;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:ProgramDataOracleJavajavapath;C:Windows system32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShe llv1.0;C:Program FilesJavajdk1.8.0_25bin;;C:ECLIPSEeclipse;;. 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 initINFO: Initializing ProtocolHandler ['http-bio-8080']Feb 25,2015 2:32:14 PM org.apache.coyote.AbstractProtocol initINFO: Initializing ProtocolHandler ['ajp-bio-8009']Feb 25,2015 2:32:14 PM org.apache.catalina.startup.Catalina loadINFO: Initialization processed in 1063 msFeb 25,2015 2:32:14 PM org.apache.catalina.core.StandardService startInternalINFO: Starting service CatalinaFeb 25,2015 2:32:14 PM org.apache.catalina.core.StandardEngine startInternalINFO: Starting Servlet Engine: Apache Tomcat/7.0.47Feb 25,2015 2:32:15 PM org.apache.catalina.util.SessionIdGenerator createSecureRandomINFO: 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 logINFO: Spring WebApplicationInitializers detected on classpath: [com.kharoud.configuration.initilizer.SpringConfigurationInitializer@389ae113]Feb 25,2015 2:32:18 PM org.apache.catalina.core.ApplicationContext logINFO: Initializing Spring root WebApplicationContextlog4j: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 logINFO: Initializing Spring FrameworkServlet ’dispatcher’Feb 25,2015 2:32:26 PM org.apache.coyote.AbstractProtocol startINFO: Starting ProtocolHandler ['http-bio-8080']Feb 25,2015 2:32:26 PM org.apache.coyote.AbstractProtocol startINFO: Starting ProtocolHandler ['ajp-bio-8009']Feb 25,2015 2:32:26 PM org.apache.catalina.startup.Catalina startINFO: Server startup in 11876 ms

MyHomeController

package com.kharoud;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HomeController { @RequestMapping('/') public String welcome(Model model){return 'index'; }}

Myindex.jsp文件位于webapp文件夹下的WEB-INF / views文件夹中

The views were properly resolved with bean configuration.

标签: java