java - GWT 与 Spring : applicationContext is null if I change the page

标签 java javascript spring gwt gwt-rpc

我从一位离开的开发人员那里继承了一个项目,我正在尝试了解 GWT 和 Spring 框架。

导致我走上这条路的最初问题是:GWT 有一个模块,我在其中加载了所有第三方 JavaScript...这可能会导致冲突。例如,我会将图表绘图库等全部包含在一页中。 可能的解决方案: 将图表绘制库放在 iframe 中,这样就不会与其他第三方 javascript 库冲突...或者在新窗口中打开页面。

我决定使用新窗口。

所以我这样做了:

Window.Location.assign(GWT.getHostPageBaseURL()
            + "chartModule.html?gwt.codesvr=127.0.0.1:9997/");

但是,在我的新chartModule.java(GWT)中,我遇到的问题是我不再在(Spring框架)applicationContext.xml中定义bean/类:

@Autowired
ApplicationContext applicationContext;

在我更改主机页面 URL 后,applicationContext 为 null...所以我没有任何尝试 Autowiring 的 bean...

是否可以从applicationContext.xml重新加载bean?

这是我的 applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <!-- Activates various annotations to be detected in bean classes -->
    <context:annotation-config />

    <!--  This file has properties that are used by other XML files loaded via ${var name} syntax -->
    <context:property-placeholder location="/WEB-INF/classes/environment.properties" />

    <import resource="spring-security-cas.xml" />

    <!-- Scans the classpath for annotated components that will be auto-registered 
        as Spring beans. For example @Controller and @Service. Make sure to set the 
        correct base-package -->
    <context:component-scan base-package="com.javamango.sixtydegrees" />
    <import resource="mongo-config.xml" />
    <import resource="rabbitmq-context.xml" />
    <import resource="spring-mail.xml" />
</beans>

最佳答案

您不能在客户端使用 spring beans。如果您想在 gwt 中从 spring 检索一些数据,可以通过两种方式执行此操作:

1) 使用服务器端库,如 gwt-sl在 gwt servlet 中注入(inject) spring beans

@Service("greetingService")
public class GreetingServiceImpl extends RemoteServiceServlet implements GreetingService
{
    @SuppressWarnings("unused")
    private static final Logger LOGGER = LoggerFactory.getLogger(GreetingServiceImpl.class);

    @Autowired
    UserFileService userFileService;

    @Autowired
    UserService userService;
}

现在您可以 Autowiring spring beans 并通过 gwt-rpc 获取数据

2)通过jsp将数据放入隐藏的html表单字段中并从中检索数据

<input type="hidden" value="7" id="documentid"/>

String id = (InputElement) (Element) DOM.getElementById("documentid").value

关于java - GWT 与 Spring : applicationContext is null if I change the page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21519271/

相关文章:

java - 将现有的 Jersey Postgres 应用程序迁移到 Spring Boot

java - Admob 实时广告未出现在 libgdx 应用程序中 - 无法加载广告 : 0

javascript - 从内存源中播放 html5 视频

javascript - ionic : Disallow all emoticons

spring - 如何注入(inject)@products方法

java - Spring - 添加一个低优先级的多线程服务(不影响生产性能)

java - 使用带有 Java 的 Selenium WebDriver 在组中选择单选按钮

java - 如何在java或linux命令行中计算一组数字的90%分布值?

java - XSLT 如何使用数组过滤数据

javascript - 使用 javascript 检查文件夹内图像文件的数量