저번에 작성했던 MVC 환경 설정을 따라하면 된다.
package com.example.springmvcform.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {
"com.example.springmvcform"
})
public class MVCConfig implements WebMvcConfigurer {
@Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
}
addResourceHandlers
가 왜 나오는 걸까? 우리가 HTML을 꾸미는 과정에서 필요한 css, js 요소를 가져오기 위하여 사용하게 되는 것이다. package com.example.springmvcform.model;
public class SignUpForm {
private String firstName;
private String lastName;
private String email;
private String userName;
private String password;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.example.springmvcform.controller;
import com.example.springmvcform.model.SignUpForm;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class SignUpController {
@ModelAttribute("signUpForm")
public SignUpForm setSignUpForm() {
return new SignUpForm();
}
@GetMapping("/showSignUpForm")
public String showForm() {
return "signup-form";
}
@PostMapping("/saveSignUpForm")
public String saveUser(@ModelAttribute("signUpForm") SignUpForm signUpForm, Model model) {
System.out.println("FirstName : " + signUpForm.getFirstName());
System.out.println("LastName : " + signUpForm.getLastName());
System.out.println("Username : " + signUpForm.getUserName());
System.out.println("Password : " + signUpForm.getPassword());
System.out.println("Email : " + signUpForm.getEmail());
model.addAttribute("message", "User SignUp successfully.");
model.addAttribute("user", signUpForm);
return "signup-success";
}
}
여기서 우리가 알아야 할점
@Controller와 @RestController를 구분해야 한다. 왜냐하면 @Controller는 흔히 jsp를 Return해주는 요소로 사용되고 @RestController는 Return 값이 json이기에 여기서 @RestController를 사용하게 되면 화면이 나오지 않고 string 값이 화면에 나오게 된다.
this application has no explicit mapping for /error, so you are seeing this as a fallback.
최종 코드 : spring-mvc-form