老师,配置过滤器后白屏怎么办?
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>
正在回答
同学你好!
你toLogin中的以下语句是否有输出呢?
如果没有,你的控制台是否报错了。如果有建议你贴一下报错信息。如果没有报错信息建议你贴一下你的核心控制器的代码,老师看一下
注意:将代码贴在我要回答中
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
- 参与学习 人
- 提交作业 323 份
- 解答问题 8263 个
本阶段将带你学习主流框架SSM,以及SpringBoot ,打通成为Java工程师的最后一公里!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星