πŸ’» Programming/JSP

[JSP] Actions ( μ•‘μ…˜ )

JSP actions을 μ΄μš©ν•˜λ©΄ λ™μ μœΌλ‘œ νŒŒμΌμ„ μ‚½μž…ν•˜κ±°λ‚˜ μžλ°”λΉˆ μ»΄ν¬λ„ŒνŠΈλ₯Ό μž¬μ‚¬μš©ν•˜κ±°λ‚˜ μ›ΉλΈŒλΌμš°μ €λ₯Ό λ³΄κ³ μžˆλŠ” μ‚¬μš©μžλ₯Ό λ‹€λ₯Έ νŽ˜μ΄μ§€λ‘œ ν¬μ›Œλ”©μ‹œν‚€κ±°λ‚˜ μžλ°”ν”ŒλŸ¬κ·ΈμΈμ„ μœ„ν•œ HTML을 생성할 μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

문법은 μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€. 

<jsp:action_name attribute="value" />

Action μš”μ†ŒλŠ” 기본적으둜 미리 μ •μ˜λ˜μ–΄μžˆλŠ” κΈ°λŠ₯λ“€μž…λ‹ˆλ‹€.

SyntaxPurpose
jsp:includeIncludes a file at the time the page is requested
jsp:useBeanFinds or instantiates a JavaBean
jsp:setPropertySets the property of a JavaBean
jsp:getPropertyInserts the property of a JavaBean into the output
jsp:forwardForwards the requester to a new page
jsp:pluginGenerates browser-specific code that makes an OBJECT or EMBED tag for the Java plugin
jsp:elementDefines XML elements dynamically.
jsp:attributeDefines dynamically defined XML element's attribute.
jsp:bodyDefines dynamically defined XML element's body.

 

jsp:text

Use to write template text in JSP pages and documents.


Common Attributes ( 곡톡 속성 )

λͺ¨λ“  μ•‘μ…˜ μš”μ†Œλ“€μ— μ μš©λ˜λŠ” 곡톡속성이 λ‘κ°œκ°€ μžˆμŠ΅λ‹ˆλ‹€.  id μ†μ„±κ³Ό scope μ†μ„±μž…λ‹ˆλ‹€.

  • Id :  id 속성은 뭐 두말할 ν•„μš”λ„ μ—†λŠ” 속성이죠. Action μš”μ†Œμ˜ μœ λ‹ˆν¬ν•œ idλ₯Ό λ§ν•©λ‹ˆλ‹€. μ£Όλ―Όλ²ˆν˜Έκ°™μ€κ±°μ£ . 그리고 JSP pageλ‚΄μ—μ„œ 이 idλ₯Ό μ΄μš©ν•΄μ„œ 객체둜 접근이 κ°€λŠ₯ν•©λ‹ˆλ‹€. Action μ–΄λ–€ 객체의 μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜μ˜€λ‹€λ©΄ λ¬΅μ‹œμ μœΌλ‘œ μ •μ˜λ˜μ–΄μžˆλŠ” 객체인 PageContextλ₯Ό ν†΅ν•΄μ„œ μ•‘μ…˜ id κ°’μœΌλ‘œ μƒμ„±λœ 객체λ₯Ό μ°Έμ‘°ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 

  • scope : Action μš”μ†Œμ˜ 생λͺ…μ£ΌκΈ°λ₯Ό μ§€μ •ν•˜λŠ” μ†μ„±μž…λ‹ˆλ‹€.  id 속성과 scope 속성은 μ§μ ‘μ μœΌλ‘œ μ—°κ²°λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€. ν•΄λ‹Ή id λ₯Ό κ°–λŠ” 객체의 생λͺ…μ£ΌκΈ°λ₯Ό κ²°μ •ν•˜λŠ” 속성이기 λ•Œλ¬Έμ΄μ£ . scope 속성은 λ‹€μŒ 넀가지 값을 κ°€μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€. (a) page, (b)request, (c)session, and (d) application.
     

The <jsp:include> Action

νŽ˜μ΄μ§€μ— νŒŒμΌμ„ μΆ”κ°€ν•˜λŠ” μ•‘μ…˜μ΄μ£ . 

