老师,配置过滤器后白屏怎么办?

老师,配置过滤器后白屏怎么办?

package com.zj.hospital.controller;

import com.zj.hospital.entity.Category;
import com.zj.hospital.entity.User;
import com.zj.hospital.service.CategoryService;
import com.zj.hospital.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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;
import java.text.ParseException;
import java.util.List;

@Controller("DefaultController")
public class DefaultController {

   @Qualifier("categoryService")
   @Autowired
   private CategoryService categoryService;

   @Autowired
   private UserService userService;
   // index.do
   public void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
       List<Category> categories = categoryService.findAll();
       for (Category c : categories) {
           System.out.println(c.toString());
       }
       request.setAttribute("categories",categories);
       request.getRequestDispatcher("/index.jsp").forward(request, response);
   }

   // /toLogin.do
   public void toLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       System.out.println("ddddd================");
       request.getRequestDispatcher("login.jsp").forward(request,response);
   }

   // /login.do
   public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
       String username = request.getParameter("username");
       String password = request.getParameter("password");
       User user = userService.login(username, password);
       if (user == null) {
           request.setAttribute("msg","用户名或密码错误");
           request.getRequestDispatcher("../login.jsp").forward(request,response);
       } else {
           HttpSession session = request.getSession();
           session.setAttribute("user",user);
           request.getRequestDispatcher("/category/list.do").forward(request,response);
       }
   }
}

package com.zj.hospital.global;


import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class AuthFilter implements Filter {
   @Override
   public void init(FilterConfig filterConfig) throws ServletException {

   }

   public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
       HttpServletRequest request = (HttpServletRequest) servletRequest;
       HttpServletResponse response = (HttpServletResponse) servletResponse;
       //String path = request.getRequestURI();
       String path = request.getServletPath();
       if (path.toLowerCase().indexOf("login")!=-1) {
           filterChain.doFilter(request,response);
       }else{
           HttpSession session = request.getSession();
           Object obj = session.getAttribute("user");
           if (obj!=null) {
               filterChain.doFilter(request,response);
           }else{
               response.sendRedirect(request.getContextPath()+"/toLogin.do");
           }
       }
   }

   @Override
   public void destroy() {

   }
}

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        version="3.1">

   <filter>
   <filter-name>encoding</filter-name>
   <filter-class>com.zj.hospital.global.EncodingFilter</filter-class>
   </filter>
   <filter-mapping>
       <filter-name>encoding</filter-name>
       <url-pattern>/*</url-pattern>
   </filter-mapping>

   <filter>
       <filter-name>Auth</filter-name>
       <filter-class>com.zj.hospital.global.AuthFilter</filter-class>
   </filter>
   <filter-mapping>
       <filter-name>Auth</filter-name>
       <url-pattern>*.jsp</url-pattern>
   </filter-mapping>
   <filter-mapping>
       <filter-name>Auth</filter-name>
       <url-pattern>*.do</url-pattern>
   </filter-mapping>

   <servlet>
       <servlet-name>Global</servlet-name>
       <servlet-class>com.zj.hospital.global.DispatchServlet</servlet-class>
   </servlet>
   <servlet-mapping>
       <servlet-name>Global</servlet-name>
       <url-pattern>*.do</url-pattern>
   </servlet-mapping>


</web-app>

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



正在回答

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

2回答

同学你好!

你toLogin中的以下语句是否有输出呢?

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

如果没有,你的控制台是否报错了。如果有建议你贴一下报错信息。如果没有报错信息建议你贴一下你的核心控制器的代码,老师看一下

注意:将代码贴在我要回答中

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

  • BirdMan98 提问者 #1
    控制台没有报错 package com.zj.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;
    2020-02-20 22:18:32
  • BirdMan98 提问者 #2
    public class DispatchServlet extends GenericServlet { private ApplicationContext context; 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; /* * /login.do * /categoryController/add.do * /deptController/add.do * * */ String path = request.getServletPath().substring(1); /* * login.do * categoryController/add.do category/add.do * deptController/add.do?cid=1 * * */ int index; String beanName=null; String methodName=null; if ((index = path.indexOf("/")) != -1) { beanName = path.substring(0, index) + "Controller"; methodName = path.substring(index + 1, path.indexOf(".")); } else { beanName = "DefaultController"; methodName = path.substring(0, path.indexOf(".")); } Object obj = context.getBean(beanName); Method method = null; try {
    2020-02-20 22:18:48
  • BirdMan98 提问者 #3
    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(); } } }
    2020-02-20 22:19:09
好帮手慕阿满 2020-02-21 15:39:59

同学你好,建议同学可以在DispatchServlet类中的beanName和methodName处打上断点,使用debug模式执行,查看是否执行到这里,beanName和methodName是否获取有值。

祝:学习愉快~

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

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

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

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

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