Struts와 JSTL(JSP,JSTL,Struts EL,Struts를 이용한 계산기)
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");
}
}
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");
}
}
[100%환급,개발자전문]빅데이터/SQL/자바/스프링/안드로이드/닷… | 12-27 | 2624 | ||
[채용확정무료교육]오라클자바개발잘하는신입뽑기2개월과정,교육… | 12-11 | 1895 | ||
53 | [평일100%환급7건]Spring,자바&JSP,안드로이드,웹퍼블리싱,C#닷… | 03-15 | 1714 | |
52 | [주말]C#,ASP.NET마스터 | 01-31 | 1823 | |
51 | [기업100%환급,평일주간]SQL기초에서스키마오브젝트,PLSQL,힌트… | 01-31 | 2674 | |
50 | [평일주간야간,주말]C기본&자료구조,알고리즘 | 01-31 | 1428 | |
49 | [평일주간,평일야간,주말]Spring,MyBatis,Hibernate개발자과정-… | 01-19 | 1751 | |
48 | [평일야간,주말]안드로이드개발자과정(Android기초실무) | 01-11 | 1638 | |
47 | [평일야간,주말주간야간]JAVA,Network&JSP&Spring,MyBatis,Hiber… | 01-03 | 2154 | |
46 | [100%환급,개발자전문]빅데이터/SQL/자바/스프링/안드로이드/닷… | 12-27 | 2624 | |
45 | [평일주간]NoSQL,MongoDB,빅데이터기초과정 | 12-19 | 1858 | |
44 | [평일주간야간, 주말]웹퍼블리싱 마스터(HTML5,CSS3,jQUERY,AJAX… | 12-14 | 1828 | |
43 | [채용확정무료교육]오라클자바개발잘하는신입뽑기2개월과정,교육… | 12-11 | 1895 | |
42 | [평일주간]빅데이터하둡기초과정(BigData Hadoop) | 12-09 | 1489 | |
41 | [평일야간]닷넷(C#,Network,ADO.NET,ASP.NET)마스터 | 12-01 | 1718 | |
40 | [기업100%환급]오라클&자바웹스프링신입과정3주(SQL,JAVA,JSP,Se… | 12-01 | 1897 | |
39 | [평일야간,주말]SQL기초에서실무까지(SQL기초,PLSQL,힌트,튜닝) | 12-01 | 1363 |
댓글 없음:
댓글 쓰기