2012-11-22 45 views
0

的DispatcherServlet我想我的應用程序的核心服務,從我的DispatcherServlet並有註解@Controller類分開。我在這裏閱讀了很多關於SO的主題,但沒有找到適合我的案例的任何示例,只是解釋了有關根子級別,組件掃描軟件包等等。網頁是不開放/碎使用Spring的ApplicationContext時,並分別

由於所有的建議都告訴我,在appicationContext.xml中,我應該掃描除@Controller類之外的所有組件,並在我的DispatcherServlet-servlet.xml中掃描僅用於@Controller類。我就像在這個@Service are constructed twice的例子中那樣做了。

所以我的應用程序成功建立,但是當我去到它的網頁,在Chrome它告訴我,This link appears to be broken.這意味着頁面存在,否則的Tomcat會告訴我,這The requested resource (/myapp) is not available.

一定有什麼錯我的配置。可能是什麼問題? 這是我applicationContext.xml

<beans <!-- omitting namespaces for readability --> > 

    <!-- Scans within the base package of the application for @Components to configure as beans --> 
    <!-- @Controller, @Service, @Configuration, etc. --> 
    <context:component-scan base-package="com.myapp"> 
     <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/> 
    </context:component-scan> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <mvc:annotation-driven /> 
    <mvc:resources mapping="/static/**" location="/" /> 
    <mvc:default-servlet-handler /> 
    <jpa:repositories base-package="com.myapp.repo" /> 
</beans> 

接下來是我的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app <!-- omitting namespaces for readability -->> 

    <display-name>My application</display-name> 

    <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     classpath:applicationContext.xml 
     classpath:spring-security.xml 
     </param-value> 
    </context-param> 

    <listener> 
    <listener-class> 
     org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
    </listener> 

    <listener> 
    <listener-class> 
     org.springframework.web.context.request.RequestContextListener 
    </listener-class> 
    </listener> 

    <servlet> 
    <servlet-name>DispatcherServlet</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 

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

    <filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 

    <filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
    </filter-mapping> 

    <!-- Ensure UTF-8 encoded pages so that certain characters are displayed and submitted correctly --> 
    <filter> 
     <filter-name>encodingFilter</filter-name> 
     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
     <init-param> 
      <param-name>encoding</param-name> 
      <param-value>UTF-8</param-value> 
     </init-param> 
     <init-param> 
      <param-name>forceEncoding</param-name> 
      <param-value>true</param-value> 
     </init-param> 
    </filter> 

    <filter-mapping> 
     <filter-name>encodingFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

    <!-- Enables support for DELETE and PUT request methods with web browser clients --> 
    <filter> 
     <filter-name>hiddenHttpMethodFilter</filter-name> 
     <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 
    </filter> 

    <filter-mapping> 
     <filter-name>hiddenHttpMethodFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

    <error-page> 
     <error-code>404</error-code> 
     <location>/WEB-INF/view/error/404.jsp</location> 
    </error-page> 
</web-app> 

下面的DispatcherServlet-servlet.xml中

<beans <!-- omitting namespaces for readability -->> 
    <context:component-scan base-package="com.myapp.mvc" use-default-filters="false"> 
     <context:include-filter type="annotation" 
      expression="org.springframework.stereotype.Controller" /> 
    </context:component-scan> 
</beans> 

這裏是我的配置類

@Configuration 
@ImportResource("classpath:applicationContext.xml") 
@PropertySource("classpath:images.properties") 
public class AppConfig implements InitializingBean { 

    @Inject 
    ServletContext context; 

    // Resolve logical view names to .jsp resources in the /WEB-INF/views directory 
    @Bean 
    ViewResolver viewResolver() { 
     InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
     resolver.setPrefix("WEB-INF/view/"); 
     resolver.setSuffix(".jsp"); 
     return resolver; 
    } 

    // Configure the multipart resolver 
    @Bean 
    CommonsMultipartResolver multipartResolver() { 
     CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); 
//  multipartResolver.setMaxUploadSize(2_202_009); 
     return multipartResolver; 
    } 

    @Override 
    public void afterPropertiesSet() throws Exception { 
     File logoImgPreview = new File("resources/img/image_footer_logo_preview.jpg"); 
     BufferedImage bufImgPreview = ImageIO.read(logoImgPreview); 
     context.setAttribute("watermark_preview", bufImgPreview); 
    } 
} 

所以,最後這裏是我的日誌服務器啓動時:

http://pastie.org/private/z8zv7jaddytxsgbh7oggw

我在DispatcherServlet的定義控制器咖啡豆,但地圖的applicationContext的RequestMappingHandlers服務器日誌中看到什麼。應該是這樣嗎?我並不確定整個過程如何工作,但根據我迄今爲止所讀到的所有內容,這種背景分離必須是正確的。但是,我的配置中的錯誤在哪裏?

這裏是我的HomeController這是非常基本的

@Controller 
public class HomeController { 

    private static Logger log = LoggerFactory.getLogger("com.myapp.mvc.HomeController"); 

    @RequestMapping("/") 
    public String home() { 
     log.debug("Requesting index page"); 
     return "index"; // it returns to index.jsp 
    } 
} 

回答

3

您應該所有的控制器相關的bean定義移動到調度-servlet.xml文件,你仍然有一些在applicationContext.xml文件 -

<mvc:annotation-driven /> 
<mvc:resources mapping="/static/**" location="/" /> 
<mvc:default-servlet-handler /> 

這可能不是你所看到的問題,所有的原因,但應該把你遠一點。

+0

它的工作原理,謝謝了很多! – Skyzer

相關問題