1、java的工程目錄html
pom.xml:工程的依賴包和對工程的說明java
templates:放一些模板spring
static:放一些網站須要的靜態服務器
2、網站的簡單分層cookie
controller與service之間,隨着工程的變大,會有一個base層session
3、簡單的測試app
建立一個controller文件函數
@Controller:表示這是一個controller測試
@RequestMapping:對訪問的映射地址進行設置網站
@ResponseBody:由於返回的是一個字符串,因此要說明response是一個body
最後在ToutiaoApplication處進行運行
網站的地址也能夠設置多個:
@RequestMapping(path = {"/","index"})
指定參數:
@PathVariable:表示是@RequestMapping處的參數
@RequestParam表示是地址?後的參數
效果圖:
開發的時候,通常使用模板,templates
4、關於使用thymeleaf 模板語言
詳情:https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#maps
5、request、response
1.request
@RequestMapping(value = {"/request"}) @ResponseBody public String request(HttpServletRequest request, HttpServletResponse response, HttpSession session) { StringBuilder sb = new StringBuilder(); Enumeration<String> headerNames = request.getHeaderNames(); //顯示reques中Header while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); sb.append(name + ":" + request.getHeader(name) + "<br>"); } for (Cookie cookie : request.getCookies()) { //顯示cookie sb.append("Cookie: "); sb.append(cookie.getName()); sb.append(":"); sb.append(cookie.getValue()); sb.append("<br>"); } sb.append("getMethod: " + request.getMethod() + "<br>"); //其餘一些內容顯示 sb.append("getPathInfo: " + request.getPathInfo() + "<br>"); sb.append("getQueryString:" + request.getQueryString() + "<br>"); sb.append("getRequestURI:" + request.getRequestURL() + "<br>"); return sb.toString(); }
效果圖
2.response
@RequestMapping(value = {"/response"}) @ResponseBody public String response(@CookieValue(value = "nowcoderid",defaultValue = "a") String nowcoderid, @RequestParam(value = "key",defaultValue = "key") String key, @RequestParam(value = "value",defaultValue = "value") String value, HttpServletResponse response){ response.addCookie(new Cookie(key,value)); response.addHeader(key,value); return "Nowcoderid From Cookie: " + nowcoderid; }
response.addCookie(new Cookie(key,value));//會將nowcoderid寫入
6、重定向
301:永久轉移 //第二次就直接請求服務器的跳轉頁
302:臨時轉移
@RequestMapping("/redirect/{code}") public RedirectView redirect(@PathVariable("code") int code){ RedirectView red = new RedirectView("/",true); if(code == 301){ red.setStatusCode(HttpStatus.MOVED_PERMANENTLY); //設置成永久性跳轉 } return red; }
第二種方法:(永遠302跳轉)
@RequestMapping("/redirect/{code}") public String redirect(@PathVariable("code") int code){ return "redirect:/"; } }
7、錯誤處理
@RequestMapping(value = {"/admin"}) @ResponseBody public String admin(@RequestParam(value = "key", required = false) String key) throws IllegalAccessException { if ("admin".equals(key)) { return "Hello admin!"; } throw new IllegalAccessException("Key error"); } @ExceptionHandler //自定義錯誤處理函數 @ResponseBody public String error(Exception e) { return "error: " + e.getMessage(); }
效果圖
1.出錯頁
2.成功頁
8、Spring IOP AOP
參考網站:https://docs.spring.io/spring/docs/current/spring-framework-reference/
IOP
例子IOP
1.建立service文件,ToutiaoService
@Service public class ToutiaoService { public String say(){ return "This is from ToutiaoService."; } }
在controller文檔使用,我用的是IndexController
2.AOP
底層其實用的是:Aspectj
舉例:
1.建立文件LogAspect
@Aspect @Component public class LogAspect { private static final Logger logger = LoggerFactory.getLogger(LogAspect.class); @Before("execution(* com.nowcoder.toutiao.controller.IndexController.*(..))") //對使用此切面的範圍,進行劃分 public void beforeMethod(JoinPoint joinPoint) { StringBuilder sb = new StringBuilder(); for (Object arg:joinPoint.getArgs()){ sb.append("arg:" + arg.toString() + "|"); } logger.info("before method" + sb.toString()); } @After("execution(* com.nowcoder.toutiao.controller.IndexController.*(..))") public void afterMethod(JoinPoint joinPoint) { logger.info("after method"); } }
在訪問範圍內的網頁後: