springboot use jsp, filters, interceptors ( interceptor and filter difference focus )
jsp use the configuration
One
Creating springboot project being only add two Dependencies in the maven: devtools (hot deployment) web (remote calls, support services)
In pom.xml created project <dependencies> jsp need to increase reliance
<!--整合jsp,需要的依赖--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency>
Two
in application.yml (yml easy to use format, the properties changed) files, view resolution increase
Server:
# Set the port 80 may be omitted when accessing
Port: 80
# view resolver
Spring:
MVC:
View:
prefix: /
suffix: .jsp
three
Mark webapp. Establishing webapp directory under src / main, then Project Structure, the idea is to identify the labeled webapp
New Directory the src / main as webapp, in the project structure
On the right side out of the establishment, conduct some operations. After application apply ok to exit. If there are Web, but the lower half of the right side is empty, click on the right side of the lower half of the +, you can specify the files you created
This, the establishment of jsp files into the webapp directory, direct access start the project in the browser you can navigate to jsp resources.
Modify the configuration file application.properties
Server:
# Set the port 80 may be omitted when accessing
Port: 80
the servlet:
context-path: /
# view resolver
Spring:
MVC:
View:
prefix: /
suffix: .jsp
Use filters springboot
First, a filter to create your own
Package com.xiaoeyu.springboot4.filter; Import the javax.servlet *. ; Import java.io.IOException; // by implementing customize filters Filter public class MyFilter the implements Filter { / * * servelet filter is predetermined specifications, only a web application, such as Tomcat servlet container is supported, just before entering servlet request, and came out function * blockers: either the web application may application, Swing program, the frame is spring supported, is a component of the spring, you can use any resource in the spring, the object * such as Service objects, data source, transaction management by injection to the IOC interceptors can be. Before and after the interceptor able to dive into the method, before and after the exception is thrown, there is greater flexibility * * / @Override public void doFilter (ServletRequest servletRequest, ServletResponse ServletResponse, FilterChain filterChain)throws IOException, ServletException { System.out.println ( "the doFilter: performing filter before reaching the request servlet" ); the FilterChain.doFilter (servletRequest, ServletResponse); System.out.println ( "the doFilter: after leaving Tomcat servlet request to leave performed before container " ); } }
The filter register is in use on springboot
Package com.xiaoeyu.springboot4.config; Import com.xiaoeyu.springboot4.filter.Myfilter; Import org.springframework.boot.web.servlet.FilterRegistrationBean; Import org.springframework.context.annotation.Bean; Import org.springframework.context .annotation.Configuration; // annotation category is a configuration equivalent to the configuration file .xml, springboot frame @Bean reads the configuration process of starting the annotation process, the return value is injected into the vessel IOC @ the configuration public class the FilterConfig { @Bean // equivalent to the spring configuration file <bean> configuration, the method will return an object value, is injected into the vessel IOC public FilterRegistrationBean filterRegistrationBean () { FilterRegistrationBean the bean = new newFilterRegistrationBean (); bean.setFilter ( new new MyFilter ()); // intercept i.e. what filters filter registration bean.addUrlPatterns ( "/ *"); // disposed to intercept the path of the filter bean.setOrder (1) ; // set the boot sequence, a positive number, the smaller the priority return the bean; } }
springboot use interceptors
Custom interceptors
Package com.xiaoeyu.springboot4.interceptor; Import org.springframework.web.servlet.HandlerInterceptor; Import org.springframework.web.servlet.ModelAndView; Import the javax.servlet.http.HttpServletRequest; Import javax.servlet.http.HttpServletResponse; / / interceptor, the method needs to access the controller will trigger public class myInterceptor the implements HandlerInterceptor { @Override public boolean preHandle (Request the HttpServletRequest, HttpServletResponse the Response, Object Handler) throws Exception { System.out.println ( "preHandle: entering the interceptor before the implementation of Controller calling "); // returns false request backtrack, but the front display 200 status code, any request can be successful, since there is not down to backtrack return to true ; } @Override public void The postHandle (the HttpServletRequest Request, Response the HttpServletResponse, Object Handler, ModelAndView ModelAndView) throws Exception { System.out.println ( "the postHandle: Controller logic been executed, before the execution of the return ModelAndView Controller can manipulate data ModelAndView" ); modelAndView.addObject ( "name", "postHandle: edit name the JRL" ); System.out.println ( "postHandle: modify the name value in ModelAndView" ); } @Override public voidafterCompletion (the HttpServletRequest Request, Response the HttpServletResponse, Object Handler, Exception EX) throws Exception { System.out.println ( "afterCompletion: Controller after a Return, Filter before returning it to the client (should be performed before FilterAfter Method)" ); } }
The interceptor is loaded into it, can play a role in springboot
Package com.xiaoeyu.springboot4.config; Import com.xiaoeyu.springboot4.interceptor.Myinterceptor; Import org.springframework.context.annotation.Configuration; Import org.springframework.web.servlet.config.annotation.InterceptorRegistry; Import org.springframework .web.servlet.config.annotation.WebMvcConfigurer; // inform springboot this is a configuration class, call addInterceptors method in object implementation WebMvcConfigurer's. Adding interceptor is injected into the vessel IOC @Configuration public class InterceptorConfig the implements WebMvcConfigurer { @Override public void new new addInterceptors (InterceptorRegistry Registry) { registry.addInterceptor ( . myInterceptor ()) addPathPatterns ( "/ *"); // path interception } }
Controller triggered by the interceptor. Can not access static resources does not trigger the interceptor
Package com.xiaoeyu.springboot4.controller; Import org.springframework.stereotype.Controller; Import org.springframework.web.bind.annotation.RequestMapping; Import org.springframework.web.servlet.ModelAndView; @Controller public class HelloController in { / * from the front desk through url localhost / hello? name = Feng Yongkang pass over a parameter, and print it and put ModelAndView, the modification of the name in the interceptor, and jump to the jsp page, view the results * / @ RequestMapping ( "/ Hello" ) public ModelAndView Hello (String name, ModelAndView Music Videos) { System.out.println ( "name:" + name); mv.setViewName ("test"); mv.addObject("name", name); return mv; } }
This is for facilitating displaying data returned directly test.jsp character may change
<% @ Page contentType = " text / HTML; charset = UTF-8 " Language = " the Java " %> < HTML > < head > < title > the Title </ title > </ head > < body > I am a jsp page < br > acquired data in ModelAndView = $ {name} name < br > </ body > </ HTML >