티스토리 뷰
실행 과정
1. 주소창에 http://~/xxxx 입력
2. DispatcherServlet → Controller : Controller에서 path가 /xxxx인 메소드 실행
3. Controller → DispatcherServlet : InternalResourceViewResolver가 가져온 return 값에 "/WEB-INF/views/"와 ".jsp"를 붙임
4. DispatcherServlet → View template : return 받은 "/WEB-INF/views/xxxx.jsp" 경로 파일 실행
(아래 DispatcherServlet 설정 참고)
DispatcherServlet(=WebMvcContextConfiguration.java) 설정
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "kr.or.connect.mvcexam.controller" })
public class WebMvcContextConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/META-INF/resources/webjars/").setCachePeriod(31556926);
registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(31556926);
registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(31556926);
registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(31556926);
}
// default servlet handler를 사용하게 합니다.
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addViewControllers(final ViewControllerRegistry registry) {
System.out.println("addViewControllers가 호출됩니다. ");
registry.addViewController("/").setViewName("main");
}
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
Controller 작성 방법1 - @GetMapping, @PostMapping을 이용한 Controller 작성
@Controller
public class PlusController {
@GetMapping(path="/plusform")
public String plusform() {
return "plusForm"; //파일명과 일치
}
@PostMapping(path="/plus")
public String plus(
@RequestParam(name = "value1", required = true) int value1,
@RequestParam(name = "value2", required = true) int value2,
ModelMap modelMap)
{
int result = value1 + value2;
modelMap.addAttribute("value1", value1);
modelMap.addAttribute("value2", value2);
modelMap.addAttribute("result", result);
return "plusResult"; //파일명과 일치
}
}
plusForm.jsp
<form method="post" action="plus">
value1 : <input type="text" name="value1"><br>
value2 : <input type="text" name="value2"><br>
<input type="submit" value="확인">
</form>
plusResult.jsp
${value1} 더하기 ${value2} (은/는) ${result} 입니다.
Controller 작성 방법2 - @RequestMapping을 이용한 Controller 작성
@Controller
public class UserController {
@RequestMapping(path="/userform", method=RequestMethod.GET)
public String userform() {
return "userform";
}
@RequestMapping(path="/regist", method=RequestMethod.POST)
public String regist(@ModelAttribute User user) {
System.out.println("사용자가 입력한 user 정보입니다. 해당 정보를 이용하는 코드가 와야합니다.");
System.out.println(user);
return "regist";
}
}
User.java
public class User {
private String name;
private String email;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", email=" + email + ", age=" + age + "]";
}
}
userform.jsp
<form method="post" action="regist">
name : <input type="text" name="name"><br>
email : <input type="text" name="email"><br>
age : <input type="text" name="age"><br>
<input type="submit" value="확인">
regist.jsp
<h2>등록되었습니다.</h2>
Controller 작성 방법3 - @PathVariable을 이용한 Controller 작성
@Controller
public class GoodsController {
@GetMapping("/goods/{id}")
public String getGoodsById(
@PathVariable(name="id") int id,
@RequestHeader(value="User-Agent", defaultValue="myBrowser") String userAgent,
HttpServletRequest request,
ModelMap model
) {
String path = request.getServletPath();
System.out.println("id : " + id);
System.out.println("user_agent : " + userAgent);
System.out.println("path : " + path);
model.addAttribute("id", id);
model.addAttribute("userAgent", userAgent);
model.addAttribute("path", path);
return "goodsById";
}
}
goodsById.jsp
id : ${id} <br>
user_agent : ${userAgent} <br>
path : ${path} <br>
참조
'Framework > Spring' 카테고리의 다른 글
트랜잭션(Transaction) (0) | 2021.01.10 |
---|---|
페이징 처리 (0) | 2021.01.07 |
Spring MVC 설정 (0) | 2021.01.06 |
Spring MVC (0) | 2021.01.06 |
Spring JDBC 실습 (0) | 2021.01.04 |
댓글