【发布时间】:2014-03-21 16:58:16
【问题描述】:
所以我有一个我一直在研究的 Vaadin 7 应用程序,但现在需要集成 JAAS 并最终集成 OpenAM 以进行身份验证和授权。我注意到很多人都在使用 Spring 来让它工作,所以我走了那条路。我使用了 VaadinSpringIntegration 插件,我认为它设置正确,但是当我启动我的应用程序时,我的自动装配 bean 为空。我以前从未使用过 Spring,所以很可能我忽略了一些东西。
所以这是我的 web.xml 的一部分
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Hoplite Tool</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>Hoplite</servlet-name>
<servlet-class>org.hoplite.servlet.HopliteServlet</servlet-class>
<init-param>
<param-name>UI</param-name>
<param-value>org.hoplite.dashboard.DashboardUI</param-value>
</init-param>
<init-param>
<param-name>widgetset</param-name>
<param-value>org.hoplite.dashboard.DashboardWidgetSet</param-value>
</init-param>
<init-param>
<param-name>UIProvider</param-name>
<param-value>org.hoplite.dashboard.DashboardUIProvider</param-value>
</init-param>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>BAT3</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
这是我的 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:lang="http://www.springframework.org/schema/lang"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<context:component-scan base-package="org.hoplite.dashboard" />
</beans>
我必须为 webapp 包含一些 javascript,所以我扩展了 SpringVaadinServlet
public class HopliteServlet extends SpringVaadinServlet {
/**
* Attempt to load extra javascript
*
* @throws ServletException
*/
@Override
protected void servletInitialized() throws ServletException {
super.servletInitialized();
getService().addSessionInitListener(new SessionInitListener() {
@Override
public void sessionInit(SessionInitEvent event) throws ServiceException {
event.getSession().addBootstrapListener(new BootstrapListener() {
@Override
public void modifyBootstrapPage(BootstrapPageResponse response) {
response.getDocument().head().appendElement("script").attr("type", "text/javascript").attr("src", "https://www.google.com/jsapi");
}
@Override
public void modifyBootstrapFragment(BootstrapFragmentResponse response) {
}
});
}
});
}
}
最后我的 UI 类有一个 Autowired bean(抱歉这个类很长,所以我只能提供一个 sn-p)
@Push(PushMode.MANUAL)
@Theme("dashboard")
@Title("Hoplite Tool")
@Component
@Scope("prototype")
public class DashboardUI extends UI {
@Autowired
private LoginBean loginBean;
...
//add listener to button
signin.addClickListener(new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
String name = loginBean.performLogin(username.getValue().toString(), password.getValue().toString());
buildMainView();
}
}
这里是 loginBean 类
@Component
@Scope("prototype")
public class LoginBean {
@Autowired
private HttpServletRequest request;
/**
* Logs the user in with given username and password.
*
* @param username
* @param password
* @return principal name if login is successful
* @throws ServletException if login fails
*/
public String performLogin(String username, String password) throws ServletException {
// If login fails, we throw exception. Otherwise return the principal
// name of logged in user.
request.login(username, password);
return request.getUserPrincipal().getName();
}
/**
* Logs out the current user.
*
* @throws ServletException if logout fails
*/
public void performLogout() throws ServletException {
request.logout();
}
}
我的bean类和UI类在org.hoplite.dashboard中
但是,每次我获得主 UI 时,loginBean 都是空的...有人可以帮我吗?
【问题讨论】:
-
你能从 web.xml 添加你的 servlet 配置吗?
-
我已将 servlet 配置添加到 web.xml 部分
-
web.xml 中的
com.company.hoplite...和 spring 配置中的<context:component-scan base-package="org.hoplite.dashboard" />中哪个是错字? -
对不起...出于商业目的我重命名了包...包名在代码中是一致的,所以我修改为“org.hoplite.dashboard”...抱歉造成混淆跨度>
标签: java spring ejb jaas vaadin7