No bean named 'smController' is defined



访问路径是:http://localhost:8080/sm/department/list.do
package com.imooc.sm.global;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class DispatcherServlet extends GenericServlet {
private ApplicationContext context;
public void init()throws ServletException{
super.init();
context=new ClassPathXmlApplicationContext("spring.xml");
}
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
HttpServletRequest request=(HttpServletRequest)req;
HttpServletResponse response=(HttpServletResponse)res;
/*
*
*/
String path=(request).getServletPath().substring(1);
String beanName=null;
String methodName=null;
int index =path.indexOf('/');
if (index!=-1){
beanName=path.substring(0,index)+"Controller";
methodName=path.substring(index+1,path.indexOf(".do"));
}else {
beanName="selfController";
methodName=path.substring(index+1,path.indexOf(".do"));
}
Object obj=context.getBean(beanName);
try {
Method method=obj.getClass().getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
method.invoke(obj,request,response);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}package com.imooc.sm.controller;
import com.imooc.sm.entity.Department;
import com.imooc.sm.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@Controller("departmentController")
public class DepartmentController {
@Autowired
private DepartmentService departmentService;
// /department/list.do /department_list.jsp
public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Department> list=departmentService.getAll();
request.setAttribute("LIST",list);
request.getRequestDispatcher("../department_list.jsp").forward(request,response);
}
}<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--Spring 整合Mybatis--> <!--数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/sm?useUnicode=true&charaterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <!--session工厂--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="typeAliasesPackage" value="com.imooc.sm.entity"></property> </bean> <!--持久化对象--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.imooc.sm.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!--声明式事务--> <!--事务管理器--> <bean id="transcationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--通知--> <tx:advice id="txAdvice" transaction-manager="transcationManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="fine*" read-only="true"/> <tx:method name="search*" read-only="true"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!--植入--> <aop:config> <aop:pointcut id="txPointcut" expression="execution(* com.imooc.sm.service.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> <!--全局扫描--> <context:component-scan base-package="com.imooc.sm"/> <aop:aspectj-autoproxy/> </beans>
35
收起
正在回答 回答被采纳积分+1
4回答
4. SSM到Spring Boot入门与综合实战
- 参与学习 人
- 提交作业 323 份
- 解答问题 8263 个
本阶段将带你学习主流框架SSM,以及SpringBoot ,打通成为Java工程师的最后一公里!
了解课程




恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星