운영에 있어서 http와 https 포트 두 개를 모두 열기 위함!

"멀티 포트"

 

 

/config/ServletConfig 파일 설정

@Configuration
public class ServletConfig {

  @Value("${server.http.port}")
  private int httpPort;
  @Value("${server.port}")
  private int httpsPort;
  
  @Bean
  public ServletWebServerFactory servletWebServerFactory() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
      @Override
      protected void postProcessContext(Context context) {
        SecurityConstraint securityConstraint = new SecurityConstraint();
        securityConstraint.setUserConstraint("CONFIDENTIAL");
        SecurityCollection collection = new SecurityCollection();
        collection.addPattern("/*");
        securityConstraint.addCollection(collection);
        context.addConstraint(securityConstraint);
      }
    };

    tomcat.addAdditionalTomcatConnectors(createStandardConnector());
    return tomcat;
  }

  private Connector createStandardConnector() {
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    connector.setPort(httpPort);
    connector.setScheme("http");
    connector.setSecure(false);
    connector.setRedirectPort(httpsPort);
    return connector;
  }
}

 

properties 설정

server.port=443
server.http.port=80

#SSL 설정
server.ssl.enabled=true
server.ssl.key-store=
server.ssl.key-store-type=
server.ssl.ciphers=
server.ssl.key-store-password=
server.ssl.key-alias= 도메인

'Web > Spring' 카테고리의 다른 글

[MultipartFile] 첨부파일 다운로드  (0) 2021.08.06
[MultipartFile] 첨부파일 업로드  (0) 2021.08.06
[ERROR] 유의해야 할 점  (0) 2021.08.06
[Annotation] @PathVariable  (0) 2021.08.06
[Pageable] 페이징 처리 - 기본  (0) 2021.08.06
  • filter를 사용할 땐 검색할 때만!!!(페이지 리스트 찾을 때) → 시도 때도 없이 찾으면 속도가 느려진다..
  • html은 포맷팅(ctrl+d)을 피하자!! 가독성이 떨어진다

"URL 경로에 변수를 넣어주는 것" "RESTful 서비스의 URI 형태"

 

사용법

