Programing

JSP 또는 JSTL 내에있는 경우

lottogame 2020. 3. 25. 08:18
반응형

JSP 또는 JSTL 내에있는 경우


JSP 파일의 조건에 따라 HTML 코드를 출력하고 싶습니다.

if (condition 1) {
    Some HTML code specific for condition 1
}
else if (condition 2) {
    Some HTML code specific for condition 2
}

어떻게해야합니까? JSTL을 사용해야합니까?


JSTL을 사용해야합니까?

예.

JSTL을 사용하여 jsp에서 조건부 렌더링을 수행하기 위해 <c:if><c:choose>태그를 사용할 수 있습니다 .

if 를 시뮬레이션하려면 다음을 사용할 수 있습니다.

<c:if test="condition"></c:if>

if ... else 를 시뮬레이션하려면 다음을 사용할 수 있습니다.

<c:choose>
    <c:when test="${param.enter=='1'}">
        pizza. 
        <br />
    </c:when>    
    <c:otherwise>
        pizzas. 
        <br />
    </c:otherwise>
</c:choose>

다른 텍스트를 출력하려는 ​​경우 더 간결한 예는 다음과 같습니다.

${condition ? "some text when true" : "some text when false"}

c : choose 보다 짧습니다 .


이를위한 구성은 다음과 같습니다.

<c:choose>
   <c:when test="${..}">...</c:when> <!-- if condition -->
   <c:when test="${..}">...</c:when> <!-- else if condition -->
   <c:otherwise>...</c:otherwise>    <!-- else condition -->
</c:choose>

조건이 비싸지 않으면 때로는 두 개의 구별되는 <c:if태그 를 사용 하는 것이 더 좋습니다. 쉽게 읽을 수 있습니다.


<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' %>
<c:set var="val" value="5"/>
<c:choose> 
  <c:when test="${val == '5'}">
    Value is 5
  </c:when>
  <c:otherwise>
    Value is not 5
  </c:otherwise>
</c:choose>

strings비교 하려면 다음 JSTL을 작성하십시오.

<c:choose>
    <c:when test="${myvar.equals('foo')}">
        ...
    </c:when>
    <c:when test="${myvar.equals('bar')}">
        ...
    </c:when>
    <c:otherwise>
        ...
    </c:otherwise>
</c:choose>

간단한 방법 :

<c:if test="${condition}">
    //if
</c:if>
<c:if test="${!condition}">
    //else
</c:if>

<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' %>
<c:set var="isiPad" value="value"/>
<c:choose>
   <!-- if condition -->
   <c:when test="${...}">Html Code</c:when> 
   <!-- else condition -->
   <c:otherwise>Html code</c:otherwise>   
</c:choose>

JSTL Tag Libe를 사용하여 다음을 수행하려면 다음 단계를 수행하십시오.

[요구 사항] 숫자가 40보다 크고 50보다 작 으면 "4로 시작하는 두 자리 숫자"를 표시하고 그렇지 않으면 "기타 숫자"를 표시하십시오.

[솔루션]

1. Please Add the JSTL tag lib on the top of the page.`
     <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>`

2. Please Write the following code
`
<c:choose>
       <c:when test="${params.number >=40 && params.number <50}">
              <p> Two digit number starting with 4. </p>
       </c:when>
       <c:otherwise>
              <p> Other numbers. </p>
       </c:otherwise>
  </c:choose>`

<% %>jsp 페이지에 if-else 조건을 작성하고 외부에 HTML 코드를 작성할 수 있습니다<% %>

예를 들면 다음과 같습니다.

   <%
        String username = (String)session.getAttribute("username");
        if(username==null)  {
    %>            
        <p> username is null</p> //html code
    <%
        } else {
    %>
        <p> username is not null</p> //html code
    <%
        }
    %>

나는 같은 문제가 있었고 조건이 맞는 경우 JSTL에서 Javascript를 사용할 수 없다고 생각했습니다 .

해결 방법은 Javascript로 할 수 있습니다.

<script>

    var isiPad = navigator.userAgent.match(/iPad/i) != null;

    if (isiPad) {
        document.write("Some HTML code for con1");
    } else {
        document.write("Some HTML code for con2");
    }

</script>

작성하려는 HTML 코드가 있어야하는 곳에이 스크립트를 넣어야합니다.


문자열을 비교하려면 다음 JSTL을 작성하십시오.

<c:choose>
    <c:when test="${myvar.equals('foo')}">
        ...
    </c:when>
    <c:when test="${myvar.equals('bar')}">
        ...
    </c:when>
    <c:otherwise>
        ...
    </c:otherwise>
</c:choose>

<c:choose>
<c:when test="${not empty userid and userid ne null}">
      <sql:query dataSource="${dbsource}" var="usersql">
                SELECT * FROM newuser WHERE ID = ?;
                <sql:param value="${param.userid}" />
      </sql:query>
 </c:when>
 <c:otherwise >
       <sql:query dataSource="${dbsource}" var="usersql">
                 SELECT * FROM newuser WHERE username = ?;
                 <sql:param value="${param.username}" />
       </sql:query>                              
  </c:otherwise>

참고 URL : https://stackoverflow.com/questions/5935892/if-else-within-jsp-or-jstl

반응형