想整合struts2.2.1与spring3.1.0,出现了一点麻烦,请各位给点建议,谢谢关注
 <bean id="loginDao" class="com.test.dao.login.impl.LoginDaoImpl"/>

<bean id="loginService" class = "com.test.service.impl.LoginServiceImpl"  scope="prototype">
   <property name="loginDao">
        <ref bean = "loginDao"/>
   </property>
 </bean>

 <bean id="loginActionBean" class="com.test.action.LoginAction" scope="prototype">
       <property name="loginService">
          <ref bean = "loginService"/>
       </property>
</bean>

 上面是applicationContext.xml的配置。

 

package com.test.action;

import com.test.base.BaseAction;
import com.test.service.LoginService;

public class LoginAction extends BaseAction{
    
    private String userName = null;
    private String password = null;
    private LoginService loginService;
    
    public LoginService getLoginService() {
        return loginService;
    }
    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    
    public String execute() throws Exception {
        System.out.println("=========用户民:"+getUserName());
        System.out.println("=========密  码:"+getPassword());
        return loginService.login(getUserName(),getPassword());
    }
    
}

 上面是action的代码

 

public interface LoginDao {
     public String login(String userName, String password);
}

上面是loginDao接口。

 

 

public class LoginDaoImpl extends BaseAction implements LoginDao{

    public String login(String userName, String password) {
        if(isInvalid(userName)||isInvalid(password)){
            return LOGIN;
        }else if(userName.equals("Tom") && password.equals("123456")){
              return SUCCESS;  
          }else{
              return LOGIN;
          }
    }
}

上面是logiDao的实现类LoginDaoImpl。

 

public interface LoginService{
    
    public abstract String login(String userName, String password);
}

上面是loginService接口。

 

public class LoginServiceImpl implements LoginService{
    LoginDao dao;
    
    public LoginServiceImpl(LoginDao dao){
        this.dao = dao;
    }
    public String login(String userName, String password){
        return this.dao.login(userName, password);
    }
}

 上面是loginService的实现类LoginServiceImpl。

 

基于上述代码运行后,会报出以下异常:

 

type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

org.apache.jasper.JasperException: java.lang.NullPointerException
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)


root cause 

java.lang.NullPointerException
    org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:792)
    org.apache.struts2.components.UIBean.end(UIBean.java:510)
    org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
    org.apache.jsp.login_jsp._jspx_meth_s_005ftextfield_005f0(login_jsp.java:185)
    org.apache.jsp.login_jsp._jspx_meth_s_005fform_005f0(login_jsp.java:141)
    org.apache.jsp.login_jsp._jspService(login_jsp.java:78)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)

 

jdk控制台的打印信息如下:

 

2011-4-17 11:48:21 com.opensymphony.xwork2.util.logging.commons.CommonsLogger er
ror
严重: Could not open template
org.springframework.beans.factory.BeanCreationException: Error creating bean wit
h name 'loginActionBean' defined in file [D:\workspace\test\WebContent\WEB-IN
F\classes\applicationContext.xml]: Cannot resolve reference to bean 'loginServic
e' while setting bean property 'loginService'; nested exception is org.springfra
mework.beans.factory.BeanCreationException: Error creating bean with name 'login
Service' defined in file [D:\workspace\test\WebContent\WEB-INF\classes\applic
ationContext.xml]: Instantiation of bean failed; nested exception is org.springf
ramework.beans.BeanInstantiationException: Could not instantiate bean class [com
.test.service.impl.LoginServiceImpl]: No default constructor found; nested ex
ception is java.lang.NoSuchMethodException: com.test.service.impl.LoginServic
eImpl.<init>()
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver
.resolveReference(BeanDefinitionValueResolver.java:328)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver
.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1325)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.createBean(AbstractAutowireCapableBeanFactory.java:456)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBe
an(AbstractBeanFactory.java:314)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean
(AbstractBeanFactory.java:194)
        at org.springframework.context.support.AbstractApplicationContext.getBea
n(AbstractApplicationContext.java:1080)
        at com.opensymphony.xwork2.spring.SpringObjectFactory.getClassInstance(S
pringObjectFactory.java:221)
        at org.apache.struts2.components.ServletUrlRenderer.renderFormUrl(Servle
tUrlRenderer.java:161)
        at org.apache.struts2.components.Form.populateComponentHtmlId(Form.java:
220)
        at org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:784)
        at org.apache.struts2.components.ClosingUIBean.start(ClosingUIBean.java:
57)
        at org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(Component
TagSupport.java:53)
        at org.apache.jsp.login_jsp._jspx_meth_s_005fform_005f0(login_jsp.java:1
30)
        at org.apache.jsp.login_jsp._jspService(login_jsp.java:78)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper
.java:374)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:3
42)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:206)
        at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
.doFilter(StrutsPrepareAndExecuteFilter.java:88)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:128)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:293)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:849)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ss(Http11Protocol.java:583)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:45
4)
        at java.lang.Thread.run(Thread.java:619)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creati
ng bean with name 'loginService' defined in file [D:\workspace\test\WebConten
t\WEB-INF\classes\applicationContext.xml]: Instantiation of bean failed; nested
exception is org.springframework.beans.BeanInstantiationException: Could not ins
tantiate bean class [com.test.service.impl.LoginServiceImpl]: No default cons
tructor found; nested exception is java.lang.NoSuchMethodException: com.test.
service.impl.LoginServiceImpl.<init>()
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.createBean(AbstractAutowireCapableBeanFactory.java:456)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBe
an(AbstractBeanFactory.java:314)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean
(AbstractBeanFactory.java:194)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver
.resolveReference(BeanDefinitionValueResolver.java:322)
        ... 37 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not insta
