2016-11-10 53 views
0

我目前對Spring很陌生,我正在嘗試學習它的純java配置元素。weblogic上的Spring Java配置

我能夠與下面的類Tomcat上運行我的Spring應用程序:

配置類:

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package com.inka.spring.test.maven.configuration; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.web.servlet.ViewResolver; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 
import org.springframework.web.servlet.view.InternalResourceViewResolver; 
import org.springframework.web.servlet.view.JstlView; 

/** 
* 
* @author User 
*/ 
@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages = "com.inka.spring.test.maven") 
public class HelloWorldConfiguration extends WebMvcConfigurerAdapter { 
    @Bean 
    public ViewResolver viewResolver() { 
     InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 
     viewResolver.setViewClass(JstlView.class); 
     viewResolver.setPrefix("/WEB-INF/views/"); 
     viewResolver.setSuffix(".jsp"); 

     return viewResolver; 
    } 
} 

初始化器類:

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package com.inka.spring.test.maven.configuration; 

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 

/** 
* 
* @author User 
*/ 
public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

    @Override 
    protected Class<?>[] getRootConfigClasses() { 
     return new Class[] {HelloWorldConfiguration.class}; 
    } 

    @Override 
    protected Class<?>[] getServletConfigClasses() { 
     return null; 
    } 

    @Override 
    protected String[] getServletMappings() { 
     return new String[] {"/"}; 
    } 

} 

控制器類:

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package com.inka.spring.test.maven.controllers; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

/** 
* 
* @author User 
*/ 
@Controller 
@RequestMapping("/") 
public class HelloWorldController { 

    @RequestMapping(method = RequestMethod.GET) 
    public String sayHello(ModelMap model) { 
     model.addAttribute("greeting", "Hello World from Spring 4 MVC"); 
     return "welcome"; 
    } 

    @RequestMapping(value = "/helloagain", method = RequestMethod.GET) 
    public String sayHelloAgain(ModelMap model) { 
     model.addAttribute("greeting", "Hello World Again, from Spring 4 MVC"); 
     return "welcome"; 
    } 
} 

這將調用一個.jsp頁面,根據我輸入的路徑打印相應的消息,如上面在代碼中所述。

但是,當我使用weblogic進行此嘗試時,它不起作用。我得到的只有403和404錯誤。

我會堅持使用Tomcat,但我們在我們的組織使用weblogic,並且我被指示構建我的應用程序以使用weblogic。

請問,是否有一些額外的配置,我應該在weblogic上使用?

+0

也許你寫錯了URL;你檢查了所有日誌嗎?春天上下文加載是否正確?你使用了正確的Web應用程序上下文嗎? –

+0

該URL是正確的:「localhost:7001/app-name /」。日誌顯示沒有活動,就好像應用程序只是一個普通的空web應用程序一樣。 Spring java配置沒有使用web.xml –

回答

1

好的,我終於解決了這個問題。結果發現,在你的初始化類中,無論你擴展了哪個類,如果你打算在weblogic上部署,你必須總是實現WebApplicationInitializer類。你不必爲Tomcat(不知道JBoss和其他),但對於weblogic,你必須實現這個類。

因此,改變這種後:

public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

要這樣:

public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer implements WebApplicationInitializer { 

一切正常!

欲瞭解更多信息,請訪問spring guide