In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "case Analysis of getting started with Springboot". The editor shows you the operation process through the actual case. The method of operation is simple and fast, and it is practical. I hope this article of "case Analysis of getting started with Springboot" can help you solve the problem.
Getting started with Springboot
Project creation can be created in IDEA.
Note:
1. All files need to be placed in:
In the sibling or subordinate directory of the Application file
2. Application.properties is the main core configuration file of the spring-boot project, and there can only be one core configuration file.
3. For the use of core configuration files in multiple environments, the file name must start with application-!
Application-xxx.properties
(1) Development environment
# Development environment configuration file server.port=9000server.servlet.context-path=/
(2) Test
# Test environment configuration file
(3) production environment
# production environment configuration file server.port=7000
Activate our custom profile in the main core profile:
# activate the application-xxx.properties configuration file spring.profiles.active=dev that we wrote
4. @ Value comment
Spring-boot core configuration file customized configuration properties, how to obtain
The following way can only be obtained one by one attribute!
For example: customize a configuration website= http://www.baidu.com in the application.properties file
Get this custom configuration in the project:
Use the annotation @ Value ("${website}")
You can also write a default value. If the configuration item is not available, the default value @ Value ("${website: default}") will be used.
Package com.lxc.sprint_boot_01.web; import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody; import javax.management.ValueExp;import javax.print.DocFlavor; / / declare the control layer @ Controllerpublic class IndexController {@ Value ("${website:values}") private String name / / at this point, the website value will be assigned to the name attribute @ RequestMapping (value = "/ self") @ ResponseBody public String self () {return name;}}
5. @ Component and @ ConfigurationProperties (prefix= "xxx") comments
The spring-boot core configuration file maps our custom configuration properties to an object (getting an object). The premise of using this way: the properties in the configuration file must be prefixed!
Application.properties file
# attribute must be preceded by a prefix. Here the prefix is useruser.name=lxcuser.password=123456.
Config-> user.java file
Package com.lxc.sprint_boot_01.config; import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component; @ Component / / submit this class to spring container management @ ConfigurationProperties (prefix = "user") / / configure attribute comments. The parameter prefix must have a value, and the value is the prefix / / defined by us. After configuring the above two comments, the following maps the attributes in the configuration file to public class User {private String username in the following class. Private String password; public String getUsername () {return username;} public void setUsername (String username) {this.username = username;} public String getPassword () {return password;} public void setPassword (String password) {this.password = password;}}
Call attribute
Package com.lxc.sprint_boot_01.web; import com.lxc.sprint_boot_01.config.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody; import javax.management.ValueExp;import javax.print.DocFlavor;import java.util.HashMap;import java.util.Map / / declare that the control layer @ Controllerpublic class IndexController {@ Autowired / / @ Autowired injects the User class into private User user; @ RequestMapping (value = "/ many") @ ResponseBody public String many () {return "user is:" + user.getUsername () + "with the password:" + user.getPassword ();}}
6. Add the @ ConfigurationProperties annotation and the above red warning will appear. To solve this problem, you need to add a dependency package:
Org.springframework.boot spring-boot-configuration-processor
7. If there is Chinese in application.properties, garbled codes will occur, and the problem of garbled Chinese will be solved in IDEA:
8. The key-value pairs of attributes in the configuration file cannot have spaces, otherwise there will be problems with parsing!
9. Spring-boo integrates JSP
First, create a webapp folder under the main folder, and then click file-> project structure-> Modules as shown below:
Then click on the file on the right in the pop-up dialog box, find the webapp folder we just created, and make sure, as follows:
At this point, the webapp looks like this.
Configure the pom.xml file
(1) first, the parsing dependence of tomcat embedded in spring-boot on jsp is introduced, and jsp cannot be parsed without adding parsing.
Org.apache.tomcat.embed tomcat-embed-jasper
(2) spring-boot uses the front-end engine thymeleaf by default. Now if we want to use springboot to inherit jsp, we need to manually specify the path to the final compilation of jsp, and the path where springboot inherits jsp is the location specified by springboot: META-INF/resources.
Src/main/webapp META-INF/resources *. *
Final step: configure the view parser in application.properties
# configure the view parser spring.mvc.view.prefix=/ # prefix spring.mvc.view.suffix=.jsp # suffix
Create a .jsp page and test:
Title ${msg} package com.lxc.boot_02; import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView; @ Controllerpublic class controller 1: @ RequestMapping (value= "/ say") public ModelAndView say () {ModelAndView mv = new ModelAndView (); / / pass the value mv.addObject ("msg", "hello") to the view / / set the name of the final view mv.setViewName ("say"); return mv;} / / write method 2: split the view from the model and return a view (return is the name of the view) @ RequestMapping (value = "/ index") public String index (Model model) {model.addAttribute ("msg", "lxc;"); return "say";}}
Write method 1:
Writing method 2:
This is the end of the introduction to "case Analysis of getting started with Springboot". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.