当前位置:网站首页>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 .
边栏推荐
- Flutter apple native Pinyin keyboard input exception on textfield | Pinyin input process callback problem
- Analysis of FLV packaging
- Inheritance (prototype)
- Experienced the troubleshooting and solution process of an online CPU100% and the application of oom
- It7259q-13, it7259ex-24 feature wearable devices
- Case analysis of building exhibition service management system with low code development platform
- String class
- H5 common positioning function package
- Jenkins plug-in development -- plug-in expansion
- The file in scanpy1.9.1 cannot be read in scanpy1.7.2. The problem is solved
猜你喜欢

Tp5.1 initialize initialization method (not \u initialize)

Do you know about real-time 3D rendering? Real time rendering software and application scenarios are coming

B2B e-commerce trading platform of heavy metal industry: breaking the state of data isolation and improving the benefits of heavy metal industry
![[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

Digital commerce cloud fine chemical industry management platform integrated informatization solution

Introduction to web security telent testing and defense

Ctfshow misc introduction

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

English grammar_ Reflexive pronoun

JS written test question -- promise, setTimeout, task queue comprehensive question
随机推荐
Wechat sports field reservation of the finished works of the applet graduation project (5) assignment
JS written test question -- deep copy of object
JS foundation -- data
Flume's study notes
Mgre.hdlc.ppp.chap.nat comprehensive experiment
Classic network learning RESNET code implementation
"Introduction to interface testing" punch in to learn day09: Micro service interface: how to use mock to solve chaotic call relationships
Arthas case: dynamic update application logger level
MySQL common function summary, very practical, often encountered in interviews
Remote sensing image classification tool and visualization application of WebGIS
Three ways to solve your performance management problems
Common Oracle commands
Interview question -- event cycle
Daily three questions 7.16
Summary and sorting of XSS (cross site script attack) related content
Coal industry supply chain centralized mining system: digitalization to promote the transformation and upgrading of coal industry
# CF #808 Div.2(A - C)
Study notes of filebeat
2022-07-19: all factors of F (I): I are added up after each factor is squared. For example, f (10) = 1 square + 2 square + 5 square + 10 square = 1 + 4 + 25 + 100 = 130.
Map set learning