Springboot 빠 른 입문 강좌
9075 단어 Springboot사용법
프로젝트 를 IDEA 에서 만 들 면 됩 니 다.
주의 점:
1.모든 파일 은 다음 과 같이 보관 해 야 합 니 다.
응용 프로그램 파일 의 동급 또는 하급 디 렉 터 리 중
2.application.properties 는 spring-boot 입 니 다. 프로젝트 의 주 핵심 프로필 이 있 고 핵심 프로필 만 있 을 수 있 습 니 다.
3.다 중 환경 에서 의 핵심 프로필 사용, 파일 이름 application- 시작!
application-xxx.properties
(1)개발 환경
#
server.port=9000
server.servlet.context-path=/
(2)테스트
#
(3)생산 환경
#
server.port=7000
주 핵심 프로필 에서 사용자 정의 프로필 을 활성화 합 니 다:
# application-xxx.properties
spring.profiles.active=dev
4.@Value 주석spring-boot 핵심 프로필 사용자 정의 설정 속성 을 가 져 오 는 방법
아래 방법 은 하나의 속성 만 획득 할 수 있 습 니 다!
예 를 들 어 application.properties 파일 에서 웹 사이트 설정 을 사용자 정의 하 였 습 니 다=http://www.baidu.com
프로젝트 에서 이 사용자 정의 설정 을 가 져 옵 니 다:
주석@Value 사용 하기("${website}")
기본 값 을 쓸 수도 있 습 니 다.설정 항목 이 없 으 면 기본 값@Value("${website:기본 값}")를 사용 합 니 다.
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;
//
@Controller
public class IndexController {
@Value("${website:values}")
private String name; // website name
@RequestMapping(value = "/self")
@ResponseBody
public String self() {
return name;
}
}
5.@Component 와@ConfigurationProperties(prefix="xxx")주석spring-boot 핵심 프로필 은 사용자 정의 설정 속성 을 대상 으로 표시 합 니 다.(가 져 온 것 은 대상 입 니 다)이러한 방식 의 전 제 를 사용 합 니 다.설정 파일 의 속성 은 접 두 사 를 써 야 합 니 다!
application.properties 파일
# , user
user.name=lxc
user.password=123456
config->user.java 파일
package com.lxc.sprint_boot_01.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component // spring
@ConfigurationProperties(prefix = "user") // , ,
// ,
public class User {
private String username;
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;
}
}
호출 속성
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;
//
@Controller
public class IndexController {
@Autowired // @Autowired User
private User user;
@RequestMapping(value = "/many")
@ResponseBody
public String many() {
return "user :"+user.getUsername() + ", :"+user.getPassword();
}
}
6.@ConfigurationProperties 주 해 를 더 하면 위 에 빨간색 경고 가 나타 납 니 다.이 문 제 를 해결 하려 면 의존 팩 을 추가 해 야 합 니 다.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
7.application.properties 에 중국어 가 있 으 면 오류 가 발생 하고 IDEA 에서 중국어 오류 문 제 를 해결 합 니 다.8.설정 파일 에 있 는 속성의 키 값 은 빈 칸 이 있 으 면 안 됩 니 다.그렇지 않 으 면 해석 에 문제 가 있 습 니 다!
9.spring-boo 통합 JSP
먼저 main 폴 더 아래 웹 app 폴 더 를 만 든 다음 클릭 하 다. file -> project structure -> Modules 다음 그림:
그리고 팝 업 대화 상자 에서 오른쪽 파일 을 누 르 면 우리 가 방금 만 든 웹 앱 폴 더 를 찾 을 수 있 습 니 다.확인 하면 됩 니 다.구체 적 으로 다음 과 같 습 니 다.
이때 웹 앱 은 다음 과 같이 변 합 니 다.
pom.xml 파일 설정
(1)먼저 spring-boot 에 설 치 된 tomcat 를 도입 하여 jsp 에 대한 분석 의존 도 를 추가 하지 않 으 면 jsp 를 해석 할 수 없습니다.
<!-- spring-boot tomcat jsp , jsp-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
(2)spring-boot 는 기본적으로 전단 엔진 thymeleaf 를 사용 합 니 다.지금 은 springboot 를 사용 하여 jsp 를 계승 하려 면 jsp 의 마지막 컴 파일 경 로 를 수 동 으로 지정 해 야 합 니 다.또한 springboot 가 jsp 를 계승 하 는 경 로 는 springboot 이 정 한 위치 입 니 다.META-INF/resources
<build>
<!--spring-boot thymeleaf, springboot jsp, jsp , springboot jsp springboot :META-INF/resources-->
<resources>
<resource>
<!-- -->
<directory>src/main/webapp</directory>
<!-- :-->
<targetPath>META-INF/resources</targetPath>
<!-- -->
<includes>
<include>*.*</include>
</includes>
</resource>
</resources>
<plugins>
<!-- ・・・ -->
</plugins>
</build>
마지막 단계:application.properties 에 보기 해상도 설정
#
spring.mvc.view.prefix=/ #
spring.mvc.view.suffix=.jsp #
.jsp 페이지 만 들 기,테스트:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>${msg}</h1>
</body>
</html>
package com.lxc.boot_02;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class controller {
// :
@RequestMapping(value="/say")
public ModelAndView say() {
ModelAndView mv = new ModelAndView();
//
mv.addObject("msg", "hello");
//
mv.setViewName("say");
return mv;
}
// : , (return )
@RequestMapping(value = "/index")
public String index(Model model) {
model.addAttribute("msg", "lxc;");
return "say";
}
}
쓰기 1:쓰기 2:
Springboot 의 빠 른 입문 튜 토리 얼 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 입문 Springboot 내용 은 저희 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 도 많은 응원 부 탁 드 리 겠 습 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[MeU] Hashtag 기능 개발➡️ 기존 Tag 테이블에 존재하지 않는 해시태그라면 Tag , tagPostMapping 테이블에 모두 추가 ➡️ 기존에 존재하는 해시태그라면, tagPostMapping 테이블에만 추가 이후에 개발할 태그 기반 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.