Simple jump topic
I personally recommend practicing the construction process again. If you feel troublesome, you can copy the previous project directly, but you need to modify a little information in pom.xml.
<groupId>com.hanpang.springmvc</groupId><artifactId>springmvc-demo01</artifactId><version>0.0.1-SNAPSHOT</version>
1. Core configuration classes and loading classes
package com.hanpang.config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.EnableWebMvc;@Configuration@EnableWebMvc@ComponentScan(basePackages="com.hanpang.**.web")public class WebConfig {} package com.hanpang.config;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[] {WebConfig.class}; } @Override protected Class<?>[] getServletConfigClasses() { return null; } @Override protected String[] getServletMappings() { return new String[] {"/"}; }}2.JavaWeb stage jump method
Please note the formal parameters in SpringMVC method, the framework completes the instantiation operation for us.
package com.hanpang.web;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controller//Tell it that it is a controller public class Demo01Controller { @RequestMapping(path="/test01") public ModelAndView Traditional way jump_request forwarding (HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { System.out.println("The formal parameters are instantiated by default"); request.getRequestDispatcher("/WEB-INF/jsp/demo01.jsp").forward(request, response); return null; } @RequestMapping(path="/test02") public ModelAndView Traditional way jump_redirect(HttpServletRequest request,HttpServletResponse response) throws IOException { System.out.println("The formal parameters are instantiated by default"); response.sendRedirect(request.getContextPath()+"/view/result01.jsp"); return null; }} NOTE: We almost no longer use this method, just a simple demonstration and review, at least we can use this method to obtain the Servlet API!!!
3. Demonstrate how Controller jumps to JSP
At the end of the example, we will add the JSP view parser. At the beginning, we will still follow the traditional method and have a step-by-step process.
package com.hanpang.web;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controller//Tell it that it is a controller public class Demo01Controller { @RequestMapping(path="/test03") public ModelAndView By default, it is requested forwarding(){ ModelAndView mav = new ModelAndView(); mav.setViewName("/WEB-INF/jsp/demo01.jsp"); return mav; } @RequestMapping(path="/test04") public ModelAndView Set the redirection method(){ ModelAndView mav = new ModelAndView(); mav.setViewName("redirect:/view/result01.jsp"); //or //mav.setViewName(UrlBasedViewResolver.REDIRECT_URL_PREFIX+"/view/result01.jsp"); return mav; }} It feels like the Java Web stage method, but a simple prefix is set during redirection.
4. Demonstrate how Controller jumps to Controller
Similar to calling from one Servlet to another
package com.hanpang.web;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controller//Tell it that it is a controller public class Demo01Controller { @RequestMapping(path="/test05") public ModelAndView Directly set the mapping path (){ ModelAndView mav = new ModelAndView(); mav.setViewName("/test03"); return mav; } @RequestMapping(path="/test06") public ModelAndView Set redirection(){ ModelAndView mav = new ModelAndView(); mav.setViewName("redirect:/test04"); return mav; }} 5. Add JSP view parser
During the above demonstration, we found that the setViewName in ModelAndView is used to complete the jump. The data passed here is a string, but the processing method is different. When the jump path is prefixed redirect:, the processing method is different.
Also, if we have multiple strings similar to /WEB-INF/jsp/demo01.jsp, we find that there are many public parts. SpringMVC provides us with a class that specializes in handling Controller requests forwarding JSP pages.
Please note my description: If you find that the string you passed does not have any prefix identifier, then by default, use the configuration JSP view parser to handle it and complete the request forwarding operation
Note: Configure the core configuration class
package com.hanpang.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.ViewResolver;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.view.InternalResourceViewResolver;import org.springframework.web.servlet.view.JstlView;@Configuration@EnableWebMvc@ComponentScan(basePackages="com.hanpang.**.web")public class WebConfig { @Bean//Instantiate public ViewResolver viewResolver() { InternalResourceViewResolver jspViewResolver = new InternalResourceViewResolver(); jspViewResolver.setViewClass(JstlView.class);//springmvc supports jstl tag jspViewResolver.setPrefix("/WEB-INF/"); jspViewResolver.setSuffix(".jsp"); return jspViewResolver; }} **Note:**Please pay attention to the annotation on the method @Bean
The method is equivalent to the code in XML as follows
<bean id="jspResourceViewResolver"> <property name="prefix" value="/WEB-INF/"/> <property name="suffix" value=".jsp"/> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> </bean>
Improve Controller jump JSP code
This view parser can only forward JSP requests and is invalid for redirects. Please pay attention to the comment content of the code
@RequestMapping(path="/test03") public ModelAndView By default, it is requested forwarding(){ ModelAndView mav = new ModelAndView(); //mav.setViewName("/WEB-INF/jsp/demo01.jsp"); //Advanced: By default, it will be handled using the JSP view parser, // prefix+"jsp/demo01"+suffix => /WEB-INF/jsp/demo01.jsp mav.setViewName("jsp/demo01"); //I found that the string has no prefix modification return mav; } @RequestMapping(path="/test04") public ModelAndView Set the redirection method(){ ModelAndView mav = new ModelAndView(); //The parser invalidates redirection mav.setViewName("redirect:/view/result01.jsp"); return mav; } Improve Controller jump Controller code
@RequestMapping(path="/test05")public ModelAndView Directly set the mapping path (){ ModelAndView mav = new ModelAndView(); mav.setViewName("/test03"); return mav;}After configuring the JSP view parser, test the above code again, and see the access results with surprise.
It conforms to what we said before "/test03" is a string that will be processed by default using the JSP view parser. So how to improve it?
You can set the prefix "forward:", and the code is improved as follows:
@RequestMapping(path="/test05")public ModelAndView Directly set the mapping path (){ ModelAndView mav = new ModelAndView(); mav.setViewName("forward:/test03"); //or //mav.setViewName(UrlBasedViewResolver.FORWARD_URL_PREFIX+"/test03"); return mav;} When the string is found to be modified with forward:, the processing situation changes to convert from the Controller request to another Controller. If redirection is performed, the code is as follows:
@RequestMapping(path="/test06")public ModelAndView Set redirect(){ ModelAndView mav = new ModelAndView(); mav.setViewName("redirect:/test04"); return mav;} 6.InternalResourceViewResolver Appendix
InternalResourceViewResolver: It is a subclass of URLBasedViewResolver, so it supports all the features supported by URLBasedViewResolver.
In practical applications, InternalResourceViewResolver is also the most widely used view resolver. So what are the unique features of InternalResourceViewResolver?
From a literal point of view, we can interpret InternalResourceViewResolver as an internal resource view resolver. This is a feature of InternalResourceViewResolver.
InternalResourceViewResolver will resolve the returned view names into InternalResourceView objects. InternalResourceView will store the model attributes returned by the Controller processor method in the corresponding request attributes, and then redirect the request forward to the target URL on the server side through RequestDispatcher.
For example, in InternalResourceViewResolver, prefix=/WEB-INF/, suffix=.jsp is defined in InternalResourceViewResolver, and then the view name returned by the requested Controller processor method is test. At this time, InternalResourceViewResolver will parse the test into an InternalResourceView object, first store the returned model attributes in the corresponding HttpServletRequest attribute, and then use RequestDispatcher to forward the request to /WEB-INF/test.jsp on the server side. This is a very important feature of InternalResourceViewResolver. We all know that the content stored in /WEB-INF/ cannot be directly requested through request request. For security reasons, we usually place the jsp file in the WEB-INF directory, and the way InternalResourceView jumps on the server side can solve this problem well. Below is a definition of an InternalResourceViewResolver. According to this definition, when the returned logical view name is test, InternalResourceViewResolver will add a defined prefix and suffix to it, form "/WEB-INF/test.jsp", and then use it as an InternalResourceView url to create a new InternalResourceView object.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.