0

spring + zk @autowired problem

asked 2013-07-15 08:37:41 +0800

demizon gravatar image demizon
169 1 6

updated 2013-07-19 06:27:10 +0800

Hi, I m trying to implement zk to existing spring app.

This is example of class from spring, which i want to use in zk modelview:

@Service
@Transactional
public class JobOrderControllerImpl{

     //@Resource(name="jobOrderDao")
     @Autowired
     JobOrderDao jobOrderDao;

     @Autowired
     HarnessbaseDao harnessbaseDao;

     @Autowired
     HarnesstypeDao harnesstypeDao;

public Harnesstype findHarnesstype(int id){
                DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Harnesstype.class);
                           detachedCriteria.add(Restrictions.eq("id", id));
        return harnesstypeDao.getHarnesstype(detachedCriteria);
            }
}

in spring I am using it like this:

@Controller
@RequestMapping("/getData")
public class JobOrderGetData {
    //private ApplicationContext context;
    private Logger log = Logger.getLogger(JobOrderGetData.class);

    @Autowired    
    JobOrderControllerImpl jobOrderControler;
 @RequestMapping(value = "getHarnessType")
    @ResponseBody
    public Harnesstype getHarnessType( @RequestParam("id") int id) {
        Harnesstype ht = jobOrderControler.findHarnesstype(id);    
        return ht;
    }
}

but when i try to use it in my zk modelview like this:

public class GeneratorViewModel {   
   @Autowired
    private JobOrderControllerImpl jobOrderController;
@Command   
    public void getCount() {
        harnesstype = jobOrderController.findHarnesstype(2);        
        }}

I am getting following error:

SEVERE: java.lang.NullPointerException at com.leoni.emo.general.modelViews.GeneratorViewModel.getCount(GeneratorViewModel.java:113) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.zkoss.bind.impl.ParamCall.call(ParamCall.java:111) at org.zkoss.bind.impl.BinderImpl.doExecute(BinderImpl.java:1736) at org.zkoss.bind.impl.BinderImpl.doCommand(BinderImpl.java:1497) at org.zkoss.bind.impl.BinderImpl.access$1500(BinderImpl.java:106) at org.zkoss.bind.impl.BinderImpl$CommandEventListener.onEvent0(BinderImpl.java:1373) at org.zkoss.bind.impl.BinderImpl$CommandEventListener.onEvent(BinderImpl.java:1325) at org.zkoss.zk.ui.AbstractComponent.onEvent(AbstractComponent.java:2742) at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2713) at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2654) at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:136) at org.zkoss.zk.ui.impl.UiEngineImpl.processEvent(UiEngineImpl.java:1717) at org.zkoss.zk.ui.impl.UiEngineImpl.process(UiEngineImpl.java:1502) at org.zkoss.zk.ui.impl.UiEngineImpl.execUpdate(UiEngineImpl.java:1212) at org.zkoss.zk.au.http.DHtmlUpdateServlet.process(DHtmlUpdateServlet.java:600) at org.zkoss.zk.au.http.DHtmlUpdateServlet.doGet(DHtmlUpdateServlet.java:482) at org.zkoss.zk.au.http.DHtmlUpdateServlet.doPost(DHtmlUpdateServlet.java:491) at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1008) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907) at java.lang.Thread.run(Thread.java:619) 15.7.2013 9:32:15 org.zkoss.bind.impl.BinderImpl$CommandEventListener onEvent:1327

I guess I am doing something wrong with autowired variable... Thank you for your help.

my zul file:

<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>

