[Spring Security] 온라인 시험 사이트 구현(3. thymeleaf를 활용한 화면 작업)

WOOK JONG KIM·2022년 12월 8일
0

패캠_java&Spring

목록 보기
92/103
post-thumbnail

메인 화면

서브 화면

학생 화면


코드 예시

html 파일은 github에서 보기

HomeController

@Controller
public class HomeController {


    @GetMapping("/")
    public String home(Model model){
        model.addAttribute("name", "jongwon2");
        return "index";
    }

    @GetMapping("/login")
    public String login(@RequestParam String site, Model model){
        model.addAttribute("site", site);

        return "loginForm";
    }

    @PostMapping("/login")
    public String loginPost(@RequestParam String site, Model model){
        model.addAttribute("site", site);

        return "redirect:/"+site;
    }

    @GetMapping("/signup")
    public String signUp(@RequestParam String site){

        return "redirect:/"+site+"/signup";
    }

}

@Controller
@RequestMapping(value="/manager")
public class ManagerController {


    @GetMapping({"", "/"})
    public String index(){

        return "/manager/index";
    }

}
@Controller
@RequestMapping("/manager/school")
@RequiredArgsConstructor
public class SchoolMngController {

    private final SchoolService schoolService;

    private final UserService userService;

    @GetMapping("/list")
    public String list(
            @RequestParam(value="pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value="size", defaultValue = "10") Integer size,
            Model model
    ){
        model.addAttribute("menu", "school");

        return "manager/school/list.html";
    }

    @GetMapping("/edit")
    public String list(
            @RequestParam(value="schoolId", required = false) Long schoolId,
            Model model
    ){
        model.addAttribute("menu", "school");
        model.addAttribute("school", School.builder().build());
        return "manager/school/edit.html";
    }

    @PostMapping(value = "/save", consumes = {"application/x-www-form-urlencoded;charset=UTF-8", MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    public String save(School school){
        schoolService.save(school);
        return "redirect:/manager/school/list.html";
    }

}
@Controller
@RequestMapping("/manager/study")
@RequiredArgsConstructor
public class StudyMngController {

    private final UserService userService;

    @GetMapping("/list")
    public String list(
            @RequestParam(value="pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value="size", defaultValue = "10") Integer size,
            Model model
    ){
        model.addAttribute("menu", "study");

        return "manager/study/list.html";
    }
}
@Controller
@RequestMapping("/manager/teacher")
@RequiredArgsConstructor
public class TeacherMngController {

    private final UserService userService;

    @GetMapping("/list")
    public String list(
            @RequestParam(value="pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value="size", defaultValue = "10") Integer size,
            Model model
    ){
        model.addAttribute("menu", "teacher");

        return "manager/teacher/list.html";
    }

}

@Controller
@RequestMapping(value="/study")
public class StudyController {

    @Autowired
    private PaperTemplateService paperTemplateService;

    @Autowired
    private PaperService paperService;

    @RequestMapping({"", "/"})
    public String  index(@AuthenticationPrincipal User user, Model model){

        // 학생수와 문제지 수
        model.addAttribute("paperCount", 1);
        model.addAttribute("resultCount", 1);


        return "/study/index";
    }

    @GetMapping("/signup")
    public String signUp(){
        return "/study/signup";
    }

    private User user(){
        return User.builder()
                .userId(1L)
                .name("홍길동")
                .email("hong@test.com")
                .grade("3")
                .enabled(true)
                .school(School.builder().schoolId(1L).name("테스트 학교").city("서울").build())
                .build();
    }

    private PaperTemplate paperTemplate(){
        return PaperTemplate.builder()
                .paperTemplateId(1L)
                .name("테스트 시험지")
                .creator(user())
                .userId(1L)
                .publishedCount(1)
                .build();
    }

    private List<Paper> paperList(){
        return List.of(Paper.builder()
                .name("테스트 시험지")
                .paperTemplateId(1L)
                .state(Paper.PaperState.START)
                .total(2)
                .paperId(1L)
                .studyUserId(1L)
                .user(user())
                .build());
    }

    // 시험지 리스트
    @GetMapping("/papers")
    public String paperList(@AuthenticationPrincipal User user, Model model){
        model.addAttribute("menu", "paper");
        model.addAttribute("papers", paperList());
        return "/study/paper/papers.html";
    }

    @GetMapping("/results")
    public String results(
            @RequestParam(value="pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value="size", defaultValue = "10") Integer size,
            @AuthenticationPrincipal User user, Model model
    ){
        model.addAttribute("menu", "result");
        model.addAttribute("page",
                new PageImpl(paperList())
        );
        return "/study/paper/results.html";
    }


    // 시험 보기
    @GetMapping(value="/paper/apply")
    public String applyPaper(@RequestParam Long paperId, @AuthenticationPrincipal User user, Model model){
        model.addAttribute("menu", "paper");

        model.addAttribute("paperId", paperId);
        model.addAttribute("paperName", "테스트 시험지");

        model.addAttribute("problem", Problem.builder()
                .content("문제")
                .answer("정답")
                .indexNum(1)
                .paperTemplateId(1L)
                .build());
        model.addAttribute("alldone", false);

        return "/study/paper/apply.html";
    }

    /**
     * TODO : 다른 사람이 풀수도 있음. 아이디를 확인해야 함.
     *
     * @return
     */
    // 정답 제출
    @PostMapping(value="/paper/answer", consumes = {"application/x-www-form-urlencoded;charset=UTF-8", MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    public String answer(Answer answer, @AuthenticationPrincipal User user, Model model){

        return "redirect:/study/paper/apply.html?paperId="+answer.getPaperId();
    }

    // 시험 완료
    @GetMapping("/paper/done")
    public String donePaper(Long paperId){

        return "redirect:/study/paper/result.html?paperId="+paperId;
    }

    // 결과 시험지 리스트
    @GetMapping("/paper/result")
    public String paperResult(Long paperId, @AuthenticationPrincipal User user, Model model){
        model.addAttribute("menu", "result");
        model.addAttribute("paper", paperList().get(0));
        return "/study/paper/result.html";
    }
}

@Controller
@RequestMapping(value="/teacher")
@RequiredArgsConstructor
public class TeacherController {

    private final UserService userService;
    private final PaperTemplateService paperTemplateService;
    private final PaperService paperService;

    @RequestMapping({"", "/"})
    public String  index(@AuthenticationPrincipal User user, Model model){

        // 학생수와 문제지 수
        model.addAttribute("studentCount", 1);
        model.addAttribute("paperTemplateCount", 1);
        return "/teacher/index";
    }

    @GetMapping("/signup")
    public String signUp(){
        return "/teacher/signup";
    }

    private User user(){
        return User.builder()
                .userId(1L)
                .name("홍길동")
                .email("hong@test.com")
                .grade("3")
                .enabled(true)
                .school(School.builder().schoolId(1L).name("테스트 학교").city("서울").build())
                .build();
    }

    private PaperTemplate paperTemplate(){
        return PaperTemplate.builder()
                .paperTemplateId(1L)
                .name("테스트 시험지")
                .creator(user())
                .userId(1L)
                .publishedCount(1)
                .build();
    }

    private List<Paper> paperList(){
        return List.of(Paper.builder()
                .name("테스트 시험지")
                .paperTemplateId(1L)
                .state(Paper.PaperState.START)
                .total(2)
                .paperId(1L)
                .studyUserId(1L)
                .user(user())
                .build());
    }

    @GetMapping("/study/list")
    public String studyList(@AuthenticationPrincipal User user, Model model){
        model.addAttribute("menu", "study");
        model.addAttribute("studyList", List.of(user()));
        return "/teacher/study/list.html";
    }

    /**
     * 시험을 본 학생과 보지 않은 학생 리스트와 결과... 리스트
     *
     * @param paperTemplateId
     * @param user
     * @param model
     * @return
     */
    @GetMapping("/study/results")
    public String studyResults(
            @RequestParam Long paperTemplateId,
            @AuthenticationPrincipal User user,
            Model model
    ){
        model.addAttribute("menu", "paper");

        model.addAttribute("template", paperTemplate());
        model.addAttribute("papers", paperList());
        return "/teacher/study/results.html";
    }

    @GetMapping("/paperTemplate/list")
    public String paperTemplateList(
            @RequestParam(value="pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value="size", defaultValue = "10") Integer size,
            @AuthenticationPrincipal User user,
            Model model
    ){
        model.addAttribute("menu", "paper");
        model.addAttribute("page", new PageImpl(List.of(paperTemplate())));
        return "/teacher/paperTemplate/list.html";
    }

    @GetMapping("/paperTemplate/create")
    public String editPaperTemplateName(@AuthenticationPrincipal User user, Model model){

        return "/teacher/paperTemplate/create.html";
    }

    @PostMapping(value="/paperTemplate/create", consumes = {"application/x-www-form-urlencoded;charset=UTF-8", MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    public String createAndEditTemplate(@RequestParam String paperName, @AuthenticationPrincipal User user, Model model){
        model.addAttribute("template", paperTemplate());
        return "/teacher/paperTemplate/edit.html";
    }

    @GetMapping("/paperTemplate/edit")
    public String editPaperTemplate(
            @RequestParam Long paperTemplateId,
            @AuthenticationPrincipal User user,
            Model model
    ){
        model.addAttribute("template", paperTemplate());
        return "/teacher/paperTemplate/edit.html";
    }

    @PostMapping(value="/paperTemplate/problem/add", consumes = {"application/x-www-form-urlencoded;charset=UTF-8", MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    public String addProblemToPaperTemplate(
            ProblemInput problemInput,
            @AuthenticationPrincipal User user, Model model){
        model.addAttribute("template", paperTemplate());
        return "/teacher/paperTemplate/edit.html";
    }

    /**
     * 시험지 배포
     * @return
     */
    @GetMapping("/paperTemplate/publish")
    public String publishPaper(
            @RequestParam Long paperTemplateId,
            @AuthenticationPrincipal User user, Model model
    ){

        return "redirect:/teacher/paperTemplate/list.html";
    }
}
profile
Journey for Backend Developer

0개의 댓글