I have just started experimenting with JSPX and ICEfaces for a work project. I would like to know the best standards used to work with .JSPX and the best way to minimize code from pages.
I had worked with PHP, asp.NET, and Java before so you get an idea of my background.
I have some issues:
First of all, does JSPX use some kind of Master Page, if not I guess using "include" would be the best way to reduce repeated code. However I still cannot get rid of the whole header because I need the Title tag, unless this has a way around!?
The second issue is, that the html dumped by JSP is sometimes not valid. For example this is the html declaration: <html id="document:html" lang="en"> which is not valid, and there are other similar issues. I know there is another post about this on Stack Overflow, but that targets mainly self closing tags, doesn't mention the doctype issue.
I would appreciate if you can help me with my current issues, and if you have some good tutorials please share. What I would like is to create maintainable code and reduce code in pages as much as possible (for example include header and footer files, so you only have those in 1 file).
Update (BalusC Comment):
These are the first three lines I have in my .jspx file:
<?xml version="1.0" encoding="UTF-8"?>
<jsp:root version="2.1" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:ice="http://www.icesoft.com/icefaces/component">
<jsp:directive.page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"/>
Related
I have two jsp files (header.jspf and footer.jspf) that I want to include in a page produced by the doGet method of a servlet (Menu.java). I'm doing this by use of the RequestDispatcher.include() method. The header.jspf file only contains html so it renders fine. The footer.jspf file, however, contains both EL and JSP tags, neither of which render on the servlet page produced by Menu.java.
Looking at the source code of the page produced by Menu.java I understand that the reason for this problem is that the EL and JSP is not evaluated prior to being included in the servlet so it is just presumed by the browser that it is html.
While I'm guessing that what I'm trying to do may be poor (and deprecated) practice, I'd just like to find out if there is a way to get an included jsp file to render in a servlet just as it would in a jsp page when it contains EL and jsp tags?
I'm thinking that this question is general enough not to require my code to be posted, however if I'm wrong please tell me so and I will update with my code.
Not sure if there is a way to have RequestDispatcher.include() files processed, but including the footer explicitly will very likely work:
<%#include file="footer.jspf" %>
I am currently debugging a JSF application based on XHTML (still on myfaces 1.1 if you are curious).
But our faces are an assembly of many XHTML fragments and sometimes, for the purposes of debuging, it would help a lot knowing how the whole thing looks once all the fragments have been put together.
Do you know any way of visualizing those?
Thanks in advance
Is there somehow that I can invoke Java running on a server from a web browser? I would like:
User navigates to URL in a browser
User fills in input boxes (text)
User presses submit button
Input fields are sent as parameters to java that is executing on the server
A new html page is displayed that was generated by the java running on the server.
What is the standard way to do this, or something similar to this.
I think with PHP this would be relatively simple. I think that you would just pass arguments after the URL like this: www.mysite.com/folder?arguments.
Yes, this is possible (and is extremely common). Two of the most common ways are Java Servlets (where responses are generated purely via Java code) and Java Server Pages (where server logic is intermingled within HTML, similar to ASP or PHP).
There are countless ways to serve HTML from Java but virtually all of them rely on java servlets and java server pages (JSPs) which are Java's specification for handling web requests.
The absolute bare minimum to get going:
Install Java EE SDK ensuring to also install Netbeans and Glassfish.
Launch Netbeans and create a "Java Web" / "Web Application" project
Enter a project name, e.g. MyWebApp
In the Server and Settings screen, you need to Add... your server so do so. Point to the file location of your Glassfish server and enter the admin name and password
Ignore the framework stuff and Finish
NetBeans will generate a sample app and you can click straightaway on Run Main Project. It will deploy your app to Glassfish and load http://localhost:8080/MyWebApp/ from your default browser
Important things to note:
A file called web.xml tells the host server some basics about your web app. This file can contain a lot of other stuff but the default is some boiler plate. The most interesting part says <welcome-file>index.jsp</welcome-file> which means when you load http://localhost:8080/MyWebApp/ it will default to load index.jsp.
The index.jsp is what gets loaded if you don't specify a page to the server. If you look at index.jsp it's just HTML with some JSP markup.
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Creating new JSPs is as simple as writing HTML. Netbeans has a wizard to create a simple JSP.
You can embed chunks of Java into a .jsp easily and step in and out of Java / HTML with the <% %> notation such as
<%
for (int i = 0; i < 10; i++) {
%>
Hello <%=i%>
<% } %>
Glassfish is just one possible app server. As long as you write compliant code it should functional with only minimal or zero modifications on any other implementation of Java Servlet / JSP spec. e.g. Jetty, Tomcat, oc4j, JBoss, WebSphere etc.
This is just the tip of the iceberg. You can make things as simple or complex as you like.
Once you know the basics then it's up to you how deep you go. More advanced topics would be:
Taglibraries - these can remove a lot of java clutter and are considered more correct
Expressions - using expressions inside JSP pages to reduce the need for messy <%= notation
Custom servlets let you move model / business stuff into a Java class and leave the .jsp to just presentational
MVC web frameworks like Struts, Spring etc.
Security & filtering
It's a massive subject but it's fairly easy to do something quick and dirty.
As a followup to Mark Peters answer, you need a java web server like Tomcat or GlassFish in order to use servlets or jsps. There are a lot of great frameworks for Java that help you abstract away from the original servlet classes, but I'll let you look those up and decided if you even need them for something this simple.
If you want to pass arguments in a URL, then easier approach is Axis
You can display result with javascript on your page.
If you want to pass arguments in a URL, then easier approach is Axis
My school has an apache server that we are required to use. I was not allowed to install tomcat. I ended up invoking my server side Java using PHP. Not the most beautiful solution but it works.
I'm using IntellIJ with Apache Wicket and IntelliJ is showing me that tags like <wicket:extend> and <wicket:container> and adding wicket:id to other html tags is not valid.
What steps do I need to take to make IntelliJ recognize the wicket tags?
I'm using IntelliJ Ultimate 9 with the wicketforge plugin.
You can't really do it, adding the wicket namespace as in the other answer will only work for wicket:id, there is no dtd that also includes the wicket:container|panel etc.
There is this really old schema from the contrib project: http://wicket-contrib.googlecode.com/files/wicket.xsd but that doesn't include xhtml, so you'd need to create a schema to merge that and xhtml, and i don't believe there is a way other then manual.
The best you can do it add them to idea's ignored tags;
I use Eclipse, but to make validation errors go away, I just add the wicket namespace:
<html xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd" >
...
</html>
I suspect #slckin may be right. and to contribute to his answer, In IDEA, File->Settings->Inspections->HTML "Unknown HTML tag" is where you can add a list of comma seperated tags, mine looks like this: nobr,noembed,comment,noscript,embed,script,wicket:head,wicket:panel,wicket:remove,wicket:extend,wicket:child,wicket:container,wicket:enclosure,wicket:message,wicket:link,wicket:fragment
(not a complete list but covers most)
The best list of the tags in one place I've found is here: https://cwiki.apache.org/WICKET/wickets-xhtml-tags.html
The next block section down is "Unknown HTML tag attribute" and if you add wicket:id to the "Custom HTML tag attributes" list it should stop throwing that warning as well.
In "Project Settings - Schemas and DTDs" you can add the http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd DTD (download it save it somewhere, then browse to its location).
That will at least get rid of the warning about the undefined namespace, and make the red warnings less obtrusively brown, assuming your HTML files start with the following:
<?xml encoding="UTF-8" ?><!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd"
lang="en" xml:lang="en">
(The first <?xml encoding="UTF-8" ?> is stripped away by wicket, used just to specify UTF-8 encoding)
Then follow Raystorm's advice about adding the unknown HTML tag definitions.
Only problem I have now is that I get double type-completion suggestions for the <wicket:whatever elements, but it beats having error-markers everywhere.
OK, so I am trying to set up a simple JSF application. I'm using NetBeans 6.8, Glassfishv3 and Maven2. I made a JSP document like so:
<?xml version="1.0" encoding="utf-8"?>
<html xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<f:view>
<head>
<title><h:outputText value="#{Welcome.title}"/></title>
</head>
<body>
<h:outputText value="Welcome"/>
</body>
</f:view>
</html>
Problem is, if I navigate to this page (http://myHost/myApp/faces/welcome.jspx), it is returned as an XML document, but with the ${Welcome.title} value populated:
<?xml version="1.0" encoding="UTF-8"?>
<html><head><title>Gymix - Welcome</title></head><body>Welcome</body></html>
In Internet Explorer this looks like I would have opened an XML document. In Google Chrome the title is printed next to the text Welcome and instead of the title the URL to the page is printed on the tab.
If I change the JSP document to a plain JSP page (taglibs instead of xmlns and so on) it works and I get a proper page returned. Any ideas on what's wrong? Thanks!
Edit: sadly none of the quick fixes fixed this, so I'll look into this more. BTW, my pom.xml has jsf-api and jsf-impl dependencies with the version for both set to 1.2_14
Aside from the fact that you need to set the proper doctype and content type so that the browser knows what to do with the page, you also should get rid of the old fashioned jspx format and use xhtml format to get the most benefit of Java EE 6-shipped JSF 2.0 and Facelets.
The given code should be changed to:
<?xml version="1.0" encoding="UTF-8"?>
<!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"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>#{welcome.title}</title>
</h:head>
<h:body>
Welcome
</h:body>
</html>
Note that the doctype is included and that JSF 2.0 / Facelets will automatically take care about the right content type with help of the <h:head> component. Also note the absence of the <f:view> tag, this isn't needed anymore in Facelets.
You probably also need to reconfigure your webapp to make use of the full powers of JSF 2.0 and Facelets. To learn more about JSF 2.0 and Facelets, I strongly recommend to go through the Java EE 6 tutorial part II chapters 4-9.
Good luck.
Update: as per the comment of bobince: I would add an important note; it is true that the XML declaration (the first line) would mess the rendering mode of some webbrowsers (also see the site behind the doctype link here above), but that's certainly not an issue here. Facelets removes the XML declaration during generating the HTML of the page. The XML declaration is simply there because Facelets needs to parse the page using a XML based tool first. We're talking about a component based MVC framework and XML based templating technology, not about a plain vanilla HTML page ;)
I think you need to put in a valid doctype.
This would go below your xml declaration:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
You have to tell the browser what you're sending. I'm not exactly sure of how to do it inside a JSP though, so you'll have to figure out yourself or wait until someone more knowledgeable than me tells you.
You have to send the Content-Type HTTP header indicating your file is a text/html; charset=UTF-8.
Content-Type: text/html, charset=UTF-8