No bean named 'SelfController' is defined
具体遇到的问题
No bean named ‘SelfController’ is defined
报错信息的截图
这是项目结构图
这是报错
相关课程内容截图
尝试过的解决思路和结果
spring.xml 没错
核心控制器可以正确输出类名与方法名
类名也没错
注解也没有错
粘贴全部相关代码,切记添加代码注释(请勿截图)
package com.imooc.hospital.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;
@Override
public void init() throws ServletException {
super.init();
context = new ClassPathXmlApplicationContext("spring.xml");
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
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(0,path.indexOf(".do"));
}
System.out.println("beanName=====>"+beanName);
System.out.println("meethodName======>"+methodName);
System.out.println();
Object object = context.getBean(beanName);
try {
Method method = object.getClass().getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
method.invoke(object,request,response);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
///==============================================
package com.imooc.hospital.controller;
import com.imooc.hospital.entity.Users;
import com.imooc.hospital.service.UserService;
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;
@Controller("selfController")
public class SelfController {
@Autowired
private UserService userService;
// toLogin.do
public void toLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("login.jsp").forward(request,response);
}
// login.do
public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("username");
String password = request.getParameter("password");
Users users = userService.login(userName,password);
if (userName == null){
response.sendRedirect("toLogin.do");
}else {
request.getSession().setAttribute("USER",users);
response.sendRedirect("dept/list.do");
}
}
// logout.do
public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getSession().setAttribute("USER",null);
response.sendRedirect("toLogin.do");
}
}
<?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">
<!-- 数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/hospital?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- session工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.imooc.hospital.entity"/>
</bean>
<!-- 持久化对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.imooc.hospital.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!-- 事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" 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.hospital.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
<!-- 全局自动扫描-->
<context:component-scan base-package="com.imooc.hospital"/>
<aop:aspectj-autoproxy/>
</beans>
9
收起
正在回答 回答被采纳积分+1
2回答
慕粉1705037409
2020-12-11 11:30:58
课程中的 也是这样啊
4. SSM到Spring Boot入门与综合实战
- 参与学习 人
- 提交作业 323 份
- 解答问题 8263 个
本阶段将带你学习主流框架SSM,以及SpringBoot ,打通成为Java工程师的最后一公里!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星