mar*_*n72 6 java spring-mvc url-mapping
试图整合hibernate和spring,我遇到了这个错误
严重:上下文初始化失败
org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping'的bean时出错:bean的初始化失败; 嵌套异常是java.lang.IllegalStateException:无法将处理程序'org.me.spring.hib.school.web.SchoolController#0' 映射到URL路径[/allschools]:已经有类型[classorg.me.spring.hib.school.web.SchoolController]映射的处理程序.
我的控制器看起来像
package org.me.spring.hib.school.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.me.spring.hib.school.dao.SchoolDAO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class SchoolController {
private SchoolDAO schoolDao;
public SchoolDAO getSchoolDao() {
return schoolDao;
}
public void setSchoolDao(SchoolDAO schoolDao) {
this.schoolDao = schoolDao;
}
@RequestMapping("/allschools")
public ModelAndView showAllSchools(HttpServletRequest request,HttpServletResponse response) throws Exception{
if(this.schoolDao ==null){
System.out.println("this.schoolDao is null");
}
ModelMap modelMap = new ModelMap();
modelMap.addAttribute("schoolsList", this.schoolDao.getAllSchools());
return new ModelAndView("allschools", modelMap);
}
}
Run Code Online (Sandbox Code Playgroud)
我在应用程序上下文文件中注入了dao实现
<context:component-scan base-package="org.me.spring.hib.school.web" />
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" >
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="annotatedClasses">
<list>
<value>org.me.spring.hib.school.domain.School</value>
<value>org.me.spring.hib.school.domain.Teacher</value>
<value>org.me.spring.hib.school.domain.Student</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="schoolDao" class="org.me.spring.hib.school.dao.SchoolDAOImpl">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
<bean class="org.me.spring.hib.school.web.SchoolController" >
<property name="schoolDao" ref="schoolDao" />
</bean>
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚为什么 SchoolController#0要映射到网址.
ska*_*man 22
发生这种情况是因为你有两者
<context:component-scan base-package="org.me.spring.hib.school.web" />
Run Code Online (Sandbox Code Playgroud)
和
<bean class="org.me.spring.hib.school.web.SchoolController" >
Run Code Online (Sandbox Code Playgroud)
第一行将自动发现并SchoolController为您注册.通过明确声明另一个,您将获得重复,并且url-mapping将发生冲突.
您需要删除<context:component-scan>或删除控制器和DAO的显式bean定义.如果您执行后者,那么您将需要使用自动装配来注入其依赖项.
| 归档时间: |
|
| 查看次数: |
11932 次 |
| 最近记录: |