오라클자바커뮤니티에서 설립한 오엔제이프로그래밍 실무교육센터
(신입사원채용추료교육, 오라클SQL, 튜닝, 힌트,자바프레임워크, 안드로이드, 아이폰, 닷넷)
HTML form에서 다음과 같이 checkbox를 사용하는 경우
<input type=”checkbox” name=”male” value=”true”/>
체크박스를 체크한 후 submit 하는 경우 male이라는 이름으로 true 값이 request parameter로서 넘어가지만 만약 체크를 하지 않고 submit을 했다면 male이라는 값은 넘어가지 않는다. 즉 체크하지 않으면 male라는 이름의 파라미터는 없는 것이다.
Struts2의 checkbox 인터셉터는 체크하지 않은 checkbox에 대해 기본값을 지정해서 파라미터로 사용가능 하도록 지원한다. checkbox 인터셉터의 uncheckedValue 값을 설정하여 체크하지 않은 경우의 파라미터 값을 지정할 수 있다(default는 false)
HTML form을 사용하는 경우 checkbox 인터셉터를 사용하기 위해서 다음과 같은 hidden 파라미터를 추가해야 한다.
<input type=”hidden” name=”__checkbox_[checkbox의 name값]”/>
[예]
<form action=”checkbox.action”>
<input type=”checkbox” name=”male” value=”true”/>
<input type=”hidden” name=”__checkbox_male” />남성
</form>
Struts2 Custom Tag를 사용하는 경우 다음과 같이 작성하면 된다.
<%@ taglib uri=”/struts-tags” prefix=”s” %>
<s:form action=”checkbox”>
<s:checkbox name=”male” value=”true” label=”남성”/>
</s:form>
struts.xml은 다음과 같이 작성한다.
<struts>
<package name="struts2" extends="struts-default">
<action name="checkbox" class="checkbox.action.CheckBoxAction">
<interceptor-ref name="checkbox">
<param name="uncheckedValue">false</param>
</interceptor-ref>
<result>/checkbox/result.jsp</result>
</action>
<action name="checkboxExam1">
<result>/checkbox/checkbox.jsp</result>
</action>
</package>
</struts>
만약 JSP에서 Struts CustomTag를 사용할 경우 직접 접근하지 말고 checkboxExam1 액션을 하나 만들어 이것의 result로 접근을 원하는 JSP를 기술하면 된다.
예제를 간단히 작성해 보자.
1. CheckBoxAction.java
package checkbox.action;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import checkbox.model.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
public class CheckBoxAction implements Action, Preparable, ModelDriven {
User user;
Log log = LogFactory.getLog(CheckBoxAction.class);
public String execute() throws Exception {
log.info(">>>> male : " + user.getMale());
log.info(">>>> female : " + user.getFemale());
return SUCCESS;
}
public void prepare() {
user = new User();
}
public Object getModel() {
return user;
}
}
2. Model 클래스인 User.java
package checkbox.model;
public class User {
private String male;
private String female;
public String getMale() {
return male;
}
public void setMale(String male) {
this.male = male;
}
public String getFemale() {
return female;
}
public void setFemale(String female) {
this.female = female;
}
}
3. JSP 파일들…
[checkbox.jsp]
<%@ page pageEncoding="euc-kr" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<s:form action="checkbox">
<s:checkbox name="male" value="true" label="남성"/>
<s:checkbox name="female" value="false" label="여성"/>
<s:submit/>
</s:form>
[result.jsp]
[Result] <br>
male : ${male} <br>
frmale : ${female}
4. struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="checkbox" class="checkbox.action.CheckBoxAction">
<interceptor-ref name="checkbox">
<param name="uncheckedValue">false</param>
</interceptor-ref>
<interceptor-ref name="prepare"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="params"/>
<result>/checkbox/result.jsp</result>
</action>
</package>
</struts>
5. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts2</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
댓글 없음:
댓글 쓰기