문법은 μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€. 

<jsp:include page="relative URL" flush="true" />

 include directiveμ™€λŠ” λ‹€λ₯΄κ²Œ νŽ˜μ΄μ§€κ°€ μš”μ²­μ΄ 될 λ•Œ νŒŒμΌμ„ μ‚½μž…ν•©λ‹ˆλ‹€. include directive의 κ²½μš°μ—λŠ” νŽ˜μ΄μ§€κ°€ λ‘œλ“œ(λ²ˆμ—­)될 λ•Œ νŒŒμΌμ„ μ‚½μž…ν•˜μ£ . 

 

include actionκ³Ό κ΄€λ ¨λœ 속성듀은 μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.

AttributeDescription
pageThe relative URL of the page to be included.
flushThe boolean attribute determines whether the included resource has its buffer flushed before it is included.

Example:

date.jsp  

<p> Today's date: <%= (new java.util.Date()).toLocaleString()%> </p>

main.jsp  

<html> <head> <title>The include Action Example</title> </head> <body> <center> <h2>The include action Example</h2> <jsp:include page="date.jsp" flush="true" /> </center> </body> </html>

 

 

μ‹€ν–‰ κ²°κ³ΌλŠ” μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.

The include action Example

Today's date: 12-Sep-2010 14:54:22

The <jsp:useBean> Action

useBean μ•‘μ…˜μ€ λ‹€μ–‘ν•˜κ²Œ μ‚¬μš©λ  수 μžˆλ‹€. νŠΉμ • 객체λ₯Ό 검색을 ν•΄μ„œ μ–»μ–΄μ˜€λŠ”λ° λ§Œμ•½ 검색이 μ‹€νŒ¨ν•˜λ©΄ 객체λ₯Ό 생성을 ν•΄μ€λ‹ˆλ‹€.  

문법은 μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€. 

<jsp:useBean id="name" class="package.class" />

빈 ν΄λž˜μŠ€κ°€ ν•œλ²ˆ λ‘œλ“œλ˜λ©΄ 빈 ν”„λ‘œνΌν‹°λ₯Ό μˆ˜μ •ν•˜κ±°λ‚˜ μ½μ–΄μ˜€κΈ° μœ„ν•΄μ„œ jsp:setProperty μ™€ jsp:getProperty μ•‘μ…˜μ„ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 

 

useBean actionκ³Ό κ΄€λ ¨λœ 속성듀은 μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€. 

AttributeDescription
classDesignates the full package name of the bean.
typeSpecifies the type of the variable that will refer to the object.
beanNameGives the name of the bean as specified by the instantiate () method of the java.beans.Beans class.

 

The <jsp:setProperty> Action

setProperty μ•‘μ…˜μ€ 빈의 ν”„λ‘œνΌν‹°λ₯Ό μˆ˜μ •ν•©λ‹ˆλ‹€. Bean 이 λ‹Ήμ—°νžˆ μ •μ˜κ°€ λ˜μ–΄μžˆμ–΄μ•Ό κ² μ£ .  

이 μ•‘μ…˜μ€ 두가지 λ°©λ²•μœΌλ‘œ μ‚¬μš©λ©λ‹ˆλ‹€. 

1.  jsp:useBean μ•‘μ…˜ 이후에, μ™ΈλΆ€μ—μ„œ μ‚¬μš©λ˜λŠ” 방법

<jsp:useBean id="myName" ... /> ... <jsp:setProperty name="myName" property="someProperty" .../>

 

2. jsp:useBean μ•‘μ…˜ λ‚΄λΆ€μ—μ„œ μ‚¬μš©λ˜λŠ” 방법

<jsp:useBean id="myName" ... > ... <jsp:setProperty name="myName" property="someProperty" .../> </jsp:useBean>

