java - spring and vaadin integration -
i'm trying implement application several ui
s - vaadin, jsp , etc.
it working simple jsp
decided use vaadin ui.
i've created vaadin servlet(and spring servlet left too). web.xml
looks this
<?xml version="1.0"?> <web-app 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_2_5.xsd" version="2.5"> <context-param> <param-name>contextconfiglocation</param-name> <param-value> classpath:/pmc-web-context.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <servlet> <servlet-name>another-pmc-servlet</servlet-name> <servlet-class>com.vaadin.server.vaadinservlet</servlet-class> <init-param> <param-name>ui</param-name> <param-value>com.xxxx.app.pmc.vaadin.pmcui</param-value> </init-param> <init-param> <param-name>contextconfiglocation</param-name> <param-value></param-value> </init-param> </servlet> <servlet> <servlet-name>pmc-servlet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value></param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>another-pmc-servlet</servlet-name> <url-pattern>/vaadin/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>pmc-servlet</servlet-name> <url-pattern>/jsp/*</url-pattern> </servlet-mapping> <servlet> <servlet-name>jsp</servlet-name> <servlet-class>org.apache.jasper.servlet.jspservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>jsp</servlet-name> <url-pattern>/web-inf/jsp/*</url-pattern> </servlet-mapping> </web-app>
i've created vaadin table
component , adjusted needs. used autowiring service.
package com.xxxx.app.pmc.vaadin.components; import com.xxxx.app.pmc.model.project; import com.xxxx.app.pmc.service.project.projectservice; import com.vaadin.data.util.indexedcontainer; import com.vaadin.ui.table; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.component; import java.util.list; @component("projecttable") public class projecttable extends table { private static final string caption = "projects"; private static final string[] headers = { "project name", "project owner", "id" }; @autowired private projectservice projectservice; public table createtable() { this.setcontainerdatasource(projectdatasource()); this.setvisiblecolumns(headers); this.setselectable(true); this.setimmediate(true); return this; } public indexedcontainer projectdatasource() { indexedcontainer indexedcontainer = new indexedcontainer(); for(string header: headers) { indexedcontainer.addcontainerproperty(header, string.class, ""); } list<project> projects = projectservice.findallprojects(); for(int = 0; < projects.size(); i++) { object id = indexedcontainer.additem(); project item = projects.get(i); indexedcontainer.getcontainerproperty(id, headers[0]).setvalue(item.getprojectname()); indexedcontainer.getcontainerproperty(id, headers[1]).setvalue(item.getprojectowner()); indexedcontainer.getcontainerproperty(id, headers[1]).setvalue(item.getprojectid()); } return indexedcontainer; } }
projectservice
spring bean too.
@service("projectservice") public class projectservice { @autowired private projectrepository projectrepository; public void insertproject(project project) { projectrepository.store(project); } public list<project> findallprojects() { return projectrepository.getallitems(); } public project getproject(string id) { return projectrepository.get(id); } }
projectrepository
spring bean. uses sqlsessiontemplate
bean mybatis.
@repository("projectrepository") public class projectrepository implements irepository<project> { private static final string store_project = "project.insertproject"; private static final string get_project_by_id = "project.getprojectbyid"; private static final string get_project_list = "project.getprojectlist"; @autowired private sqlsessiontemplate sqlsessiontemplate; @override public void store(project object) { sqlsessiontemplate.insert(store_project, object); } @override public project get(string id) { return sqlsessiontemplate.selectone(get_project_by_id, id); } @override public list<project> getallitems() { return sqlsessiontemplate.selectlist(get_project_list); } }
when wrote application using spring controller
(using jsp) - working fine. when added vaadin - jsp
stopped working , vaadin application throws nullpointerexception
projectservice
, projectrepository
... beans use.
what problem? context xml
context files simple.
<import resource="classpath:com/xxxx/app/pmc/pmc-service-context.xml"/> <context:component-scan base-package="com.xxx.app.pmc"/>
and pmc-web-context.xml
has lines too.
<mvc:annotation-driven/> <context:annotation-config/>
it working fine jsp think problem not spring declarations integration of spring vaadin of mine.
how resolve it? when example created projecttable
object manually - throws nullpointerexception
projectservice
. when create projectservice
manually - throws nullpointerexception
projectrepository
, on. seems autowiring doesn't work.
p.s forgot add ui code
package com.xxxx.app.pmc.vaadin; import com.xxxx.app.pmc.vaadin.components.projecttable; import com.vaadin.annotations.title; import com.vaadin.server.vaadinrequest; import com.vaadin.ui.ui; import com.vaadin.ui.verticallayout; import org.springframework.stereotype.component; @title("pmc") @component("pmcvaadin") public class pmcui extends ui { @override protected void init(vaadinrequest request) { verticallayout mainlayout = new verticallayout(); projecttable projecttable = new projecttable(); mainlayout.addcomponent(projecttable.createtable()); mainlayout.setsizefull(); setcontent(mainlayout); } }
like suspected spring beans created, in application , spring's servlet contexts. vaadin servlet has no access of those.
for detailed (manual) solution code either check vaadin's wiki or search proper vaadin's add ons job (personally recommend springvaadinintegration).
the typical idea programmatically pass vaadin servlet 1 of spring's utility classes (eg.: webapplicationcontextutils) , retrieve application context (loaded contextloaderlistener). if need servlet context, code examples have seen, same above, additionally manually read context (eg.: using xmlwebapplicationcontext) , set application context it's parent.
Comments
Post a Comment