MVC와 JSP Form Tags는 Spring 기반 웹을 만드는 과정에서 중요한 역할을 한다. 사실 Form Tag는 일반 웹 프론트엔드 작업을 하는 과정에서도 많이 사용된다. 물론, Typescript기반 React.tx가 요새 인기를 끌고 있긴 하지만 기본적인 Spring을 공부해나가는 과정도 중요하니 한번 살펴보도록 하겠다.
spring-webmvc.jar
에서 spring-form.tld
로 존재한다. <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<form:form>
<table>
<tr>
<td>First Name:</td>
<td>
<form:input path="firstName" />
</td>
</tr>
<tr>
<td>Last Name:</td>
<td>
<form:input path="lastName" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Save Changes" />
</td>
</tr>
</table>
</form:form>
<input name="firstName" type="text" value="김" />
<form:input path="firstName"/>
<form:input path="lastName"/>
<input name="firstName" type="text" value="김"/>
<input name="lastName" type="text" value="진성"/>
public class Preferences {
private boolean receiveNewsletter;
private String[] interests;
private String favouriteWord;
public boolean isReceiveNewsletter() {
return receiveNewsletter;
}
public void setReceiveNewsletter(boolean receiveNewsletter) {
this.receiveNewsletter = receiveNewsletter;
}
public String[] getInterests() {
return interests;
}
public void setInterests(String[] interests) {
this.interests = interests;
}
public String getFavouriteWord() {
return favouriteWord;
}
public void setFavouriteWord(String favouriteWord) {
this.favouriteWord = favouriteWord;
}
}
<form:form>
<table>
<tr>
<td>Subscribe to newsletter?:</td>
<%-- Approach 1: Property is of type java.lang.Boolean --%>
<td>
<form:checkbox path="preferences.receiveNewsletter" />
</td>
</tr>
<tr>
<td>Interests:</td>
<%-- Approach 2: Property is of an array or of type java.util.Collection --%>
<td>
Quidditch:
<form:checkbox path="preferences.interests" value="Quidditch" /> Herbology:
<form:checkbox path="preferences.interests" value="Herbology" /> Defence Against the Dark Arts:
<form:checkbox path="preferences.interests" value="Defence Against the Dark Arts" />
</td>
</tr>
<tr>
<td>Favourite Word:</td>
<%-- Approach 3: Property is of type java.lang.Object --%>
<td>
Magic:
<form:checkbox path="preferences.favouriteWord" value="Magic" />
</td>
</tr>
</table>
</form:form>
java.lang.Boolean
의 유형 값으로 묶어 체크했을 경우 true, 반대의 경우 false로 묶어서 사용한다.java.util.Collection
으로 묶어서 체크했을 경우 collection에 setValue(Object) 값으로 넣어서 사용한다.<tr>
<td>Interests:</td>
<td>
Quidditch: <input name="preferences.interests" type="checkbox" value="Quidditch" />
<input type="hidden" value="1" name="_preferences.interests" /> Herbology: <input name="preferences.interests" type="checkbox" value="Herbology" />
<input type="hidden" value="1" name="_preferences.interests" /> Defence Against the Dark Arts: <input name="preferences.interests" type="checkbox" value="Defence Against the Dark Arts" />
<input type="hidden" value="1" name="_preferences.interests" />
</td>
</tr>
<form:form>
<table>
<tr>
<td>Interests:</td>
<td>
<%-- Property is of an array or of type java.util.Collection --%>
<form:checkboxes path="preferences.interests" items="${interestList}" />
</td>
</tr>
</table>
</form:form>
<tr>
<td>Sex:</td>
<td>
Male:
<form:radiobutton path="sex" value="M" /> <br/> Female:
<form:radiobutton path="sex" value="F" />
</td>
</tr>
<tr>
<td>Password:</td>
<td>
<form:password path="password" value="^76525bvHGq" showPassword="true" />
</td>
</tr>
<tr>
<td>Skills:</td>
<td>
<select name="skills" multiple="true">
<option value="Potions">Potions</option>
<option value="Herbology" selected="selected">Herbology</option>
<option value="Quidditch">Quidditch</option>
</select>
</td>
</tr>
<tr>
<td>House:</td>
<td>
<select name="house">
<option value="Gryffindor" selected="selected">Gryffindor</option>
<option value="Hufflepuff">Hufflepuff</option>
<option value="Ravenclaw">Ravenclaw</option>
<option value="Slytherin">Slytherin</option>
</select>
</td>
</tr>
<tr>
<td>Country:</td>
<td>
<form:select path="country">
<form:option value="-" label="--Please Select" />
<form:options items="${countryList}" itemValue="code" itemLabel="name" />
</form:select>
</td>
</tr>
<tr>
<td>Notes:</td>
<td>
<form:textarea path="notes" rows="3" cols="20" />
</td>
<td>
<form:errors path="notes" />
</td>
</tr>
<form:hidden path="house"/>
<input name="house" type="hidden" value="Gryffindor"/>
public class UserValidator implements Validator {
public boolean supports(Class candidate) {
return User.class.isAssignableFrom(candidate);
}
public void validate(Object obj, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "required", "Field is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "required", "Field is required.");
}
}
form.jsp 형식
<form method="POST">
<table>
<tr>
<td>First Name:</td>
<td><input name="firstName" type="text" value="" /></td>
<%-- Associated errors to firstName field displayed --%>
<td><span name="firstName.errors">Field is required.</span></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input name="lastName" type="text" value="" /></td>
<%-- Associated errors to lastName field displayed --%>
<td><span name="lastName.errors">Field is required.</span></td>
</tr>
<tr>
<td colspan="3">
<input type="submit" value="Save Changes" />
</td>
</tr>
</table>
</form>
이번에는 Spring에 대한 부분보다 Spring 내 jsp에서 form tag를 사용할 수 있는 다양한 방법에 대하여 정리하였다. 그러나, jsp로 개발하는 곳이 종종 있어서 알아두면 좋을거 같다.