关于登陆的问题

关于登陆的问题

不太明白登陆所涉及的路径该怎么写

 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'SelfController' available

at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:685)

at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1210)

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)

at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089)

at com.imooc.hp.global.DispatcherServlet.service(DispatcherServlet.java:45)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at com.imooc.hp.global.EncodingFilter.doFilter(EncodingFilter.java:21)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)

at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)

at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)

at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)

at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)

at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)

at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Thread.java:748)

项目结构

http://img1.sycdn.imooc.com//climg/5c3ff18e00015a5919201080.jpg

package com.imooc.hp.controller;

import com.imooc.hp.entity.User;
import com.imooc.hp.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 javax.servlet.http.HttpSession;
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 IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        User user = userService.getByUsername(username,password);
        if(user == null){
            response.sendRedirect("toLogin.do");
        }else {
            HttpSession session = request.getSession();
            session.setAttribute("USER",user);
            response.sendRedirect("category/list.do");
        }
    }

    //        /main.do
    public void main(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("hospital_detail,jsp").forward(request,response);
    }
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <link rel="stylesheet" href="css/login.css">
</head>
<body>
<div class="login">
    <div class="header">
        <h1>
            <a href="login.do">登录</a>
        </h1>
        <button></button>
    </div>
    <form action="login.do" method="post">
        <div class="name">
            <input type="text" id="name" name="username">
            <p></p>
        </div>
        <div class="pwd">
            <input type="password" id="pwd" name="password">
            <p></p>
        </div>
        <div class="btn-red">
            <input type="submit" value="登录" id="login-btn">
        </div>
    </form>
</div>
</body>
</html>

核心控制器

package com.imooc.hp.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 {
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        //规范
        //       /Category/add.do          CategoryController       add
        //       /login.do                 SelfController           login
        //获得路径
        String path = request.getServletPath().substring(1);

        //      Category/add.do             CategoryController      add
        //      login.do                    SelfController          login
        int index = path.indexOf("/");
        String beanName = null;
        String methodName = null;
        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"));
        }

        //IOC容器中获取对应的对象
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        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();
        }

    }
}


正在回答

登陆购买课程后可参与讨论,去登陆

2回答

1、如下图所示,根据报错提示,找不到SelfController这个bean,

http://img1.sycdn.imooc.com//climg/5c4030c90001ad4806970079.jpg

原因如下:代码中的beanName首字母是大写的,

http://img1.sycdn.imooc.com//climg/5c4032570001ef5004570126.jpg

应该与下图标记的Controller中的单词相匹配。

http://img1.sycdn.imooc.com//climg/5c40329600010cc702630086.jpg

2、关于登陆路径的问题不是很明白同学指哪里不明白呢,在form表单的action中写login.do就可以呀,同学是指哪里不会写呢?可以详细说明一下你的问题,方便我们具有针对性的为你解答,祝学习愉快~

  • 精慕门6573819 提问者 #1
    关于传递路径的问题,所谓的二级目录与一级目录,完全取决于自己制定的规范吗?并不是实际在哪个目录下面吗? 比如在转发页面的时候,实现后台功能的Controller一般都是../xx.jsp 登陆相关Controller的则直接写xx.jsp 但是看起来这两个在项目结构中目录是一样的 这一点不大理解。
    2019-01-17 17:16:05
chrismorgen 2019-01-17 19:10:52

1、实现后台登陆的一般都是通过login.do请求去访问分配器,然后有分配去访问对应的Controller,然后在Controller中去访问login.jsp页面,比如下图标记的这个路径,

http://img1.sycdn.imooc.com//climg/5c4062250001198911940087.jpg

2、路径的写法有很多种,同学可以配上截图描述一下,因为用文字描述我不知道到你的路径是怎么写的,祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
SSM主流框架入门与综合项目实战2018版
  • 参与学习           人
  • 提交作业       205    份
  • 解答问题       4317    个

Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师