2013-04-26 72 views
-1

我有春天項目三個XML文件如下錯誤在XML文件中的Spring MVC

應用程序的context.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:p="http://www.springframework.org/schema/p" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:util="http://www.springframework.org/schema/util" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema 
     /jdbc/spring-jdbc-3.0.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema 
    /tx/spring-tx-3.0.xsd 
http://www.springframework.org/schema/util http://www.springframework.org/schema 
    /util/spring-util-3.0.xsd 
http://www.springframework.org/schema/mvc http://www.springframework.org/schema 
    /mvc/spring-mvc-3.0.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema 
    /context/spring-context-3.0.xsd"> 


<context:annotation-config/> 

<bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3306/indi" /> 
    <property name="username" value="root" /> 
    <property name="password" value="" /> 

</bean> 

</beans> 

CController-servlet.xml中(這是經理級別的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:p="http://www.springframework.org/schema/p" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:util="http://www.springframework.org/schema/util" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/aop http://www.springframework.org/schema 
     /aop/spring-aop-2.5.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema 
     /tx/spring-tx-2.5.xsd 
http://www.springframework.org/schema/util http://www.springframework.org/schema 
    /util/spring-util-2.0.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema 
    /context/spring-context-2.5.xsd"> 

    <context:annotation-config/> 
    <context:component-scan base-package="project4"/> 

<bean 
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> 


<bean id="urlMapping" 
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
    <property name="mappings"> 
     <props> 
      <prop key="index.htm">indexController</prop> 


     </props> 
    </property> 
</bean> 



<bean id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
     p:prefix="/WEB-INF/jsp/" 
     p:suffix=".jsp" /> 


<bean name="frm4" class="project4.CController" > 
    <property name="userDAO" ref="myUserDAO" /> 
</bean> 


<bean name="indexController" 
    class="org.springframework.web.servlet.mvc.ParameterizableViewController" 
     p:viewName="index" /> 

</beans> 

和UUController-servlet.xml中(這是一個DAO水平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:p="http://www.springframework.org/schema/p" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:util="http://www.springframework.org/schema/util" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/aop http://www.springframework.org/schema 
    /aop/spring-aop-2.5.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema 
    /tx/spring-tx-2.5.xsd 
    http://www.springframework.org/schema/util http://www.springframework.org/schema 
    /util/spring-util-2.0.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema 
    /context/spring-context-2.5.xsd"> 



<bean id="mySessionFactory" 
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     <property name="dataSource" ref="myDataSource" /> 
     <property name="packagesToScan" value="project4"/> 

     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.hbm2ddl.auto">create</prop> 
      </props> 
     </property> 
    </bean> 

<bean id="myUserDAO" class="project4.UserDAOImpl1"> 
    <property name="sessionFactory" ref="mySessionFactory"/> 
</bean> 
</beans> 

我有兩個xml文件,一個用於管理員調用,另一個用於dao調用...我知道ref從一個xml點鏈接到另一個xml中的bean。我以爲他們會自動鏈接,但我得到以下錯誤

Error creating bean with name 
'org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping#0' 
defined in ServletContext resource [/WEB-INF/CController-servlet.xml]: Initialization 
of bean failed; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
'frm4' defined in ServletContext resource [/WEB-INF/CController-servlet.xml]: Cannot 
    resolve reference to bean 'myUserDAO' while setting bean property 'userDAO'; nested 
    exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No 
    bean  named 'myUserDAO' is defined 



Caused by: org.springframework.beans.factory.BeanCreationException: Error creating 
bean with name 'frm4' defined in ServletContext resource [/WEB-INF/CController- 
servlet.xml]: Cannot resolve reference to bean 'myUserDAO' while setting bean property 
    'userDAO'; nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 
'myUserDAO' is defined 

UserDAOImpl1是我的休眠模板類和CCController是我的春天控制器類

CCController.java

package project4; 
import project4.UserDAO1; 
import project4.User1; 
import java.util.Map; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.springframework.stereotype.Controller; 
import org.springframework.transaction.annotation.Transactional; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.servlet.mvc.multiaction.MultiActionController; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.validation.BindingResult; 


@Controller 
@RequestMapping("frm4.do") 

public class CController{ 

    private UserDAO1 userDAO; 

    public void setUserDAO(UserDAO1 userDAO) { 
     this.userDAO = userDAO; 
    } 

    @RequestMapping(params = "/add", method = RequestMethod.POST) 
    public ModelAndView add(@ModelAttribute("add") User1 user,HttpServletRequest 
      request,HttpServletResponse response) throws Exception { 
     userDAO.saveUser(user); 
     System.out.println("hai"); 
     return new ModelAndView("redirect:list.htm"); 
    } 

    @RequestMapping(params = "delete", method = RequestMethod.POST) 
    @Transactional 
    public ModelAndView delete(@ModelAttribute("delete") User1 user,HttpServletRequest 
      request,HttpServletResponse response) throws Exception { 
      userDAO.deleteUser(user); 
      return new ModelAndView("redirect:list.htm"); 
    } 

    @RequestMapping(params = "find", method = RequestMethod.POST) 
    @Transactional 
    public ModelAndView find(@ModelAttribute("find") User1 user,HttpServletRequest 
        request,HttpServletResponse response) throws Exception { 
        userDAO.findUser(user); 
         return new ModelAndView("redirect:list.htm"); 
    } 


    @RequestMapping(params = "update", method = RequestMethod.POST) 
    @Transactional 
    public ModelAndView update(@ModelAttribute("update") User1 user,HttpServletRequest 
       request,HttpServletResponse response) throws Exception { 
        userDAO.updateUser(user); 
         return new ModelAndView("redirect:list.htm"); 
    } 

    public ModelAndView list(HttpServletRequest request, 
     HttpServletResponse response) throws Exception { 


    ModelMap modelMap = new ModelMap(); 
    modelMap.addAttribute("userList", userDAO.listUser()); 
    modelMap.addAttribute("user", new User1()); 
    return new ModelAndView("userForm", modelMap); 
    } 
    } 

可以有人幫忙PLZ

編輯:

在web.xml是如下

<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value> /WEB-INF/application-context.xml</param-value>  
</context-param> 

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener- 
    class> 
</listener> 


<servlet> 
    <servlet-name>CController</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>2</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>CController</servlet-name> 
    <url-pattern>*.do</url-pattern> 
</servlet-mapping> 
+1

可以共享web.xml中也 – 2013-04-26 06:34:57

+0

增加其與後 – Ezhil 2013-04-26 06:42:08

回答

1

frm4 bean有具有參考userDAO,這是在其他的xml文件中聲明的屬性。所以請確保所有的xml文件都在應用程序上下文中加載。爲此,您需要在web.xml中添加所有xml文件。如果這不是爲你工作

試試這個:

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
    /WEB-INF/UUController-servlet.xml, 
    /WEB-INF/application-context.xml, 
    /WEB-INF/CController-servlet.xml 
    </param-value>  
</context-param> 

編輯。嘗試在application-context.xml文件中添加其他文件。使用bean導入標籤。

<beans:import resource="/WEB-INF/UUController-servlet.xml"/> 
<beans:import resource="/WEB-INF/CController-servlet.xml"/> 

並且在啓動時僅加載應用程序上下文文件。

編輯:

錯誤背後的原因「不能映射處理程序」是你在應用程序啓動掃描組件,並且還具有相同的bean在應用程序上下文中聲明。從XML文件

<context:component-scan base-package="project4"/> 

&

<bean name="frm4" class="project4.CController" > 
    <property name="userDAO" ref="myUserDAO" /> 
</bean> 

刪除bean聲明,因爲你已經掃描特定的組件。並在您的控制器類中自動導入myUserDAO bean。

@Autowire 
@Qualifier("myUserDAO") 
private UserDAOImpl1 myUserDAO; 
+0

我改變了它,但仍然顯示相同的錯誤 – Ezhil 2013-04-26 06:52:54

+0

確認xml文件路徑是否正確。檢查它們是否僅在WEB-INF下,而不是在WEB-INF下的嵌套文件夾中。 – 2013-04-26 06:55:04

+0

@ user1847395請參閱編輯答案。 – 2013-04-26 06:58:16

0

問題是UUController-servlet文件不是由應用程序上下文加載。由於它是一個數據庫級別的上下文,請將其添加到contextConfigLocation,如下所示。此外,我會推薦它重命名爲dao-context.xml

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     /WEB-INF/application-context.xml 
     /WEB-INF/UUController-servlet.xml 
    </param-value>  
</context-param> 
+0

它只是顯示了同樣的錯誤 – Ezhil 2013-04-26 06:48:29

+0

阿倫一起 - 我猜文件條目應該是逗號分隔。 – 2013-04-26 06:49:57