@GetMapping({"/edit", "/edit/{id}"})
  public String edit(@PathVariable(required = false) Integer id, Model model) {

위와 같이 GetMapping에서 id가 필요할 경우에만 사용할 땐 (required = false)를 붙인다.

그럼 id가 들어오면 /edit/{id}를 주고 id가 없으면 /edit을 준다.

 

 @GetMapping("/read/{id}")
  public String read(@PathVariable Integer id, Model model) {

위는 id가 필수적으로 필요하다.

 

❗ id가 없을 때도 잘 받기 위해선 view 쪽에 hidden input으로 id를 설정해주면 알아서 처리해줌... ❗

<form method="post" th:action="|@{/project/save}|" th:object="${project}">
        <input th:field="*{id}" type="hidden"/>

/repository/support안에 impl 파일들

public class ProjectRepositoryImpl extends QuerydslRepositorySupport implements
    CustomProjectRepository {

위와 같이 QuerydslRepositorySupport를 extends 한다. 안에 필요한 함수들을 구현해주면 된다.

 

 @Override
  public Page<Project> findAllByFilter(Pageable pageable, ProjectFilter filter) {
    BooleanBuilder builder = new BooleanBuilder();

    if (!StringUtils.isBlank(filter.getDate())) {
      builder.and(project.date.containsIgnoreCase(filter.getDate()));
    }
    if (!StringUtils.isBlank(filter.getTitle())) {
      builder.and(project.title.containsIgnoreCase(filter.getTitle()));
    }
    if(!StringUtils.isBlank(filter.getAddress())){
      builder.and(project.address.containsIgnoreCase(filter.getAddress()));
    }
    if(!StringUtils.isBlank(filter.getTechnology())){
      builder.and(project.technology.containsIgnoreCase(filter.getTechnology()));
    }
    if(!StringUtils.isBlank(filter.getAddress())){
      builder.and(project.purpose.containsIgnoreCase(filter.getPurpose()));
    }

    final JPQLQuery<Project> query = from(project).where(builder);

    List<Project> result = getQuerydsl().applyPagination(pageable, query).fetch();

    return new PageImpl<>(result,pageable,query.fetchCount());

  }

위와 같이 원하는 정보들을 모아서 paging 해준다.

 

물론 /repository/custom 과 /repository 설정은 기본적으로 해준 상태에서 진행해야한다!!!

 

그 후 controller에 pageable 활용

  @GetMapping("")
  public String list(Model model, Pageable pageable, ProjectFilter filter) {

    model.addAttribute("projectList", projectService.findAllByFilter(pageable, filter));
    model.addAttribute("filter", filter);
    return "project/list";
  }

 

❗ paging 원리에 대해선 나중에 다뤄봐야겠다.. ❗

'Web > Spring' 카테고리의 다른 글

[ERROR] 유의해야 할 점  (0) 2021.08.06
[Annotation] @PathVariable  (0) 2021.08.06
[배경] JPA vs JDBC, JPA vs Mybatis, JPA vs Spring Data JPA  (0) 2021.08.06
[설정] 자동망치 기능  (0) 2021.08.06
[설정] application.properties  (0) 2021.08.04

View에 데이터 전달

  • Model 객체 사용
  @GetMapping("")
  public String list(Model model, Pageable pageable, ProjectFilter filter) {

    model.addAttribute("projectList", projectService.findAllByFilter(pageable, filter));
    model.addAttribute("filter", filter);
    return "project/list";
  }

간단히 controller 생성

 

<tr th:each="p: ${projectList}">
          <td scope="row" style="width: 50px"><a th:text="${p.id}"
                                                 th:href="|@{/project/read}/${p.id}|"></a></td>
          <td><a th:text="${p.date}" th:href="|@{/project/read}/${p.id}|"></a></td>
          <td><a th:text="${p.title}" th:href="|@{/project/read}/${p.id}|"></a></td>

model로 받아온 projectList 활용

 

 

  • @ModelAttribute("key") 활용

controller의 함수 위에 @ModelAttribute("원하는 key")를 붙여서 사용

파라미터 안에 선언해줘도 가능

 

❗ 나는 주로 객체를 활용할 듯.. ❗

JDBC

  • JDBC는 DB에 접근하고, SQL을 날릴 수 있게 해주는 자바의 표준 API
  • DriverMAnager를 사용해 각 드라이버들을 로딩, 해제한다.

[출처] https://skyblue300a.tistory.com/7

JPA

  • JPA는 자바 진영 ORM의 API 표준 명세
  • 내부적으로 JDBC를 사용

[출처] https://skyblue300a.tistory.com/7

Spring JDBC(SQL Mapper → MyBatis)

  • JDBC에서 DriveManager가 하는 일들을 JdbcTemplate에게
  • 쿼리문을 사용한다.

[출처] https://skyblue300a.tistory.com/7

Spring Data JDBC

  • Spring data는 Spring에서 DB를 쉽게 다루기 위해 시작한 프로젝트
  • @Query 어노테이션 사용

[출처] https://skyblue300a.tistory.com/7

Hibernate

  • JPA를 구현한 프레임워크
  • Spring은 기본 JPA vendor로 Hibernate를 사용

[출처] https://skyblue300a.tistory.com/7

정리

 

 

❗ 나는 Hibernate를 사용해 구현할 예정..!!!! ❗

'Web > Spring' 카테고리의 다른 글

[Annotation] @PathVariable  (0) 2021.08.06
[Pageable] 페이징 처리 - 기본  (0) 2021.08.06
[설정] 자동망치 기능  (0) 2021.08.06
[설정] application.properties  (0) 2021.08.04
[CRUD] R구현  (0) 2021.08.02

+ Recent posts