BaseController 层 优化代码
1 package com.seecen.controller; 2 3 import com.seecen.service.StudentService; 4 import org.springframework.web.context.WebApplicationContext; 5 import org.springframework.web.context.support.WebApplicationContextUtils; 6 7 import javax.servlet.ServletConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 import java.io.IOException; 13 import java.io.PrintWriter; 14 import java.io.UnsupportedEncodingException; 15 16 /** 17 * @program: springWeb 18 * @description: 19 * 所有 controller 的基类 20 * @author: lan 21 * @create: 2022-05-19 16:24 22 * @version:1.0 23 **/ 24 public class BaseController extends HttpServlet { 25 26 public StudentService studentService; 27 28 @Override 29 public void init(ServletConfig config) throws ServletException { 30 // 通过 web 程序获取 IOC 容器 31 WebApplicationContext wac = WebApplicationContextUtils 32 .getWebApplicationContext 33 (config.getServletContext()); 34 // 从 IOC 容器中取出 StudentService 实例 35 studentService = wac.getBean(StudentService.class); 36 } 37 38 39 /** 40 * 自定义的 servlet 初始设置 41 * @param req 42 * @param resp 43 * @throws UnsupportedEncodingException 44 */ 45 public void defaultSet(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException { 46 req.setCharacterEncoding("UTF-8"); 47 resp.setCharacterEncoding("UTF-8"); 48 // 设置允许其他非同源访问,Access-Control-Allow-Origin 49 resp.setHeader("Access-Control-Allow-Origin", "*"); 50 51 } 52 53 54 /** 55 * 将指定的内容响应至页面 56 * @param resp 57 * @param content 58 * @throws IOException 59 */ 60 public void printHtml(HttpServletResponse resp,String content) throws IOException { 61 PrintWriter writer = resp.getWriter(); 62 writer.print(content); 63 writer.close(); 64 } 65 66 67 }
findAllController
1 package com.seecen.controller; 2 3 import com.alibaba.fastjson.JSONArray; 4 import com.seecen.pojo.Student; 5 import com.seecen.service.StudentService; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.context.WebApplicationContext; 9 import org.springframework.web.context.support.SpringBeanAutowiringSupport; 10 import org.springframework.web.context.support.WebApplicationContextUtils; 11 12 import javax.servlet.ServletConfig; 13 import javax.servlet.ServletException; 14 import javax.servlet.annotation.WebServlet; 15 import javax.servlet.http.HttpServlet; 16 import javax.servlet.http.HttpServletRequest; 17 import javax.servlet.http.HttpServletResponse; 18 import java.io.IOException; 19 import java.io.PrintWriter; 20 import java.util.List; 21 22 /** 23 * @program: springWeb 24 * @description: 25 * 用于接收用户发起的 /findAll.do 请求的 servlet 26 * @author: lan 27 * @create: 2022-05-19 15:13 28 * @version:1.0 29 **/ 30 @WebServlet("/findAll.do") 31 public class FindAllController extends BaseController { 32 33 @Override 34 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 35 System.out.println("接收到了 /findAll.do 请求"); 36 // 1,设置编码格式,设置跨域 37 defaultSet(req, resp); 38 39 // 2,获取传递参数,调用 service 层 40 System.out.println(studentService); 41 List<Student> list = studentService.findAll(); 42 43 // 3,通过 resp 将 json 响应至页面 44 printHtml(resp, JSONArray.toJSONString(list)); 45 } 46 47 }