Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Spring Boot practice-- SpringMVC View parsing

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

I. Notes

In the spring-boot+spring mvc project, sometimes we need to configure some project settings, we will involve these three, so, what is the relationship between them?

First of all, @ EnableWebMvc=WebMvcConfigurationSupport, using the @ EnableWebMvc annotation equals extending WebMvcConfigurationSupport without overriding any methods.

So there are several ways to use it:

@ EnableWebMvc+extends WebMvcConfigurationAdapter, override the method of the parent class in the extended class, which shields the setting extends WebMvcConfigurationSupport in @ EnableAutoConfiguration of springboot, overrides the method of the parent class in the extended class, shields the setting extends WebMvcConfigurationAdapter in @ EnableAutoConfiguration of springboot, overrides the method of the parent class in the extended class, and still uses the settings in @ EnableAutoConfiguration of springboot

Which method is suitable depends on the individual's demand for the project and the degree of control.

There are some default settings in both WebMvcConfigurationSupport (@ EnableWebMvc) and @ EnableAutoConfiguration

And WebMvcConfigurationAdapter is an abstract class.

For more information on how to personalize the settings in the class, please refer to the following article:

Spring Boot: custom HTTP message translator

EnableWebMvc official documentation

Using the @ EnableWebMvc annotation, you need to programmatically specify the view file-related configuration / * * Web MVC configuration adapter * @ ClassName: WebAppConfigurer * @ Description: * @ author OnlyMate * @ Date at 2:39:31 on August 28th 2018 * * WebAppConfigurer extends WebMvcConfigurerAdapter is out of date in the Spring Boot2.0 version Replace * * / @ Configurationpublic class WebAppConfigurer implements WebMvcConfigurer {/ * springmvc View Resolution * @ Title: viewResolver * @ Description: TODO * @ Date with the new class mentioned on the official website 4:46:07 * @ author OnlyMate * @ return * / @ Bean public InternalResourceViewResolver viewResolver () {InternalResourceViewResolver viewResolver = new InternalResourceViewResolver () ViewResolver.setPrefix ("/ WEB-INF/views/"); viewResolver.setSuffix (".jsp"); / / viewResolver.setViewClass (JstlView.class); / / this property usually does not need to be manually configured, and higher versions of Spring automatically detect return viewResolver. } / * SpringBoot Settings Home page * / @ Override public void addViewControllers (ViewControllerRegistry registry) {WebMvcConfigurer.super.addViewControllers (registry); registry.addViewController ("/"). SetViewName ("index"); registry.setOrder (Ordered.HIGHEST_PRECEDENCE);} using @ EnableAutoConfiguration annotation, the configuration in the application.properties or application.yml file will be read

View

# # View spring.mvc.view.prefix=/WEB-INF/views/spring.mvc.view.suffix=.jsp

Or

Spring: http: encoding: charset: UTF-8 enabled: true force: true messages: encoding: UTF-8 profiles: active: dev mvc: view: prefix: / WEB-INF/views/ suffix: .jsp II, template engine

Spring boot integrates ContentNegotiatingViewResolver and BeanNameViewResolver by default in springmvc's view parser, and the automatically configured template engine has been integrated on the view engine, as follows:

FreeMarkerGroovyThymeleafVelocity (deprecated in 1.4) Mustache

JSP technology spring boot is not officially recommended for three reasons:

On tomcat, jsp cannot parse nested tomcat containers, that is, when packaged into executable jar, Jetty nested containers do not support jspUndertow

Other template engines are supported by spring boot, and they will look for template engines in classpath's templates by default. Here, if we use freemarker template engines,

Now pom.xml starts the freemarker template engine view org.springframework.boot spring-boot-starter-freemarker

Define a template suffix as ftp. Note that it is in the templates directory of classpath.

Return the view path @ Controllerpublic class HelloWorldController {private Logger logger = LoggerFactory.getLogger (HelloWorldController.class) on controller; @ Value ("${question}") private String question; @ Value ("${answer}") private String answer; @ Value ("${content}") private String content; @ ResponseBody @ RequestMapping ("/ hello") public String index () {return "hello world" } @ ResponseBody @ RequestMapping (value= "/ hello1") public String index1 () {return question + answer;} @ ResponseBody @ RequestMapping (value= "/ hello2") public String index2 () {return content;} @ RequestMapping (value= "/ hello3") public String index3 (Model model) {try {model.addAttribute ("question", question) Model.addAttribute ("answer", answer);} catch (Exception e) {logger.info ("HelloWorldController = = > index3 method: error", e);} return "/ hello";}}

If you use @ RestController, @ Responsebody is added to each method by default, and the returned value of the method is directly converted by httpmessageconverter. If you want to return directly to the view, you need to specify modelAndView directly.

Public class HelloWorldController {private Logger logger = LoggerFactory.getLogger (HelloWorldController.class); @ Value ("${question}") private String question; @ Value ("${answer}") private String answer; @ Value ("${content}") private String content; @ RequestMapping ("/ hello3") public ModelAndView hello3 () {try {model.addAttribute ("question", question); model.addAttribute ("answer", answer) } catch (Exception e) {logger.info ("HelloWorldController = = > index3 method: error", e);} return new ModelAndView ("hello") }} configuration homepage / * Web MVC configuration Adapter * @ ClassName: WebAppConfigurer * @ Description: * @ author OnlyMate * @ Date 2:39:31 on August 28th 2018 * * WebAppConfigurer extends WebMvcConfigurerAdapter is out of date in the Spring Boot2.0 version Replace * * / @ Configurationpublic class WebAppConfigurer implements WebMvcConfigurer {/ * SpringBoot setup homepage * / @ Override public void addViewControllers (ViewControllerRegistry registry) {WebMvcConfigurer.super.addViewControllers (registry) with the new class mentioned on the official website. Registry.addViewController ("/") .setViewName ("index"); registry.setOrder (Ordered.HIGHEST_PRECEDENCE);}} visit http://localhost:8088/springboot

Although jsp technology is not officially recommended by spring boot, considering that it is a commonly used technology, here is also the integration of jsp technology. First of all, you need to add the webapp standard web project folder to your project.

Configure prefixes and suffixes for jsp

Refer to the header 'Note'

Return to the view in controller

Same as the third step of freemaker

Configure the home page

Same as the fourth step of freemaker

Visit http://localhost:8088/springboot

Be careful

If the freemarker template engine and jsp technology exist at the same time, springmvc will return the specific view according to the priority of the parser. By default, the priority of FreeMarkerViewResolver is higher than that of InternalResourceViewResolver, so if it exists at the same time, it will return the freemarker view.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report