开发者

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

开发者 https://www.devze.com 2022-12-30 13:03 出处:网络
I\'ve got a .jsp file that is working fine.It\'s maybe a bit special in that it is calling a factory (ArticlesFactory) that returns a singleton (but that is a detail) of class Articles (it does so by

I've got a .jsp file that is working fine. It's maybe a bit special in that it is calling a factory (ArticlesFactory) that returns a singleton (but that is a detail) of class Articles (it does so by automa开发者_运维知识库tically fetching shared Google Docs that are transformed to html and then stored into ".../text/en" but that is a detail too).

The following is working fine: it does exactly what I need, it fetches the articles automatically and I can access my Articles instance fine.

<%@ 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 must transform it to some notation I don't know nor understand, I'm not even sure what the name for that is and obviously I've got some issue.

I don't know if it's a namespace issue or if there's a problem with the ArticlesFactory factory's static factory method creating the Articles singleton:

<?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 many things and couldn't figure it out.

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

(which should give back an Articles instance)

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

Note that I don't want to have to call getServletContext from any servlet/dispatcher: I want to do it just like in the first working example (ie directly from inside the .jsp).


You're basically looking for "JSP in XML syntax". Most is already explained in this (old) tutorial. You yet have to replace <% %> by <jsp:scriptlet> and <%= %> by <jsp:expression>.

The xmlns:c namespace is by the way unnecessary here, unless you'd like to use any of the JSTL core tags.

The Expression Language (those ${} things) which is explained in this (also old) tutorial is by the way a separate subject. It only acts on objects in the page, request, session or application scope. In scriptlets however, variables are only defined in local scopes (methodlocal actually), those aren't available in EL. You would need to do the following in the 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.

This way it's available by ${articles} in EL.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号