Struts와 JSTL(JSP,JSTL,Struts EL,Struts를 이용한 계산기) Example 입니다.
열공하세요~
오라클자바커뮤니티에서 설립한 오엔제이프로그래밍 실무교육센터
Struts 1.1.부터는 JSTL을 지원하며 Struts에서 JSTL을 사용하기 위해 http://www.apache.org/dist/jakarta/taglibs/standard/ 에서 jakarta-taglibs-standard-current.zip 파일을 다은 받은 후 jar 파일을 /WEB-INF/lib 폴더에 복사 합니다.
JSTL에서는 스트럿츠의 logic, bean 태그 중 일부가 삭제 되었습니다.
Struts-EL – Logic Tags
Logic Tag Struts-EL Related JSTL Mechanisms
empty/notEmpty No c:if, c:when, EL ([not] empty)
equal/notEqual No c:if, c:when, EL (eq, ne)
forward No None (jsp:forward)
greaterThan/Equal No c:if, c:when, EL (gt, ge)
iterate Yes c:forEach
lessThan/Equal No c:if, c:when, EL (lt, le)
match/notMatch Yes EL*
messages[Not]Present Yes EL ([not] empty)
present/notPresent Yes EL ([not] empty)
redirect Yes c:redirect
Struts-EL – Bean Tags
Bean Tag Struts-EL Related JSTL Mechanisms
cookie No c:set, EL (cookie)
define No c:set, EL
header No c:set, EL (header, headerValues)
include Yes* c:import
message Yes fmt:bundle, fmt:message
page Yes EL (pageContext)
parameter No c:set, EL (param, paramValues)
resource Yes None
size Yes c:forEach, varStatus
struts Yes None
write No c:out
[계산기 예제]
아래 예제의 경우 전 Login이라는 Tomcat Project를 만들고 그 아래에 폴더(jstl)를 만들어서 JSP 파일을 두었으며 /WEB-INF/src/ 아래에 jstl 패키지를 만들어 CalcAction.java를 만들었습니다.
--------------------
Calc.jsp
--------------------
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<h2>JSP Calculator</h2>
<form action="">
<input type="text" name="j1" value='<%=request.getParameter("j1") == null ? "" : request.getParameter("j1")%>'/>
<br>
<input type="text" name="j2" value='<%=request.getParameter("j2") == null ? "" : request.getParameter("j2")%>'/>
<input type="submit"/>
</form><p>
<%= request.getParameter("j1") == null ? 0 :Float.parseFloat(request.getParameter("j1")) + Float.parseFloat(request.getParameter("j2")) %>
<h2>Struts Calculator</h2>
<bean:parameter id="b1" name="b1" value="0"/>
<bean:parameter id="b2" name="b2" value="0"/>
<form action="">
<input type="text" name="b1" value='<bean:write name="b1"/>'/><br>
<input type="text" name="b2" value='<bean:write name="b2"/>'/>
<input type="submit"/>
</form><p>
<bean:define id="bsum" value="<%=String.valueOf(Float.parseFloat(b1) + Float.parseFloat(b2))%>"/>
<bean:write name="bsum"/>
<h2>JSTL Calculator</h2>
<form action="">
<input type="text" name="v1" value='<c:out value="${param.v1}"/>'/><br>
<input type="text" name="v2" value='<c:out value="${param.v2}"/>'/>
<input type="submit"/>
</form><p>
<c:out value="${param.v1 + param.v2}" default="0.0"/>
<h2>Struts-EL Calculator</h2>
<!-- 아래의 Action 처리 위해 Struts에서 Action Mapping을 해야 함 -->
<html:form action="Calc.do">
<html:text property="s1" /><br>
<html:text property="s2" />
<html:submit/>
</html:form><p>
<c:out value="${requestScope.calcSum}" default="0.0"/>
-----------------------
struts-config.xml
-----------------------
적절히 수정 하세요~
<!-- ========= Form Bean Definitions ============== -->
<form-beans>
<form-bean name="CalcForm" type="jstl.CalcForm">
<form-property name="s1" type="java.lang.String" />
<form-property name="s2" type="java.lang.String" />
</form-bean>
</form-beans>
<!-- ========== Global Forward Definitions ============= -->
<global-forwards>
<forward name="calcjsp" path="/jstl/Calc.jsp" />
</global-forwards>
<action-mappings>
<action
path="/Calc"
type="jstl.CalcAction"
name="CalcForm"
validate="false"
input="/jstl/Calc.jsp"
/>
</action-mappings>
-------------------
CalcForm.java
-------------------
package jstl;
import org.apache.struts.action.ActionForm;
public class CalcForm extends ActionForm {
private String s1=null;
private String s2=null;
public String getS1() {
return s1;
}
public void setS1(String s1) {
this.s1 = s1;
}
public String getS2() {
return s2;
}
public void setS2(String s2) {
this.s2 = s2;
}
}
-------------------
CalcAction.java
-------------------
package jstl;
import org.apache.struts.action.Action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
public class CalcAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
int s1 = Integer.parseInt(((CalcForm)form).getS1());
int s2 = Integer.parseInt(((CalcForm)form).getS2());
int sum = s1 + s2;
request.setAttribute("calcSum",new Integer(sum));
return mapping.findForward("calcjsp");
}
}
(오라클SQL, 튜닝, 힌트,자바프레임워크, 안드로이드, 아이폰, 닷넷 실무전문 강의)
Struts 1.1.부터는 JSTL을 지원하며 Struts에서 JSTL을 사용하기 위해 http://www.apache.org/dist/jakarta/taglibs/standard/ 에서 jakarta-taglibs-standard-current.zip 파일을 다은 받은 후 jar 파일을 /WEB-INF/lib 폴더에 복사 합니다.
JSTL에서는 스트럿츠의 logic, bean 태그 중 일부가 삭제 되었습니다.
Struts-EL – Logic Tags
Logic Tag Struts-EL Related JSTL Mechanisms
empty/notEmpty No c:if, c:when, EL ([not] empty)
equal/notEqual No c:if, c:when, EL (eq, ne)
forward No None (jsp:forward)
greaterThan/Equal No c:if, c:when, EL (gt, ge)
iterate Yes c:forEach
lessThan/Equal No c:if, c:when, EL (lt, le)
match/notMatch Yes EL*
messages[Not]Present Yes EL ([not] empty)
present/notPresent Yes EL ([not] empty)
redirect Yes c:redirect
Struts-EL – Bean Tags
Bean Tag Struts-EL Related JSTL Mechanisms
cookie No c:set, EL (cookie)
define No c:set, EL
header No c:set, EL (header, headerValues)
include Yes* c:import
message Yes fmt:bundle, fmt:message
page Yes EL (pageContext)
parameter No c:set, EL (param, paramValues)
resource Yes None
size Yes c:forEach, varStatus
struts Yes None
write No c:out
[계산기 예제]
아래 예제의 경우 전 Login이라는 Tomcat Project를 만들고 그 아래에 폴더(jstl)를 만들어서 JSP 파일을 두었으며 /WEB-INF/src/ 아래에 jstl 패키지를 만들어 CalcAction.java를 만들었습니다.
--------------------
Calc.jsp
--------------------
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<h2>JSP Calculator</h2>
<form action="">
<input type="text" name="j1" value='<%=request.getParameter("j1") == null ? "" : request.getParameter("j1")%>'/>
<br>
<input type="text" name="j2" value='<%=request.getParameter("j2") == null ? "" : request.getParameter("j2")%>'/>
<input type="submit"/>
</form><p>
<%= request.getParameter("j1") == null ? 0 :Float.parseFloat(request.getParameter("j1")) + Float.parseFloat(request.getParameter("j2")) %>
<h2>Struts Calculator</h2>
<bean:parameter id="b1" name="b1" value="0"/>
<bean:parameter id="b2" name="b2" value="0"/>
<form action="">
<input type="text" name="b1" value='<bean:write name="b1"/>'/><br>
<input type="text" name="b2" value='<bean:write name="b2"/>'/>
<input type="submit"/>
</form><p>
<bean:define id="bsum" value="<%=String.valueOf(Float.parseFloat(b1) + Float.parseFloat(b2))%>"/>
<bean:write name="bsum"/>
<h2>JSTL Calculator</h2>
<form action="">
<input type="text" name="v1" value='<c:out value="${param.v1}"/>'/><br>
<input type="text" name="v2" value='<c:out value="${param.v2}"/>'/>
<input type="submit"/>
</form><p>
<c:out value="${param.v1 + param.v2}" default="0.0"/>
<h2>Struts-EL Calculator</h2>
<!-- 아래의 Action 처리 위해 Struts에서 Action Mapping을 해야 함 -->
<html:form action="Calc.do">
<html:text property="s1" /><br>
<html:text property="s2" />
<html:submit/>
</html:form><p>
<c:out value="${requestScope.calcSum}" default="0.0"/>
-----------------------
struts-config.xml
-----------------------
적절히 수정 하세요~
<!-- ========= Form Bean Definitions ============== -->
<form-beans>
<form-bean name="CalcForm" type="jstl.CalcForm">
<form-property name="s1" type="java.lang.String" />
<form-property name="s2" type="java.lang.String" />
</form-bean>
</form-beans>
<!-- ========== Global Forward Definitions ============= -->
<global-forwards>
<forward name="calcjsp" path="/jstl/Calc.jsp" />
</global-forwards>
<action-mappings>
<action
path="/Calc"
type="jstl.CalcAction"
name="CalcForm"
validate="false"
input="/jstl/Calc.jsp"
/>
</action-mappings>
-------------------
CalcForm.java
-------------------
package jstl;
import org.apache.struts.action.ActionForm;
public class CalcForm extends ActionForm {
private String s1=null;
private String s2=null;
public String getS1() {
return s1;
}
public void setS1(String s1) {
this.s1 = s1;
}
public String getS2() {
return s2;
}
public void setS2(String s2) {
this.s2 = s2;
}
}
-------------------
CalcAction.java
-------------------
package jstl;
import org.apache.struts.action.Action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
public class CalcAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
int s1 = Integer.parseInt(((CalcForm)form).getS1());
int s2 = Integer.parseInt(((CalcForm)form).getS2());
int sum = s1 + s2;
request.setAttribute("calcSum",new Integer(sum));
return mapping.findForward("calcjsp");
}
}
댓글 없음:
댓글 쓰기