gpt4 book ai didi

java - weblogic上的Spring Java配置

转载 作者:行者123 更新时间:2023-11-30 02:44:45 25 4
gpt4 key购买 nike

我目前是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[] {"/"};
}

}

Controller 类:

/*
* 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 上使用一些额外的配置吗?

最佳答案

好的,我终于解决了这个问题。事实证明,在你的初始化器类中,无论你扩展什么类,如果你打算在 weblogic 上部署,你必须始终实现 WebApplicationInitializer 类。对于 Tomcat,您不必这样做(不了解 JBoss 和其他),但对于 weblogic,您必须实现该类。

因此,更改此后:

public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

对此:

public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer implements WebApplicationInitializer {

一切正常!

欲了解更多信息,请访问spring guide .

关于java - weblogic上的Spring Java配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40531096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com