JSP actionsμ μ΄μ©νλ©΄ λμ μΌλ‘ νμΌμ μ½μ νκ±°λ μλ°λΉ μ»΄ν¬λνΈλ₯Ό μ¬μ¬μ©νκ±°λ μΉλΈλΌμ°μ λ₯Ό λ³΄κ³ μλ μ¬μ©μλ₯Ό λ€λ₯Έ νμ΄μ§λ‘ ν¬μλ©μν€κ±°λ μλ°νλ¬κ·ΈμΈμ μν HTMLμ μμ±ν μλ μμ΅λλ€.
λ¬Έλ²μ μλμ κ°μ΅λλ€.
<jsp:action_name attribute="value" />
Action μμλ κΈ°λ³Έμ μΌλ‘ 미리 μ μλμ΄μλ κΈ°λ₯λ€μ λλ€.
Syntax | Purpose |
---|---|
jsp:include | Includes a file at the time the page is requested |
jsp:useBean | Finds or instantiates a JavaBean |
jsp:setProperty | Sets the property of a JavaBean |
jsp:getProperty | Inserts the property of a JavaBean into the output |
jsp:forward | Forwards the requester to a new page |
jsp:plugin | Generates browser-specific code that makes an OBJECT or EMBED tag for the Java plugin |
jsp:element | Defines XML elements dynamically. |
jsp:attribute | Defines dynamically defined XML element's attribute. |
jsp:body | Defines 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κ³Ό κ΄λ ¨λ μμ±λ€μ μλμ κ°μ΅λλ€.
Attribute | Description |
---|---|
page | The relative URL of the page to be included. |
flush | The 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 ExampleToday'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κ³Ό κ΄λ ¨λ μμ±λ€μ μλμ κ°μ΅λλ€.
Attribute | Description |
---|---|
class | Designates the full package name of the bean. |
type | Specifies the type of the variable that will refer to the object. |
beanName | Gives 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 μ‘μ μμ μ¬μ©λλ μμ±λ€μ μλμ κ°μ΅λλ€.
Attribute | Description |
---|---|
name | Designates the bean whose property will be set. The Bean must have been previously defined. |
property | Indicates 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. |
value | The 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. |
param | The 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 μ‘μ μμ μ¬μ©λλ μμ±λ€μ μλμ κ°μ΅λλ€. μ£Όμν μ μ λκ°μ μμ± λͺ¨λ νμλ‘ μ¬μ©λμ΄μΌ νλ€λ κ²μ λλ€.
Attribute | Description |
---|---|
name | The name of the Bean that has a property to be retrieved. The Bean must have been previously defined. |
property | The 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 JSPGot message....Hello JSP... |
The <jsp:forward> Action
forward μ‘μ μ νμ¬ νμ΄μ§μ μ‘μ μ μ’ λ£νκ³ μμ²μ λ€λ₯Έ νμ΄μ§λ μλΈλ¦ΏμΌλ‘ ν¬μλ©ν©λλ€.
λ¬Έλ²μ μλμ κ°μ΅λλ€.
<jsp:forward page="Relative URL" />
μ΄ μ‘μ μ μμ±μ λ¬λ νκ°λ€μ.
Attribute | Description |
---|---|
page | Should 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λ₯Ό μμ²ν΄λ³΄μΈμ.
λ©μΈνμ΄μ§μ 컨ν νΈλ₯Ό μ μΈν ν¬μλ©λ νμ΄μ§μμμμΆλ ₯μ μλμ²λΌ λμ¬ κ²μ λλ€.
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
'π» Programming > JSP' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[JSP] Request Object ( Client Request ) (0) | 2019.02.15 |
---|---|
[JSP] Implicit Objects ( 묡μμ κ°μ²΄λ€ ) (0) | 2019.02.15 |
[JSP] Custom Tags ( 컀μ€ν , μ¬μ©μ μ μ νκ·Έ ) (0) | 2019.02.15 |
[JSP] Directives ( λλ ν°λΈ ) (0) | 2019.02.15 |
[JSP] JSP μλͺ μ£ΌκΈ° ( life cycle ) (0) | 2019.02.15 |