Java: help me convert working .jsp to some XML notation

I have a .jsp file that works fine. It might be a bit special in that it calls a factory (ArticlesFactory) that returns a singleton (but that's a detail) of the class Articles

(it does this by automatically fetching public Google Docs, which is converted to html and then stored in "... / text / en ", but that's also a detail).

The following works great: it does exactly what I need it to do, it fetches the articles automatically and I can access the instance instance Articles

.

<%@ page pageEncoding="UTF-8" contentType="text/html;charset=utf-8" %>
<%@ page import="com.domain.projectname.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head></head>
<body>

    <% Articles articles = ArticlesFactory.create( getServletContext().getRealPath( "text/en" )); %>
    We have <%= articles.getNbItems()%>

</body>
</html>

      

However, I have to convert it to some entries that I don't know and don't understand, I'm not even sure what the title is and obviously I have a problem.

I don't know if there is a namespace issue or if there is a problem with the ArticlesFactory

static factory method creating the syntax Articles

:

<?xml version="1.0" encoding="UTF-8"?>
<jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page"
    xmlns:c="urn:jsptld:http://java.sun.com/jsp/jstl/core">
<jsp:directive.page import="com.domain.project.ArticlesFactory"/>
<jsp:directive.page contentType="text/html; charset=UTF-8" />

We have ${variable.nbItems}  <!-- What to put here !? -->

</jsp:root>

      

I tried a lot of things and couldn't figure it out.

Basically I need to: - call the static create method from the ArticlesFactory class - passing it the result of getServletContext (). getRealPath ("text / en"))

(which should return a copy of the article)

  • then I want to put the result of getNbItems () in the variable that I want to display

Note that I do not want to call getServletContext

from any servlet / dispatcher: I want to do it exactly like in the first working example (i.e. directly from within the .jsp).

+2


a source to share


1 answer


You are mainly looking for "JSP in XML Syntax" . Most of them are already explained in this (old) tutorial . You still need to replace <% %>

with <jsp:scriptlet>

and <%= %>

with <jsp:expression>

.

Namespace by the xmlns:c

way is unnecessary here unless you want to use tags this (and old) tutorial is , by the way, a separate subject.It only acts on objects in the scope of a page, request, session or application. However, in scripts, variables are only defined in local scopes (actually a method) that are not available in EL. You will need to do the following in your scriptlet to make it available in EL:



pageContext.setAttribute("articles", articles); // Put in page scope (recommended).
request.setAttribute("articles", articles); // Or in request scope. Also accessible by any include files.
session.setAttribute("articles", articles); // Or in session scope. Accessible by all requests in same session.
application.setAttribute("articles", articles); // Or in application scope. Accessible by all sessions.

      

Thus, it is available ${articles}

in EL.

+2


a source







All Articles