티스토리 뷰
http://millky.com/@origoni/post/1100
1. New - Spring boot - Spring Stater Project 로 프로젝트 생성
- 기본정보
Spring boot 2.1.5
gradle buildship 2.x
(PRJ)name 과 artifact (name) 을 맞춰야 함. 안맞으면 오류로 종료
package = war
- dependencies
mysql
Spring data jpa
Spring web
Java 1.8
# 참고 : 모 프로젝트 생성시의 옵션선택 Full URL
2. application.properties 설정
위치 : 프로젝트/src/main/resources/application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/dbName
spring.datasource.username=root
spring.datasource.password=****
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.mvc.view.prefix: /WEB-INF/views/
spring.mvc.view.suffix: .jsp
서버 포트 바꾸고 싶다면
server.port = 8080
- DB 속성 추가 안해주면 다음 오류 발생함
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
https://devtimothy.tistory.com/100
3. html 은
src/main/resources/static 에 넣으면 작동
4. DAO 추가 및 엔티티 추가
@Repository
public interface KtcDao extends JpaRepository<KtCate, Long> {
...
}
@Entity
public class KtCate {
@Id
@GeneratedValue
public Long id;
...
}
5. Application 클래스에 다음 어노테이션 추가
@SpringBootApplication(scanBasePackages={"Bean있는패키지"})
@EnableJpaRepositories(basePackages = "DAO패키지")
@EntityScan("엔티티패키지")
[ JSP View 연결 ]
6. Controller 추가
@Controller
public class KtController {
@Autowired
KtcDao ktcDao;
@RequestMapping("/hello")
public String hello(Model model, @RequestParam(value="name", defaultValue="Unknown", required=false) String name) {
String s = "Hello, "+name + " ! " ;
model.addAttribute("str", s);
return "/hello";
}
7. JSP 추가
/프로젝트/src/main/webapp/WEB-INF/views 에
JSP 추가 (폴더 없으면 생성)
의존성 다음 추가
compile('org.apache.tomcat.embed:tomcat-embed-jasper')
compile('javax.servlet:jstl:1.2')
( + refresh)
* JSP 못 읽는 경우
https://dogcowking.tistory.com/326
참고 : JSP 는 Spring boot 1.3.5 이후 JAR 로 지원하지 않음
다른 View Template Engine (Velocity Thymeleaf3, Freemarker)
https://supawer0728.github.io/2018/03/14/Mustache/
8. Front-end 관련 의존성 추가
jquery, bootstrap, mdbootstrap
compile("org.webjars:jquery:3.4.1")
compile("org.webjars:bootstrap:4.3.1")
compile("org.webjars.bower:origoni-startbootstrap-clean-blog:1.0.3")
사용
<script src="/webjars/jquery/3.4.1/jquery.min.js"></script>
<script src="/webjars/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link href="/webjars/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
<%
String mdb = baseUrl+"/webjars/mdbootstrap-bootstrap-material-design/4.7.4";
%>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css">
<!-- Bootstrap core CSS -->
<link href="<%=mdb %>/css/bootstrap.min.css" rel="stylesheet">
<!-- Material Design Bootstrap -->
<link href="<%=mdb %>/css/mdb.min.css" rel="stylesheet">
<!-- Bootstrap tooltips -->
<script type="text/javascript" src="<%=mdb %>/js/popper.min.js"></script>
<!-- Bootstrap core JavaScript -->
<script type="text/javascript" src="<%=mdb %>/js/bootstrap.min.js"></script>
<!-- MDB core JavaScript -->
<script type="text/javascript" src="<%=mdb %>/js/mdb.min.js"></script>
구동 했는데 Whitelabel Error Page 메시지 보이며 404 오류 나온다면...
https://dogcowking.tistory.com/326