1번과 같이 μ‚¬μš©ν•  κ²½μš°μ—λŠ” Bean이 μƒˆλ‘œ μƒμ„±λ˜κ±°λ‚˜ 기쑴의 Bean이 κ²€μƒ‰λ˜κ±°λ‚˜ 상관없이 λͺ¨λ‘ jsp:setProperty κ°€ 싀행이 λ˜μ§€λ§Œ 2번처럼 μ‚¬μš©ν•  κ²½μš°μ—λŠ” μƒˆλ‘œμš΄ Bean이 μƒμ„±λ˜μ—ˆμ„ κ²½μš°μ—λ§Œ 싀행이 λ©λ‹ˆλ‹€.  

 

setProperty μ•‘μ…˜μ—μ„œ μ‚¬μš©λ˜λŠ” 속성듀은 μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.

 

AttributeDescription
nameDesignates the bean whose property will be set. The Bean must have been previously defined.
propertyIndicates the property you want to set. A value of "*" means that all request parameters whose names match bean property names will be passed to the appropriate setter methods.
valueThe value that is to be assigned to the given property. The the parameter's value is null, or the parameter does not exist, the setProperty action is ignored.
paramThe param attribute is the name of the request parameter whose value the property is to receive. You can't use both value and param, but it is permissible to use neither.

The <jsp:getProperty> Action

getProperty μ•‘μ…˜μ€ Bean의 ν”„λ‘œνΌν‹°λ₯Ό μ½μ–΄μ™€μ„œ String으둜 λ³€ν™˜ν•˜μ—¬ λ°˜ν™˜ν•΄μ€λ‹ˆλ‹€.  

getProperty μ•‘μ…˜μ€ λ‘κ°œμ˜ μ†μ„±λ§Œ 가지고 μžˆμŠ΅λ‹ˆλ‹€. μ‚¬μš©λ²•κ³Ό 속성은 μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.  

<jsp:useBean id="myName" ... /> ... <jsp:getProperty name="myName" property="someProperty" .../>

getProperty μ•‘μ…˜μ—μ„œ μ‚¬μš©λ˜λŠ” 속성듀은 μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€. μ£Όμ˜ν•  점은 λ‘κ°œμ˜ 속성 λͺ¨λ‘ ν•„μˆ˜λ‘œ μ‚¬μš©λ˜μ–΄μ•Ό ν•œλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€.

 

AttributeDescription
nameThe name of the Bean that has a property to be retrieved. The Bean must have been previously defined.
propertyThe property attribute is the name of the Bean property to be retrieved.

Example:

ν…ŒμŠ€νŠΈ λΉˆμ„ λ§Œλ“€μ–΄ λ΄…μ‹œλ‹€.

/* File: TestBean.java */ package action; public class TestBean { private String message = "No message specified"; public String getMessage() { return(message); } public void setMessage(String message) { this.message = message; } }

그리고 μ»΄νŒŒμΌν•΄μ„œ TestBean.class νŒŒμΌμ„ λ§Œλ“€κ³  C:\apache-tomcat-7.0.2\webapps\WEB-INF\classes\action 폴더에 λ³΅μ‚¬ν•΄μ„œ λ„£μŠ΅λ‹ˆλ‹€. 그리고 CLASSPATH λ³€μˆ˜μ— ν΄λ”κ²½λ‘œλ₯Ό λ„£μ–΄μ€λ‹ˆλ‹€.

이제 main.jsp νŒŒμΌμ„ λ§Œλ“€μ–΄μ„œ λΉˆμ„ μƒμ„±ν•˜κ³  set/getPropertyμ•‘μ…˜μ„ μ‚¬μš©ν•΄ λ΄…μ‹œλ‹€. 

<html> <head> <title>Using JavaBeans in JSP</title> </head> <body> <center> <h2>Using JavaBeans in JSP</h2> <jsp:useBean id="test" class="action.TestBean" /> <jsp:setProperty name="test" property="message" value="Hello JSP..." /> <p>Got message....</p> <jsp:getProperty name="test" property="message" /> </center> </body> </html>

κ²°κ³ΌλŠ” μ•„λž˜μ²˜λŸΌ λ‚˜μ™€μ•Ό κ² μ£ . 

Using JavaBeans in JSP

Got message....
Hello JSP...

The <jsp:forward> Action