<groupbox title="Tlac etikiet" closable="false" apply="org.zkoss.bind.BindComposer"
          viewModel="@id('vm') @init('com.leoni.emo.general.modelViews.GeneratorViewModel')">

    <grid width="700px">
        <rows>

            <row>
                Zadajte typ kablovky:
                <textbox value="@bind(vm.harnesstypeAliasName)" onOK="@command('getCount')"/>
            </row>

            <row visible="@load(vm.showStep2)">
                <label value="Pocet kabloviek" width="150px"/>
                <hlayout>
                    <combobox width="150px" visible="@load(vm.showStep2)" model="@load(vm.numberList)"
                              selectedItem="@bind(vm.selectedNumber)" readonly="true" onOK="@command('generateUniqueJobOrder')">
                        <template name="model">
                            <comboitem label="@load(each)"/>
                        </template>

                    </combobox>
                </hlayout>

            </row>
        </rows>

    </grid>

    <button mold="trendy" visible="@load(vm.showStep1)" label="OK" onClick="@command('getCount')"/>
    <button mold="trendy" visible="@load(vm.showStep2)" label="Generova┼ą sprievodku"
            onClick="@command('generateUniqueJobOrder')"/>
    <separator bar="true"/>


    <listbox height="590px" model="@load(vm.jobOrderList)" emptyMessage="No joborders">
        <auxhead>
            <auxheader colspan="4" class="topic">Joborder List</auxheader>
        </auxhead>
        <listhead>
            <listheader width="95%" align="center" label="Job order ID"/>
            <listheader width="95%" align="center" label="Order number"/>
            <listheader width="95%" align="center" label="Status"/>
            <listheader width="95%" align="center" label="Created date"/>
            <listheader width="95%" align="center" label="Number of harnesses"/>
            <listheader width="95%" align="center" label="Print"/>
        </listhead>
        <template name="model">
            <listitem>
                <!--listcell label="@load(each.id)"/-->
                <listcell label="@load(each.harnesstypeid)"/>
                <listcell label="@load(each.ordernumber)"/>
                <listcell label="@load(each.status)"/>
                <listcell label="@load(each.numberofharnesses)"/>
                <listcell label="@load(each.createddate)"/>
                <listcell>
                    <button label="Print" mold="trendy" onClick="@command('printJobOrder', jobOrder=each)"/>

                </listcell>
            </listitem>
        </template>
    </listbox>


</groupbox>
delete flag offensive retag edit

Comments

Hi, can you provide your zul file? and how do you call getCount command in your zul file?

jimmyshiau ( 2013-07-17 11:39:53 +0800 )edit

Ok, i edited my question and added zul file but i doubt its matter of zul file/zk at all, i think its problem with spring context file (configuration).

demizon ( 2013-07-19 06:30:06 +0800 )edit

3 Answers

Sort by ┬╗ oldest newest most voted
1

answered 2014-03-17 15:23:29 +0800

rickcr gravatar image rickcr
704 7

In ZK view models instead of @Autowired or @Resource use:

@WireVariable

     public class GeneratorViewModel {   
          @WireVariable
          private JobOrderControllerImpl jobOrderController;
link publish delete flag offensive edit
0

answered 2014-03-17 09:36:19 +0800

chillworld gravatar image chillworld flag of Belgium
5337 4 9
https://github.com/chillw...

updated 2014-03-17 16:40:34 +0800

This is cause you don't have springs context.

Create a AbstractVM and extend your VM who need spring context.

Code of the Absract class : (for MVVM)

package your.package;

import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.zkoss.zk.ui.Executions;

public abstract class AbstractVM {

    private WebApplicationContext applicationContext;

    public AbstractVM() {
        this.autowire();
    }

    private void autowire() {
        this.getApplicationContext().getAutowireCapableBeanFactory()
                .autowireBean(this);
    }

    public ApplicationContext getApplicationContext() {
        if (applicationContext == null) {
            applicationContext = WebApplicationContextUtils
                    .getRequiredWebApplicationContext(getServletContext());
        }
        return applicationContext;
    }

    public ServletContext getServletContext() {
        return Executions.getCurrent().getDesktop().getWebApp()
                .getServletContext();
    }

    public void setApplicationContext(WebApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

}
link publish delete flag offensive edit

Comments

the above is not necessary. See my app here https://github.com/rickcr/mybatis-spring-zk

rickcr ( 2014-03-17 17:11:25 +0800 )edit

Your example running has a 503, and your code doesn't say's the import for @WireResource or other objects. Strange that your github only comes up when you search that annotation in google....

chillworld ( 2014-03-18 07:39:10 +0800 )edit

Not sure what you what you mean chllworld. Not getting a 503 here. Maybe github was down for a bit? The source code there and the notes show me using @WireVariable as I mention above as well.

rickcr ( 2014-03-18 21:04:59 +0800 )edit

I mean "See it running on the RedHat OpenShift cloud at http://mybatisspringzk-simbo1905.rhcloud.com/" doesn't work. and in your class EmployeeVm you use @WireResource (oke that is not in source code but in your explication).

chillworld ( 2014-03-18 21:47:38 +0800 )edit

In the github itself you are using it correct and use a delegater in combination with the @WireVariable witch is good and also a working solution. (maybe time to update your explication cause I didn't go further to the source code the first time) Greetz chill.

chillworld ( 2014-03-18 21:53:34 +0800 )edit
0

answered 2016-08-12 01:33:42 +0800

Breezewind gravatar image Breezewind
1

user @WireVariable instead of autowired

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2013-07-15 08:37:41 +0800

Seen: 111 times

Last updated: Aug 12 '16

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More