当前位置:网站首页>Learning record 10
Learning record 10
2022-07-25 02:51:00 【young_ man2】
Catalog
1. Interceptor (interceptor) The role of
2. The difference between interceptors and filters
3. Interceptor (interceptor) introduction
4. Interceptor method description
5. Case study ---- User login permission control
Preface
study hard , In order to yourself !
One 、 Punch in
Given a number only
2-9String , Returns all the letter combinations it can represent . You can press In any order return .Example :
Input :digits = "23" Output :["ad","ae","af","bd","be","bf","cd","ce","cf"]
//1. Tell compiler , What letter does each of your numbers correspond to ? How to use collections , Key value pair
//2. For non-existent numbers, you should be able to check for errors , For example, the input number contains 0 perhaps 1 Output the error that there is no corresponding letter
//3. For existing numbers , Make a combination , That is, complete permutation
class Solution {
public List<String> letterCombinations(String digits) {
List<String> combinations = new ArrayList<String>();
if (digits.length() == 0) {
return combinations;
}
Map<Character, String> phoneMap = new HashMap<Character, String>() {
{
put('2', "abc");
put('3', "def");
put('4', "ghi");
put('5', "jkl");
put('6', "mno");
put('7', "pqrs");
put('8', "tuv");
put('9', "wxyz");
}};
backtrack(combinations, phoneMap, digits, 0, new StringBuffer());
return combinations;
}
public void backtrack(List<String> combinations, Map<Character, String> phoneMap, String digits, int index, StringBuffer combination) {
if (index == digits.length()) {
combinations.add(combination.toString());
} else {
char digit = digits.charAt(index);
String letters = phoneMap.get(digit);
int lettersCount = letters.length();
for (int i = 0; i < lettersCount; i++) {
combination.append(letters.charAt(i));
backtrack(combinations, phoneMap, digits, index + 1, combination);
combination.deleteCharAt(index);
}
}
}
}Two 、SpringMVC Interceptor
1. Interceptor (interceptor) The role of
Springmvc The interceptor of is similar to Servlet Filters in development Filter, Used to modify the processor Pretreatment and post-processing
Link interceptors into a chain in a certain order , This chain is called the interceptor chain (Interceptor Chain) . When accessing a blocked method or field , The interceptors in the interceptor chain are called in the order they were previously defined . Interceptors, too AOP Embodiment of thought .
2. The difference between interceptors and filters

3. Interceptor (interceptor) introduction
Custom interceptors are simple , The steps are :
① Create an interceptor class to implement HandlerIntercptor Interface
package com.wxy.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterceptor1 implements HandlerInterceptor {
@Override
// Execute before target method execution
public boolean preHandle(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse, Object o) throws Exception {
System.out.println("preHandle......");
String param = httpServletRequest.getParameter("param");
if("yes".equals(param)){
return true;
}
else {
httpServletRequest.getRequestDispatcher("/error.jsp").forward(httpServletRequest,httpServletResponse);
return false; // return true On behalf of release , return false It means not to let go
}
}
// After the target method is executed Execute... Before attempting to return
@Override
public void postHandle(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
modelAndView.addObject("name","exy");
System.out.println("postHandle.....");
}
// After the whole process is executed
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("afterCompletion......");
}
}
② Configure interceptors
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 1、mvc Annotation driven -->
<mvc:annotation-driven/>
<!-- 2、 Configure the view parser -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 3、 Static resource permissions are open -->
<mvc:default-servlet-handler/>
<!-- 4、 Component scan scanning Controller -->
<context:component-scan base-package="com.wxy.controller"/>
<!-- Configure interceptors -->
<mvc:interceptors>
<mvc:interceptor>
<!-- Which resources are intercepted -->
<mvc:mapping path="/**"/>
<bean class="com.wxy.interceptor.MyInterceptor1"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>③ Test the interceptor's interception effect
package com.wxy.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class TargetController {
@RequestMapping("/target")
public ModelAndView show(){
System.out.println(" Target resource execution ......");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("name","itcast");
modelAndView.setViewName("index");
return modelAndView;
}
}
4. Interceptor method description

5. Case study ---- User login permission control

