1. 程式人生 > 其它 >Controller簡單優化示例程式碼

Controller簡單優化示例程式碼

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 }