Spring에서 .xml 에서 메핑하지 않고
@을 사용해서 로그인 / 글 목록 출력 / 글 확인 / 글 작성
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
version="6.0">
<servlet>
<servlet-name>ds</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ds</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
ds-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:component-scan base-package="com.nsd.app.biz.member"/>
<context:component-scan base-package="com.nsd.app.biz.board"/>
<context:component-scan base-package="com.nsd.app.view.member"/>
<context:component-scan base-package="com.nsd.app.view.board"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
LoginController.java
package com.nsd.app.view.member;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.nsd.app.biz.member.MemberDAO;
import com.nsd.app.biz.member.MemberDTO;
import jakarta.servlet.http.HttpSession;
@Controller("login")
public class LoginController {
@RequestMapping("login.do")
public String login(){
return "redirect:login.jsp";
}
@RequestMapping("/loginCheck.do")
public String lognin(MemberDAO memberDAO, MemberDTO memberDTO,Model model, HttpSession session) throws Exception {
System.out.println(memberDTO);
System.out.println("model = "+model);
memberDTO = memberDAO.SelectOne(memberDTO);
System.out.println(memberDTO);
if(memberDTO!=null) {
session.setAttribute("mid", memberDTO.getMember_id());
model.addAttribute("error", "success");
model.addAttribute("title", "로그인에 성공");
model.addAttribute("msg", "로그인 되었습니다.");
model.addAttribute("path", "main.do");
}
else {
model.addAttribute("error", "error");
model.addAttribute("title", "로그인에 실패하셨습니다..ㅠ");
model.addAttribute("msg", "로그인 실패");
model.addAttribute("path", "login.do");
}
System.out.println(model);
return "info";
}
}
BoardViewPageController.java
package com.nsd.app.view.board;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.nsd.app.biz.board.BoardDAO;
import com.nsd.app.biz.board.BoardDTO;
@Controller("viewPage")
public class BoardViewPageController {
@RequestMapping("/main.do")
public String mainPage(BoardDAO boardDAO, Model model, BoardDTO boardDTO) throws Exception {
List<BoardDTO> datas = boardDAO.SelectAll(boardDTO);
model.addAttribute("datas", datas);
return "main";
}
@RequestMapping("/view.do")
public String handleRequest(BoardDAO boardDAO, BoardDTO boardDTO, Model model) throws Exception {
boardDTO = boardDAO.SelectOne(boardDTO);
model.addAttribute("data", boardDTO);
return "view";
}
}
BoardWriterController.java
package com.nsd.app.view.board;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.nsd.app.biz.board.BoardDAO;
import com.nsd.app.biz.board.BoardDTO;
import jakarta.servlet.http.HttpSession;
@Controller("writer")
public class BoardWriterController{
@RequestMapping("/write.do")
public String BoardWriterPage() throws Exception {
return "redirect:write.jsp";
}
@RequestMapping("/writeController.do")
public String handleRequest(HttpSession session, BoardDAO boardDAO, BoardDTO boardDTO, Model model) throws Exception {
String mid = (String)session.getAttribute("mid");
if(mid != null) {
boardDTO.setBoard_writer(mid);
boolean flag = boardDAO.insert(boardDTO);
if(flag) {
model.addAttribute("error", "success");
model.addAttribute("title", "글작성 성공!");
model.addAttribute("msg", "글작성에 성공하였습니다.");
}
else {
model.addAttribute("error", "error");
model.addAttribute("title", "글작성 실패!");
model.addAttribute("msg", "작성을 실패했습니다. \n 관리자에게 문의해주세요.");
}
model.addAttribute("path", "main.do");
}
else {
model.addAttribute("error", "error");
model.addAttribute("title", "로그인해주세요");
model.addAttribute("msg", "로그인 후 이용 가능한 서비스 입니다.");
model.addAttribute("path", "login.do");
}
return "info";
}
}
이전에 쓰던 .jsp / .java 로 했던 프로젝트를 보다
Spring을 배우니 코드가 점점 간결해지는 것이 눈에 보이니 신기한 것 같다 생각한다.
728x90
'국비 내용 정리 > 과제' 카테고리의 다른 글
[Spring] Board 게시판 검색 기능 제작 (3) | 2024.10.10 |
---|---|
중간 프로젝트 Spring 이관작업 (1) | 2024.10.09 |
[Spring] Page 작성 과제(BOARD 관련) (0) | 2024.10.05 |
[Spring] Page 작성 과제(MEMBER 관련) (0) | 2024.10.05 |
국비 34일차 과제 (0) | 2024.08.19 |