Prepare Interview

Exams Attended

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Apache Tapestry Interview Questions and Answers

Experienced / Expert level questions & answers

Ques 1. Why do we need @Script in Apache Tapestry?

The script framework is an effective means to bundle scripts in components. It provides scripts with the advantages of components. It can now be reused like a component and not have to worry about renaming field names or the wiring between the fields and the scripts. You just declare the component and you are good to go. It certainly is another layer of abstraction that one will have to learn but once you have learned it, it is very powerful. And honestly there is not much to it.
The script framework is mandated by the fact that form element/field names are automatically generated by the framework. And so you write your script in XML and use variables for these names and let the framework provide the correct names during runtime. Going further, you may also ask the framework to provide other objects that would help in creating your script. For example:

<input-symbol key="select"
    class="org.apache.tapestry.form.PropertySelection"
    required="yes"/>
      
This defines an input variable 'select' of type 'org.apache.tapestry.form.PropertySelection'. All such variables/symbols passed in to the script is stored in a symbol map. And now you can use the form select list name by using an ant style syntax like ${select.name}. The expression within ${} is an OGNL expression and is evaluated with respect to the symbol map. You may also define your own symbols/variables using <let> like:

<let key="formObj">
    document.${select.form.name}
</let>
<let key="selectObj">
    ${formObj}.${select.name}
</let>

These variables/symbols are stored in the symbol map also. So now if you want to set the value of the form select list all you do is say ${formObj}.${selectObj}.value = whatever; this would be equivalent to document.myForm.mySelect.value = whatever; where myForm is the form name and mySelect is the select list name.

<input-symbol...>s are like method parameters and <let...>s are like instance variables. Typically you would pass values to the <input-symbol...>s via the Script component like...
<component id="myScript" type="Script">
    <static-binding name="script" value="ScriptSpecificationName.script"/>
    <binding name="select" expression="components.somePropertySelection"/>
</component>

The actual scripts are defined in one of the two sections of the script specification, <body> or <initialization>, depending on when you want the script to execute. If you want the script to execute on load of the page, then you define it in the <initialization>, if you want it to execute on any other event, define it in the <body> section of the specification. For example:

<body>
    function onChangeList(listObj)
    {
        alert(listObj.value);
    }
</body>
<initialization>
    ${selectObj}.onchange = function(e)
    {
        onChangeList(${selectObj});
    }
</initialization>

As you can see in the rendered page all scripts are aggregated at the top of the page body, there are no more scripts all over the page. Even event handlers are attached to form objects in the initialization block.
One more thing to remember, scripts being components, and components by nature being independent of its environment, will render the script in the page once for every ocurrance of the component. If you want the body of the script to be rendered only once no matter how many times the component is used, just wrap the body in a <unique> tag like:

<body>
<unique>
    function onChangeList(listObj)
    {
        alert(listObj.value);
    }
</unique>
</body>

Is it helpful? Add Comment View Comments
 

Ques 2. How to alter the URL to point to the correct page?

You would need to throw a RedirectException with the new URL; this sends an HTTP redirect to the client.

Is it helpful? Add Comment View Comments
 

Ques 3. How should do page navigation in apache tapestry?

Usage page properties:

Page1.page
<page-specification class="Welcome.Action">
        <property name="success" value="Home" />
        <property name="error" value="Error" />
</page-specification>

Page2.page
<page-specification class="Welcome.Action">
        <property name="success" value="Home2" />
        <property name="error" value="Error2" />
</page-specification>

Welcome.Action.java
public void submitListener(IRequestCycle cycle)
{
    if (success)
        cycle.activate(getSpecification().getProperty("success"));
    if (error)
        cycle.activate(getSpecification().getProperty("error"));
}

So on success, it will be redirected to Home2 and on error it will be redirected to Error2 page.

Is it helpful? Add Comment View Comments
 

Ques 4. How to get a file from client input to server end in apache tapestry?

Make a method like the following a a listener, such as from a DirectLink or whatever.
(The Document is just a class that holds the file information you want to send to the user.)

public void downloadAction(IRequestCycle cycle)
{
    try
    {
        HttpServletResponse response =
        cycle.getRequestContext().getResponse();

        byte[] data = new byte[1024];
        FileInputStream in = document.getFileInputstream();

        response.setHeader("Content-disposition",
          "inline; filename=" +
           document.getFileName());
        response.setContentType(document.getMimeType());
        response.setContentLength(new Long(document.getSize()).intValue());
        ServletOutputStream out = response.getOutputStream();

        while (in.read(data) > -1)
        {
            out.write(data);
        }
        in.close();
        response.flushBuffer();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

Related interview subjects

Core Java interview questions and answers - Total 306 questions
Tomcat interview questions and answers - Total 16 questions
Apache Wicket interview questions and answers - Total 26 questions
Java Applet interview questions and answers - Total 29 questions
JAXB interview questions and answers - Total 18 questions
JMS interview questions and answers - Total 64 questions
Log4j interview questions and answers - Total 35 questions
Struts interview questions and answers - Total 84 questions
RMI interview questions and answers - Total 31 questions
Apache Camel interview questions and answers - Total 20 questions
JDBC interview questions and answers - Total 27 questions
Java 11 interview questions and answers - Total 24 questions
JPA interview questions and answers - Total 41 questions
EJB interview questions and answers - Total 80 questions
GWT interview questions and answers - Total 27 questions
Kotlin interview questions and answers - Total 30 questions
Glassfish interview questions and answers - Total 8 questions
Google Gson interview questions and answers - Total 8 questions
JSP interview questions and answers - Total 49 questions
J2EE interview questions and answers - Total 25 questions
Apache Tapestry interview questions and answers - Total 9 questions
Java Swing interview questions and answers - Total 27 questions
Java Mail interview questions and answers - Total 27 questions
Hibernate interview questions and answers - Total 52 questions
JSF interview questions and answers - Total 24 questions
Java 8 interview questions and answers - Total 30 questions
Java 15 interview questions and answers - Total 16 questions
JBoss interview questions and answers - Total 14 questions
Web Services interview questions and answers - Total 10 questions
RichFaces interview questions and answers - Total 26 questions
Servlets interview questions and answers - Total 34 questions
Java Beans interview questions and answers - Total 57 questions
Spring Boot interview questions and answers - Total 50 questions
JUnit interview questions and answers - Total 24 questions
Spring Framework interview questions and answers - Total 53 questions
Java Design Patterns interview questions and answers - Total 15 questions
©2023 WithoutBook