23-07-04 TIL

more·2023년 7월 4일
0

문제

  • 객체 전달 후 redirect 하는 방법
    • 팀 프로젝트를 시행 중인데, 객체를 return 하고 나서 redirect로 새로고침을 하는 방법이 필요해서 시도 중인데, return 후에 redirect 해주는 방법은 찾아봐도 나오지를 않는다.

시도

  • return 후 redirect 하는 방법을 잘 모르겠다. 그래서 두 가지를 생각해보았는데
    • 우선은 return 전에 redirect 경로를 넘겨주는 방식
      -> 이렇게 하면 결국 삭제나 수정 전에 redirect가 되는 거 같아서 아닌 것으로 보임
    • return 을 해주지 말고 redirect를 해주는 방식

해결

  • return 을 하지 말고 redirect를 해주자

    • controller에서 삭제나 수정에서의 타입을 void로 바꾸어서 service에서 실행은 하되 따로 리턴 값을 안 주는 방식으로 하고 controller에서는 redirect를 해주면 될 것으로 보인다.
    
    @DeleteMapping("/comments/{id}")
    public void deleteComment (@AuthenticationPrincipal UserDetailsImpl userDetails,
                               @PathVariable Long id,
                               HttpServletResponse response) throws IOException {
        commentService.deleteComment(id, userDetails.getUser());
        String redirect_uri= "http://localhost:8080/pettalk/comments";
        // redirect
        response.sendRedirect(redirect_uri);
    }
    • HttpServletResponse에 sendRedirect()라는 메서드가 있는데 이 메서드가 redirect 할 곳의 uri를 지정하는 메서드이다.
    • 위와 같이 해서 굳이 type을 리턴 받지 않고 redirect 해서 새로 고침을 하는 방식으로 시도해보았다.
      -> 나중에 팀원들과 코드를 합치고 나서 제대로 되는지 확인해보자.

오늘 푼 문제

  • 백준 1205 (등수 구하기) - Java

    • 저번에 풀었던 메달 수로 나라 등수 구하기보다 조금 더 간략화된 버전의 문제인 것 같다.
    • 배열을 내림차순으로 정렬해두고, 범위를 벗어났는지 확인하는 것이 관건으로 보인다.
    
    import java.io.*;
    import java.util.*;
    
    public class Main {
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    
            StringTokenizer st = new StringTokenizer(br.readLine());
    
            int N = Integer.parseInt(st.nextToken()), score = Integer.parseInt(st.nextToken()), P = Integer.parseInt(st.nextToken());
    
            ArrayList<Integer> arr = new ArrayList<>();
    
            if (N != 0) {
    
                st = new StringTokenizer(br.readLine());
    
                for (int i = 0; i < N; i++) {
    
                    arr.add(Integer.parseInt(st.nextToken()));
    
                }
    
                Collections.sort(arr, Collections.reverseOrder());
    
                if(N == P && score <= arr.get(arr.size()-1))    bw.write(-1 + "\n");
                else{
                    int count = 1;
                    for(int i = 0; i < arr.size(); i++){
                        if(score < arr.get(i))
                            count++;
                        else
                            break;
                    }
                    bw.write(count + "\n");
                }
            }
            else {
                bw.write(1 + "\n");
            }
    
            bw.flush();
            bw.close();
            br.close();
        }
    
    }
    
  • 백준 1244 (스위치 켜고 끄기) - Java

    • 왜 안될까 고민했는데, 20개 넘기면 줄바꾸는 코드를 수행할 때에 20개째가 끝나고 했어야 했는데 20개째면 바로 줄바꿈을 한 것이 문제였다.
    
    import java.io.*;
    import java.util.*;
    
    public class Main {
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    
            int number = Integer.parseInt(br.readLine());
    
            StringTokenizer st = new StringTokenizer(br.readLine());
    
            ArrayList<Integer> switchStatus = new ArrayList<>();
    
            switchStatus.add(0);
    
            for (int i = 0; i < number; i++) switchStatus.add(Integer.valueOf(st.nextToken()));
    
            int studentNumber = Integer.parseInt(br.readLine());
    
            for (int i = 0; i < studentNumber; i++) {
                st = new StringTokenizer(br.readLine());
                int sex = Integer.parseInt(st.nextToken());
                int position = Integer.parseInt(st.nextToken());
                if (sex == 1)   male (switchStatus, position);
                if (sex == 2) female (switchStatus, position);
            }
    
            for (int i = 1; i <= number; i++) {
    
                bw.write(switchStatus.get(i) + " ");
    
                if (i % 20 == 0)    bw.newLine();
            }
    
            bw.flush();
            bw.close();
            br.close();
        }
    		// 남자일 경우
        public static void male (ArrayList<Integer> switchStatus, int bae) {
            for (int i = 1; i < switchStatus.size(); i++) {
                if (i % bae == 0) {
                    changeSwitch(switchStatus, i);
                }
            }
        }
    		// 여자일 경우
        public static void female (ArrayList<Integer> switchStatus, int position) {
    
            int addPosition = changeFemale(switchStatus, position, 1);
    
            changeSwitch(switchStatus, position);
    
            for (int i = 1; i < addPosition; i++) {
                changeSwitch(switchStatus, position - i);
                changeSwitch(switchStatus, position + i);
            }
    
        }
    
    		// 여자일 경우, 배열 값 바꿔주는 함수
        public static int changeFemale (ArrayList<Integer> switchStatus, int position, int addPosition) {
    
            if (position - addPosition <= 0 || position + addPosition >= switchStatus.size())   return addPosition;
            else if (switchStatus.get(position - addPosition) == switchStatus.get(position + addPosition)) {
                return changeFemale(switchStatus, position, addPosition + 1);
            }
            else {
                return addPosition;
            }
        }
    
    			// 1이면 0, 0이면 1로 바꿔주는 함수
        public static void changeSwitch (ArrayList<Integer> switchStatus, int changePosition) {
            if (switchStatus.get(changePosition) == 1)   switchStatus.set(changePosition, 0);
            else switchStatus.set(changePosition, 1);
        }
    }
    

0개의 댓글