이번에 작성할 포스팅은 회원가입을 할때 필요한 페이지의 작성이며 주로 다루게 될 내용은 아래의 내용입니다.
Maven Project 와 webapp을 선택하여 Maven Project 생성
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SpringExample-SignUp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>springExample-signUp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<!-- JSTL Dependency -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- Servlet Dependency -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- JSP Dependency -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>springExample-signUp</finalName>
</build>
</project>
참고자료의 내용은 Eclipsed의 내용이지만 IntelliJ로 바꾸어 적용하였다.
package com.example.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"})
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/");
}
}
@Configuration : 설정파일을 만들기위한 annotation
@EnableWebMvc : Spring이 제공하는 웹과 관련된 최신 전략 Bean들이 등록
resolver.setViewClass(JstlView.class);
이코드는 사용할 view Class를 설정한다. 여기에서는 JstlView 클래스가 사용된다. 이 클래스는 JSTL 태그를 지원하는 JSP를 렌더링하는데 사용된다.
ResourceHandlerRegistry : 하나 이상의 리소스 랜들러를 등록하는데 사용
.addResourceHandler("/resources/**") : 정적 리소스를 제공하기 위한 핸들러 /resources/로 시작하는 모든 파일 경로
.addResourceLocations("/resources/"): 정적 리소스가 저장되는 위치를 지정 /resources/ 산하 디렉토리.
package com.example.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpringMvcDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class < ? > [] getRootConfigClasses() {
// TODO Auto-generated method stub
return null;
}
@Override
protected Class < ? > [] getServletConfigClasses() {
return new Class[] {
MVCConfig.class
};
}
@Override
protected String[] getServletMappings() {
return new String[] {
"/"
};
}
}
package com.example.model;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class SignUpForm {
private String firstName;
private String lastName;
private String email;
private String userName;
private String password;
}
model 데이터를 구성하였고 lombok을 사용하여 코드의 길이를 줄였다.
package com.example.controller;
import com.example.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";
}
}
@ModelAttribute
model.addAttribute = value 객체를 name 이름으로 추가해준다.
-> view에서 name으로 지정된 value를 사용
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Spring MVC 5 - form handling | Java Guides</title>
<link href="<c:url value="/resources/css/bootstrap.min.css" />"
rel="stylesheet">
<script src="<c:url value="/resources/js/jquery-1.11.1.min.js" />"></script>
<script src="<c:url value="/resources/js/bootstrap.min.js" />"></script>
</head>
<body>
<div class="container">
<div class="col-md-offset-2 col-md-7">
<h2 class="text-center">Spring MVC 5 Form Handling Example -
Sign up Form</h2>
<div class="panel panel-info">
<div class="panel-heading">
<div class="panel-title">Sign Up</div>
</div>
<div class="panel-body">
<form:form action="saveSignUpForm" cssClass="form-horizontal"
method="post" modelAttribute="signUpForm">
<div class="form-group">
<label for="firstName" class="col-md-3 control-label">First
Name</label>
<div class="col-md-9">
<form:input path="firstName" cssClass="form-control" />
</div>
</div>
<div class="form-group">
<label for="lastName" class="col-md-3 control-label">Last
Name</label>
<div class="col-md-9">
<form:input path="lastName" cssClass="form-control" />
</div>
</div>
<div class="form-group">
<label for="userName" class="col-md-3 control-label">User
Name </label>
<div class="col-md-9">
<form:input path="userName" cssClass="form-control" />
</div>
</div>
<div class="form-group">
<label for="password" class="col-md-3 control-label">Password</label>
<div class="col-md-9">
<form:password path="password" cssClass="form-control" />
</div>
</div>
<div class="form-group">
<label for="email" class="col-md-3 control-label">Email</label>
<div class="col-md-9">
<form:input path="email" cssClass="form-control" />
</div>
</div>
<div class="form-group">
<!-- Button -->
<div class="col-md-offset-3 col-md-9">
<form:button cssClass="btn btn-primary">Submit</form:button>
</div>
</div>
</form:form>
</div>
</div>
</div>
</div>
</body>
</html>
<%@ page isELIgnored="false" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>javaguides.net</title>
<link href="<c:url value="/resources/css/bootstrap.min.css" />"
rel="stylesheet">
<script src="<c:url value="/resources/js/jquery-1.11.1.min.js" />"></script>
<script src="<c:url value="/resources/js/bootstrap.min.js" />"></script>
</head>
<body>
<div class="container">
<div class="col-md-offset-2 col-md-7">
<h1>${message}</h1>
<hr />
<table class="table table-striped table-bordered">
<tr>
<td><b>First Name </b>: ${user.firstName}</td>
</tr>
<tr>
<td><b>Last Name </b> : ${user.lastName}</td>
</tr>
<tr>
<td><b>UserName </b> : ${user.userName}</td>
</tr>
<tr>
<td><b>Email </b>: ${user.email}</td>
</tr>
</table>
</div>
</div>
</body>
</html>
isELIgnored
${user.email}와 같이 작성한 form tag를 String으로 적용하는 여부를 설정한다.
defualt 값은 true이며, 만약 true로 설정시
이전 form에서 입력한 값이 아니라 "${user.email}"로 내용이 출력되게 된다.
따라서 전달된 값을 view에서 출력하기 위해서 isELIgnored =false로 설정해야 한다.
bootstrap의 css와 js 그리고 jqury의 js를 사용하여 resources 밑에 패키시를 설정하여 파일을 넣었다.
tomcat을 사용하여 서버를 실행
<%@ page isELIgnored="false" %>
를 설정하였다. 만약 설정하지 않았다면 signup-form.jsp에서 넘겨준 값을 가져오지 못한다.
https://www.javaguides.net/2018/10/spring-mvc-sign-up-form-handling.html
https://mangkyu.tistory.com/176
https://donggu1105.tistory.com/14