How to transmit the attribute value through the session property to the parameters of the attribute value to the controller

tags: java  servlet  session  spring  spring boot  

Today, I want to be the idea of ​​the waterfall flow that passed to the back end after the front end of the MD5 encryption.

I did a SpringMVC interceptor (interceptor) who did not understand Filter and Interceptor and Listener. You can go to this blogger's blog.NapYo

https://blog.csdn.net/qq_34958326/article/details/120535736

Next we analyze the problem through the URL

http://localhost:8080/register/username=12


Among them, the username and 12 in the URL passed to the back end. I believe everyone knows that it is through the request packaging attribute of the HTTP.

The interceptor code is as follows:


@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("----------------helo I am here (LoginInterceptor)!!! -----------------------");
   			log.debug(request.toString());
        return true;

    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        //System.out.println ("Posthandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //System.out.println ("After executing aftercompleting");

    }
}

The interceptor is written. We need to write it in webmvcconfigurer. I will simply take the code to everyone

@Configuration
public class LoginConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/login");
    }
}

We step into the topic

We start to write a break point
 We look at the console


I am very happy to see this. There is a value here, click in and see

 What is the value of a key and value, but the value still has a value, let's click in to see


Seeing this without "username" -> "12"

We can think of

org.springframework.web.servlet.HandlerMapping.uriTemplateVariables = {key=“org.springframework.web.servlet.HandlerMapping.uriTemplateVariables”,value=“LinkeHashMap”}
We enter the value of the username that we get into LinkhashMap to get

We directly modify the code in the interceptor

First obtain the value of the attribute

// This return is an Object, so the next step is to convert the value of HashMap
request.getAttribute("org.springframework.web.servlet.HandlerMapping.uriTemplateVariables");
// Step 2
HashMap parameter = (HashMap<String,String>)request.getAttribute("org.springframework.web.servlet.HandlerMapping.uriTemplateVariables");
// The third step we can know the value of the attribute value. This is HashMap. We can use get and get the value
 String username = (String)parameter.get("username");
// Last test results

The code is finally posted at the end


@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        
        log.debug(request.toString());
        System.out.println("----------------helo I am here (LoginInterceptor)!!! -----------------------");
        HashMap parameter = (HashMap<String, String>) request.getAttribute("org.springframework.web.servlet.HandlerMapping.uriTemplateVariables");
        String username = (String) parameter.get("username");
        log.debug("------------------------username = " + username + "---------------------");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        //System.out.println ("Posthandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //System.out.println ("After executing aftercompleting");

    }
}

Speak! Intersection Intersection Intersection

Intelligent Recommendation

As attribute and property value and related processing of Vue.js

Attribute and property are concepts that are easy to confuse in Web development, but for value, because of its particularity, it is easier to confuse. This article will try to sort out and illustrate ...

A property of Java depends on another attribute value

A property in a Java class starts to start from another property, and now is as follows. The simplest example, judgment is successful Front-end callresult.success()You can judge whether it is TRUE. An...

First Vue 3.0 - Attribute: Property Value

See the interaction effect:https://course.51qux.com/vue3-0-1 Copyright Notice:Original works, allow reproduced, please be sure to indicate the article Original source , Author information and this sta...

Cycle to create a property name and attribute value

Note 4.7: 1 loop to the object to create a property name and property value:...

More Recommendation

Attribute value

When passing the value in the forward direction, set the textField of the first page to a global variable. In the second page, you need to define an attribute in the .h file to receive the content of ...

The attribute value parameter can not be obtained in the session object

After adding an attribute name to the session object in a register page, the attributes in the session object cannot be obtained in another login interface Create login.jsp to display the attribute va...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top