2일(08.18) MCV TestController
💾 Test1Controller 통해서 요청을 하고 요청을 받아서 실행
Controller생성: src/main/java - com.spring.ex - Test1Controller(java class 로 생성)
JSP파일 생성: src- main- webapp- WEB-INF- views- test1 (test1 폴더에 test1Formjsp , result.jsp 파일 생성한다.)
test1Form.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
test1/test1Form.jsp<br>
<form action="test1/join1" method="get">
<table border="1">
<tr>
<td>name</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="전송">
</td>
</tr>
</table>
</form>
result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
test1/result.jsp<br>
<!-- 파라미터로 넘겨받는다. -->
name1 : ${param.name}<br>
name2 : <%=request.getParameter("name") %><br>
Test1Controller.java
package com.spring.ex;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Test1Controller {
@RequestMapping("form")
public String tes1(Model model) {
model.addAttribute("name","kim");
return "test1/test1Form";
}
@RequestMapping("test1/join1")
public String join1() {
return "test1/result";
}
}
@RequestMapping("form") - form을 요청하면 test1/test1Form 으로 이동된다.
public String tes1(Model model) {
model.addAttribute("name","kim"); -이름을 kim으로 속성 설정한다.
return "test1/test1Form";
}
http://localhost:8080/ex/form 요청
test1/test1Form.jsp , submit 클릭 - test1/join1 요청
http://localhost:8080/ex/test1/join1?name=sss -설정한 이름의 값을 가지고 test1/result 이동한다.
@RequestMapping("test1/join1")
public String join1() {
return "test1/result";
}
💾 Test2Controller 통해서 요청을 하고 요청을 받아서 실행
Controller생성: src/main/java - com.spring.ex - Test2Controller(java class 로 생성)
JSP파일 생성: src- main- webapp- WEB-INF- views- test1 (test2 폴더에 test2Formjsp , result.jsp 파일 생성한다.)
test2Form.jsp
<form action="join2" method="get">
<table border="1">
<tr>
<td>name</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="전송">
</td>
</tr>
</table>
</form>
<form action="join2" method="get">
http://localhost:8080/ex/test2/join2?name=sdsdf
action에 test2 요청명을 지웠기때문에 경로가 제대로 이루어져있다.
test2를 두번 인식하여 주소에 test2가 두번씩 나오게 되어 경로가 잘못되어 에러가 발생하게 된다.
result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
test2/result.jsp<br>
<!-- 파라미터로 넘겨받는다. -->
name1 : ${param.name}<br>
name2 : <%=request.getParameter("name") %><br>
Test2Controller.java
package com.spring.ex;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Test2Controller {
@RequestMapping("test2/form2")
public String form() {
return "test2/test2Form";
}
@RequestMapping("test2/join2")
public String join1() {
return "test2/result";
}
}
@RequestMapping("test2/form2")
public String form() {
return "test2/test2Form";
}
- http://localhost:8080/ex/test2/form2 요청 → test2/test2Form.jsp이동
- test2/test2Form.jsp , submit 클릭 test2/join2 요청
// http://localhost:8080/ex/test2/join2?name=sdsdf
// http://localhost:8080/ex/test2/test2/join2?name=sdsdf
@RequestMapping("test2/join2") //여기에 적힌 test2는 요청명
public String join1() {
return "test2/result"; //여기에 적힌 test2는 폴더명이다.
}