Spring

211118_form 데이터 읽기(기본)

요옫 2021. 11. 19. 09:41

servlet-context.xml 수정

 

pom.xml에서 버전 수정

 

web.xml에서 한글깨짐엔코딩 코드 추가

 

index.jsp (메인화면)

 

 

boardController.java

package spring.day1118.mouse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/board")  //반복되는 매핑주소를 한번만 써준후 나머지는 뒤의 주소값만 써주면 됨..대신 /board외의 주소는 불가 
public class BoardController {

@GetMapping("/form1")
public String form1() {  //단순 포워드할 거기에 string
return "board/writeform";  //servlet-context에서 WEB-INF뒤에 폴더 설정 안했으므로 폴더명/jsp파일명 구조로 포워드
}

@PostMapping("/process1")
public ModelAndView process1( //string,modelandview 둘 다 됨
@RequestParam String name,
@RequestParam String date,
@RequestParam String gender,
@RequestParam(name = "pageNum",defaultValue = "1") int currentPage,   //항목에 없는 값을 주려면 defaultValue도 줘야함
        @RequestParam(required = false) String msg)  //required = true가 기본값..항목이 없어도 에러 안나려면 false
{
ModelAndView model=new ModelAndView();
model.addObject("name", name);
model.addObject("date", date);
model.addObject("gender", gender);
model.addObject("currentPage", currentPage);
model.addObject("msg", msg);

//포워드
model.setViewName("board/result1");

return model;
}

@GetMapping("/result2")
public String result2(Model model) {

model.addAttribute("myimg1", "../image/01.png");
model.addAttribute("title", "미니언즈");
return "board/result2";
}
}

 

board/writeform

 

board/result1

 

board/result2

 

 

 

폼데이터읽기_기본
result2에서 이미지 확인

 

 

'Spring' 카테고리의 다른 글

211122_답변형게시판  (0) 2021.11.22
mybatis 세팅  (0) 2021.11.22
post방식 한글 encoding  (0) 2021.11.22