我从一位离开的开发人员那里继承了一个项目,我正在尝试了解 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/