2012-03-24 81 views
2

我一直在使用Spring 3.1.1的新特性“基于java的配置”。 我在创建一个小演示来测试这个新概念时遇到了一个问题。Spring 3.1.1基于java的配置问题

当我部署我的 “springway.war” 在tomcat的,当我要求的 “http://本地主机:8080/springway /” - >我痛风 “是HTTP状态404”

我相信我错过了一些东西,但我无法弄清楚。 我一直在寻找Spring Green站点上的“GreenHouse”示例,但我仍然遇到同样的问题。

这里是我的WebConfig.java:

package config; 

import java.util.List; 

import javax.inject.Inject; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.support.ReloadableResourceBundleMessageSource; 
import org.springframework.core.env.Environment; 
import org.springframework.http.converter.HttpMessageConverter; 
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter; 

import org.springframework.validation.Validator; 
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; 

import org.springframework.web.multipart.MultipartResolver; 
import org.springframework.web.multipart.commons.CommonsMultipartResolver; 
import org.springframework.web.servlet.ViewResolver; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 

import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 
import org.springframework.web.servlet.view.UrlBasedViewResolver; 
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; 
import org.springframework.web.servlet.view.freemarker.FreeMarkerView; 
import org.springframework.web.servlet.view.tiles2.TilesConfigurer; 
import org.springframework.web.servlet.view.tiles2.TilesView; 

@Configuration 
@EnableWebMvc 
public class WebConfig extends WebMvcConfigurerAdapter { 

    @Inject 
    private Environment environment; 

    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); 
    } 

    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { 
     converters.add(new MappingJacksonHttpMessageConverter()); 
    } 


    /** 
    * ViewResolver configuration required to work with Tiles2-based views. 
    */ 
    @Bean 
    public ViewResolver viewResolver() { 
     UrlBasedViewResolver viewResolver = new UrlBasedViewResolver(); 
     viewResolver.setViewClass(FreeMarkerView.class); 
     return viewResolver; 
    } 

    /** 
    * Configures Tiles at application startup. 
    */ 
    @Bean 
    public TilesConfigurer tilesConfigurer() { 
     TilesConfigurer configurer = new TilesConfigurer(); 
     configurer.setDefinitions(new String[]{ 
       "/WEB-INF/layouts/tiles.xml", 
       "/WEB-INF/views/**/tiles.xml" 
     }); 
     configurer.setCheckRefresh(true); 
     return configurer; 
    } 

// @Bean 
// public FreeMarkerConfigurer freeMarkerConfigurer() { 
//  FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); 
//  configurer.setTemplateLoaderPath(
// 
//    "/WEB-INF/views/**/freemarker.xml"); 
////  configurer.set(true); 
//  return configurer; 
// } 

    @Bean 
    public MultipartResolver multipartResolver() { 
     CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); 
     multipartResolver.setMaxUploadSize(500000); 
     return multipartResolver; 
    } 
} 

而且在web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

    <!-- Java-based annotation-driven Spring container definition --> 
    <context-param> 
     <param-name>contextClass</param-name> 
     <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> 
    </context-param> 

    <!-- Location of Java @Configuration classes that configure the components that makeup this application --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>config</param-value> 
    </context-param> 

    <!-- Specifies the default mode of this application, to be activated if no other profile (or mode) is specified --> 
    <context-param> 
     <param-name>spring.profiles.default</param-name> 
     <param-value>embedded</param-value> 
    </context-param> 

    <!-- Creates the Spring Container shared by all Servlets and Filters --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 


     <!-- Handles requests into the application --> 
    <servlet> 
     <servlet-name>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <!-- No explicit configuration file reference here: everything is configured in the root container for simplicity --> 
      <param-name>contextConfigLocation</param-name> 
      <param-value></param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>appServlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 
+0

我didn't发布它,所以我必须问这个问题。您是否在控制器中设置了@RequestParam? – Fixus 2012-03-24 12:04:17

+0

我注意到,在“绿屋”里有一个叫App Controller的类,当应用程序启动时会调用它。 我认为那是负责扫描控制器的人。 但是,当我使用这个类,它不是从我的演示调用。 你可以看我的演示:http://www.zshare.net/download/994635177102c4ff/ – Echo 2012-03-24 14:49:19

+0

我的主要问题是如何扫描/配置控制器使用基于Java的配置! – Echo 2012-03-24 16:12:24

回答

1

我所面临的问题已经得到解决。 我已经解决了通过扫描 控制器的ISSE “@ComponentScan(” my.package.containing.controllers.to.scan “)”

您可以在这里看到演示:http://refile.net/f/?o8v