forward μ•‘μ…˜μ€ ν˜„μž¬ νŽ˜μ΄μ§€μ˜ μ•‘μ…˜μ„ μ’…λ£Œν•˜κ³  μš”μ²­μ„ λ‹€λ₯Έ νŽ˜μ΄μ§€λ‚˜ μ„œλΈ”λ¦ΏμœΌλ‘œ ν¬μ›Œλ”©ν•©λ‹ˆλ‹€.

문법은 μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.

<jsp:forward page="Relative URL" />

이 μ•‘μ…˜μ˜ 속성은 λ‹¬λž‘ ν•œκ°œλ„€μš”.

AttributeDescription
pageShould consist of a relative URL of another resource such as a static page, another JSP page, or a Java Servlet.

Example:

 date.jsp

<p> Today's date: <%= (new java.util.Date()).toLocaleString()%> </p>

main.jsp

<html> <head> <title>The include Action Example</title> </head> <body> <center> <h2>The include action Example</h2> <jsp:forward page="date.jsp" /> </center> </body> </html>

root 디렉토리에 이 νŒŒμΌλ“€μ„ λ„£κ³  main.jspλ₯Ό μš”μ²­ν•΄λ³΄μ„Έμš”.

λ©”μΈνŽ˜μ΄μ§€μ˜ μ»¨ν…νŠΈλ₯Ό μ œμ™Έν•œ ν¬μ›Œλ”©λœ νŽ˜μ΄μ§€μ—μ„œμ˜μΆœλ ₯은 μ•„λž˜μ²˜λŸΌ λ‚˜μ˜¬ κ²ƒμž…λ‹ˆλ‹€.

Today's date: 12-Sep-2010 14:54:22

The <jsp:plugin> Action

plugin μ•‘μ…˜μ€ JSPνŽ˜μ΄μ§€μ— μžλ°” μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‚½μž…ν•˜κΈ° μœ„ν•΄μ„œ μ‚¬μš©λ©λ‹ˆλ‹€. λΈŒλΌμš°μ € νƒ€μž…μ„ κ²°μ •ν•˜κ³ , ν•„μš”ν•œκ²½μš° <object> λ˜λŠ” <embed> νƒœκ·Έλ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€.

ν•„μš”ν•œ ν”ŒλŸ¬κ·ΈμΈμ΄ μ‘΄μž¬ν•˜μ§€ μ•ŠμœΌλ©΄ ν”ŒλŸ¬κ·ΈμΈμ„ λ‹€μš΄λ‘œλ“œν•˜κ³  μžλ°”μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‹€ν–‰μ‹œν‚΅λ‹ˆλ‹€. κ·Έ Java componentλŠ” Applet이 될 μˆ˜λ„ 있고  JavaBean이 될 μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.  

plugin action 은 Java componentsλ₯Ό formattingν•˜κΈ° μœ„ν•΄μ„œ 곡톡 HTML νƒœκ·Έμ— μƒμ‘ν•˜λŠ” μ—¬λŸ¬ 속성듀을 가지고 μžˆμŠ΅λ‹ˆλ‹€.  <param> μš”μ†Œ λ˜ν•œ Applet μ΄λ‚˜ Bean에 νŒŒλΌλ―Έν„°λ₯Ό μ „λ‹¬ν•˜κΈ° μœ„ν•΄μ„œ μ‚¬μš©λ  수 μžˆμŠ΅λ‹ˆλ‹€.

 

<jsp:plugin type="applet" codebase="dirname" code="MyApplet.class" width="60" height="80"> <jsp:param name="fontcolor" value="red" /> <jsp:param name="background" value="black" /> <jsp:fallback> Unable to initialize Java Plugin </jsp:fallback> </jsp:plugin>

μ—¬κΈ°μ„œ <fallback> μš”μ†ŒλŠ” μžλ°”μ»΄ν¬λ„ŒνŠΈ μ‹€ν–‰μ‹œ 였λ₯˜κ°€ λ°œμƒν–ˆμ„ λ•Œ 였λ₯˜ λ©”μ‹œμ§€λ₯Ό μ‚¬μš©μžμ—κ²Œ μ „λ‹¬ν•΄μ£ΌλŠ” 역할을 ν•©λ‹ˆλ‹€. 

 <jsp:element>,  <jsp:attribute>,  <jsp:body> Action