ntiate bean class [com.test.service.impl.LoginServiceImpl]: No default constr
uctor found; nested exception is java.lang.NoSuchMethodException: com.test.se
rvice.impl.LoginServiceImpl.<init>()
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy
.instantiate(SimpleInstantiationStrategy.java:70)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.instantiateBean(AbstractAutowireCapableBeanFactory.java:958)
        ... 43 more
Caused by: java.lang.NoSuchMethodException: com.test.service.impl.LoginServic
eImpl.<init>()
        at java.lang.Class.getConstructor0(Class.java:2706)
        at java.lang.Class.getDeclaredConstructor(Class.java:1985)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy
.instantiate(SimpleInstantiationStrategy.java:65)
        ... 44 more
org.springframework.beans.factory.BeanCreationException: Error creating bean wit
h name 'loginActionBean' defined in file [D:\workspace\test\WebContent\WEB-IN
F\classes\applicationContext.xml]: Cannot resolve reference to bean 'loginServic
e' while setting bean property 'loginService'; nested exception is org.springfra
mework.beans.factory.BeanCreationException: Error creating bean with name 'login
Service' defined in file [D:\workspace\test\WebContent\WEB-INF\classes\applic
ationContext.xml]: Instantiation of bean failed; nested exception is org.springf
ramework.beans.BeanInstantiationException: Could not instantiate bean class [com
.test.service.impl.LoginServiceImpl]: No default constructor found; nested ex
ception is java.lang.NoSuchMethodException: com.test.service.impl.LoginServic
eImpl.<init>()
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver
.resolveReference(BeanDefinitionValueResolver.java:328)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver
.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1325)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.createBean(AbstractAutowireCapableBeanFactory.java:456)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBe
an(AbstractBeanFactory.java:314)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean
(AbstractBeanFactory.java:194)
        at org.springframework.context.support.AbstractApplicationContext.getBea
n(AbstractApplicationContext.java:1080)
        at com.opensymphony.xwork2.spring.SpringObjectFactory.getClassInstance(S
pringObjectFactory.java:221)
        at org.apache.struts2.components.ServletUrlRenderer.renderFormUrl(Servle
tUrlRenderer.java:161)
        at org.apache.struts2.components.Form.populateComponentHtmlId(Form.java:
220)
        at org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:784)
        at org.apache.struts2.components.ClosingUIBean.start(ClosingUIBean.java:
57)
        at org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(Component
TagSupport.java:53)
        at org.apache.jsp.login_jsp._jspx_meth_s_005fform_005f0(login_jsp.java:1
30)
        at org.apache.jsp.login_jsp._jspService(login_jsp.java:78)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper
.java:374)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:3
42)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:206)
        at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
.doFilter(StrutsPrepareAndExecuteFilter.java:88)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:128)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:293)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:849)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ss(Http11Protocol.java:583)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:45
4)
        at java.lang.Thread.run(Thread.java:619)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creati
ng bean with name 'loginService' defined in file [D:\workspace\test\WebConten
t\WEB-INF\classes\applicationContext.xml]: Instantiation of bean failed; nested
exception is org.springframework.beans.BeanInstantiationException: Could not ins
tantiate bean class [com.test.service.impl.LoginServiceImpl]: No default cons
tructor found; nested exception is java.lang.NoSuchMethodException: com.test.
service.impl.LoginServiceImpl.<init>()
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.createBean(AbstractAutowireCapableBeanFactory.java:456)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBe
an(AbstractBeanFactory.java:314)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean
(AbstractBeanFactory.java:194)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver
.resolveReference(BeanDefinitionValueResolver.java:322)
        ... 37 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not insta
ntiate bean class [com.test.service.impl.LoginServiceImpl]: No default constr
uctor found; nested exception is java.lang.NoSuchMethodException: com.test.se
rvice.impl.LoginServiceImpl.<init>()
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy
.instantiate(SimpleInstantiationStrategy.java:70)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.instantiateBean(AbstractAutowireCapableBeanFactory.java:958)
        ... 43 more
Caused by: java.lang.NoSuchMethodException: com.test.service.impl.LoginServic
eImpl.<init>()
        at java.lang.Class.getConstructor0(Class.java:2706)
        at java.lang.Class.getDeclaredConstructor(Class.java:1985)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy
.instantiate(SimpleInstantiationStrategy.java:65)
        ... 44 more

 

请各位高手看看,这是什么原因哈,struts2配置没有问题的,因为已经测试过了,struts.xml的配置如下:

<struts>

    <package name="login"  extends="struts-default">
            <action name="login" class= "loginActionBean">
            <result name="success">/success.jsp</result>
            <result name="login">/error.jsp</result>
        </action>
        <!-- Add actions here -->
    </package>
    
</struts>

 

5个回答

分出dao层的目的是方便跟数据库打交道的,不是用来验证的。验证可以直接在action里面写。

Could not instantiate bean class [com.test.service.impl.LoginServiceImpl]: No default constructor found.

为什么LoginDaoImpl 要 extends baseAction

LoginServiceImpl 为什么要弄个带参构造方法??

在service层 去掉构造方法,添加dao的 get() ,set()方法

还有那个dao 层实现写的也有问题 怎么extends baseaction了 要么继承hibernatedaosupport 要么直接注入hibernatetemplate

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