6. SpringMVC Exception handling mechanism of
6.1 Abnormal handling ideas
Exceptions in the system mainly include two types : Expected and runtime exceptions RuntimeException, The former obtains exception information by catching exceptions , The latter is mainly developed through standard code 、 Testing and other means to reduce the occurrence of runtime exceptions .
Systematic Dao、service、controller All appear through throws Exception Throw up , Finally by Springmvc The front-end controller is sent to the exception processor for exception handling , Here's the picture :

6.2 Two ways to handle exceptions
1. Use Spring MVC The simple exception handler provided SimpleMappingExceptionResolver
<!-- Configure exception handler -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!--<property name="defaultErrorView" value="error"/>-->
<property name="exceptionMappings">
<map>
<entry key="java.lang.ClassCastException" value="error1"/>
<entry key="com.itheima.exception.MyException" value="error2"/>
</map>
</property>
</bean>2. Use Spring Exception handling interface HandleExceptionResolver Customize your own exception handler
① Create an exception handling implementation class HandleExceptionResolver
public class MyExceptionResolver implements HandlerExceptionResolver {
/*
Parameters Exception: Exception object
Return value ModelAndView: Jump to error view information
*/
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
ModelAndView modelAndView = new ModelAndView();
if(e instanceof MyException){
modelAndView.addObject("info"," Custom exception ");
}else if(e instanceof ClassCastException){
modelAndView.addObject("info"," Class conversion exception ");
}
modelAndView.setViewName("error");
return modelAndView;
}
}
② Configure exception handler
!-- Custom exception handler -->
<bean class="com.itheima.resolver.MyExceptionResolver"/>③ Write exception pages
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1> General error prompt page </h1>
<h1>${info}</h1>
</body>
</html>④ Test abnormal jump
3、 ... and 、Spring Related exercises of
1.Spring Practice environment construction
1.1 Environment building steps

1.2 The relationship between users and roles

2. Role list display and adding operations
2.1 Analysis of the display steps of the role list

summary
Tips : Here is a summary of the article :
for example : That's what we're going to talk about today , This article only briefly introduces pandas Use , and pandas Provides a large number of functions and methods that enable us to process data quickly and conveniently .
边栏推荐
- Summary and sorting of XSS (cross site script attack) related content
- # CF #807 Div.2(A - D)
- "Introduction to interface testing" punch in day08: can you save all parameters to excel for test data?
- Go multiplexing
- Conceptual distinction between Po, Bo, VO, dto and POJO
- SQL Server 2022 installation
- Digital business cloud: how to realize the application value of supplier SRM management system?
- Do you know about real-time 3D rendering? Real time rendering software and application scenarios are coming
- Details of C language compilation preprocessing and comparison of macros and functions
- Ten year structure and five-year Life-03 trouble as a technical team leader
猜你喜欢

C: wechat chat software instance (wpf+websocket+webapi+entityframework)

MySQL common function summary, very practical, often encountered in interviews
![[jailhouse article] certify the uncertified rewards assessment of virtualization for mixed criticality](/img/12/1763571a99e6ef15fb7f9512c54e9b.png)
[jailhouse article] certify the uncertified rewards assessment of virtualization for mixed criticality

Pycharm writes SQLite statements without code prompts

Tp5.1 include include files (reference public files)

JS written test question -- promise, setTimeout, task queue comprehensive question

JS foundation -- data

Get to know string thoroughly

Solution to the occupation of project startup port

English grammar_ Reflexive pronoun
随机推荐
Dynamic programming -- Digital DP
Flutter apple native Pinyin keyboard input exception on textfield | Pinyin input process callback problem
Keepalivetime=0 description of ThreadPoolExecutor
How to communicate with aliens
If there is a segment in the encryption field, are you "bronze" or "King"?
Ten year structure and five-year Life-03 trouble as a technical team leader
Tp5.0 background admin access
JS foundation -- JSON
Application method and practical case of sqlmap of penetration test SQL injection
Operator explanation - C language
JS foundation -- math
# CF #808 Div.2(A - C)
Experienced the troubleshooting and solution process of an online CPU100% and the application of oom
How to take the mold for the picture of 1.54 inch TFT st7789 LCD screen
Keil compile download error: no algorithm found for: 08000000h - 08001233h solution
Use unicloud cloud function to decode wechat motion steps in applet
Sequence diagram of UML diagram series
Wechat sports field reservation of the finished works of the applet graduation project (7) mid-term inspection report
【C】 Advanced knowledge of file operation
SQL recursive follow-up