<jsp:element>, <jsp:attribute> and <jsp:body> μ•‘μ…˜λ“€μ€ XML μš”μ†Œλ₯Ό λ™μ μœΌλ‘œ μ •μ˜ν•˜κΈ°μœ„ν•΄μ„œ μ‚¬μš©λ©λ‹ˆλ‹€. λ™μ μ΄λΌλŠ” 것은 컴파일 μ‹œκ°€ μ•„λ‹ˆλΌ λŸ°νƒ€μž„μ— μš”μ²­μ— μ˜ν•΄μ„œ λ§Œλ“€μ–΄μ§„ λ‹€λŠ” 것을 μ˜λ―Έν•©λ‹ˆλ‹€. 

μ•„λž˜λŠ” κ°„λ‹¨ν•œ μ˜ˆμ œμž…λ‹ˆλ‹€. 

<%@page language="java" contentType="text/html"%> <html xmlns="http://www.w3c.org/1999/xhtml" xmlns:jsp="http://java.sun.com/JSP/Page"> <head><title>Generate XML Element</title></head> <body> <jsp:element name="xmlElement"> <jsp:attribute name="xmlElementAttr"> Value for the attribute </jsp:attribute> <jsp:body> Body for XML element </jsp:body> </jsp:element> </body> </html>

μ‹€ν–‰μ‹œν‚€λ©΄ λ™μ μœΌλ‘œ μ•„λž˜μ™€κ°™μ€ HTML파일이 λ§Œλ“€μ–΄μ§‘λ‹ˆλ‹€. 

<html xmlns="http://www.w3c.org/1999/xhtml" xmlns:jsp="http://java.sun.com/JSP/Page"> <head><title>Generate XML Element</title></head> <body> <xmlElement xmlElementAttr="Value for the attribute"> Body for XML element </xmlElement> </body> </html>

The <jsp:text> Action

<jsp:text> μ•‘μ…˜μ€ JSP νŽ˜μ΄μ§€μ™€ λ¬Έμ„œλ₯Ό μœ„ν•œ ν…œν”Œλ¦Ώ ν…μŠ€νŠΈλ₯Ό μ“°κΈ°μœ„ν•΄ μ‚¬μš©λ  수 μžˆμŠ΅λ‹ˆλ‹€.

문법은 μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€. 

<jsp:text>Template data</jsp:text>

λ°”λ””μ—λŠ” λ‹€λ₯Έ μš”μ†Œλ₯Ό κ°€μ§ˆ 수 μ—†μŠ΅λ‹ˆλ‹€. μ˜€λ‘œμ§€ ν…μŠ€νŠΈμ™€ EL ν‘œν˜„μ‹λ§Œ λ“€μ–΄κ°ˆ 수 μžˆμŠ΅λ‹ˆλ‹€. XML filesμ—μ„œλŠ” ${whatever > 0}와 같은 ν‘œν˜„μ‹μ€ μ“Έ 수 μ—†μŠ΅λ‹ˆλ‹€ λΉ„κ΅μ—°μ‚°μž > κ°€ μ‚¬μš©λ  수 μ—†κΈ° λ•Œλ¬Έμ΄μ£ . λŒ€μ‹  ${whatever gt 0} 와 같이 μ“°κ±°λ‚˜ CDATA λ₯Ό μ΄μš©ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

<jsp:text><![CDATA[<br>]]></jsp:text>

 XHTML을 μœ„ν•œ DOCTYPE 선언을 μΆ”κ°€ν•  λ•Œμ—λ„ μ•„λž˜μ²˜λŸΌ  <jsp:text> μš”μ†Œλ₯Ό μ΄μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 

<jsp:text><![CDATA[<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">]]> </jsp:text> <head><title>jsp:text action</title></head> <body> <books><book><jsp:text> Welcome to JSP Programming </jsp:text></book></books> </body> </html>

 <jsp:text> μ•‘μ…˜ 없이 ν•œλ²ˆ μ‹€ν–‰ν•΄λ³΄μ„Έμš”.

 

 

 

 

Reference : http://www.tutorialspoint.com/jsp/jsp_actions.htm