I started to learn xhtml as it's the first topic from the book I'm reading and tried creating a simple form to enter just a name.
This is half of what I copied from the book only keeping the input text field:
<?xml version = "1.0" encoding = "UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<head>
<title>Enter Customer Name</title>
</head>
<body>
<h:outputStylesheet library="css" name="styles.css"/>
<h:messages/>
<h:form id="test">
<h:outputLabel for="firstname" value="first name:"></h:outputLabel>
<h:inputText id="firstname" label="first name" value="#{test.firstname}" required="true">
<f:validateLength minimum="2" maximum="10">
</f:validateLength>
</h:inputText>
</h:form>
</body>
</html>
But when I open it int he browser, this is what I get
The output
What am I doing wrong?
Your document isn't XHTML.
The Doctype claims it is, but then you have a bunch of errors and non-XHTML content.
The browser is parsing the document in XML mode, which is a good first step if you were actually writing XHTML.
As pointed out in a comment:
The xmlns attribute is not OK. It should be "http://www.w3.org/1999/xhtml" rather than "http://w3.org/1999/xhtml"
Since you got it wrong, your default namespace isn't recognised as XHTML and the browser treats it as an unknown XML format with no special handling. This is why you get view of the XML node tree.
When you correct that, your document begins to be parsed as XHTML.
now all i get is a blank screen
This is because the only XHTML elements you have are the root element, the head and its descendants, and the body.
Everything else (e.g. h:form) is not XHTML. You have imported it from the http://java.sun.com/jsf/html namespace (except for those parts you imported from the http://java.sun.com/jsf/core namespace). (Note that this is forbidden by your choice of Doctype).
Web browsers do not know what to do with elements from those namespaces, so they get inserted into the DOM but are not rendered.
You need to either:
Determine what tool those elements are designed for use with and use that instead of a web browser. (It is possible that that tool outputs code you can use with a web browser).
Write code using languages designed for use with a web browser. That could be real XHTML, but the benefits of it only apply to rare edge cases and you would almost certainly be better off writing HTML.
Related
I have following form,
<form action="contact_us.asp" method="post" enctype="multipart/form-data" name="form1" id="form1">
<input name="firstname" type="text" id="firstname" size="30" />
<input name="lastname" type="text" id="lastname" size="30" />
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
But when I am trying to get value of these post variables in my ASP file contact_us.asp then it returns blank. Code is below:
<%#LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<%
Dim FirstName, LastName, Email, Message
FirstName = request.form("firstname")
LastName = request.form("lastname")
response.write(FirstName & "OK")
%>
Its returning only "OK" to me. nothing in Message variable?
Please help me and tell me what's wrong here?
Classic ASP doesn't provide built-in support for working with `multipart/form-data. This is a surprisingly basic deficiency even for a language of ASP's venerable age, but what're you gonna do about it, move to ASP.NET? (Yes? oh. well never mind then.)
If you aren't doing file uploads, it's easiest just to stick with the default enctype (which is application/x-www-form-urlencoded). The only advantage of multipart/form-data is that you can put file uploads in it. (Theoretically, it would also have the advantage that you can specify character encodings definitively. But no browser actually does that.)
If you do need to handle multipart/form-data in Classic ASP you will need to parse the incoming request body yourself, splitting it into fields and values. Or rather, more typically, use an existing library to do it*.
That library will usually provide separate interfaces for reading the uploaded files and the other form values. This completely replaces use of the Classic ASP Request.Form interface. Exactly where you can find it depends on the library you choose. This does mean that if you want to have a form that can respond to either enctype equally you have to check the type and use one of the two different interfaces.
*: There are loads. for example. I'm not endorsing either of these as such... neither of them actually parse multiparts properly as per the standard and both seem a bit lax about filename security (never store a file under the user-submitted filename! security disaster!). But that seems to be par for the course for ASP upload scripts. At least, unlike many, they're not asking money for them.
I found by removing enctype completely (which defaults to application/x-www-form-urlencoded) from the form tag that Request.Form("SomeInputTagId") worked fine with method="post". I also didn't need to install any third party readers. Hope this helps.
Don't use enctype="multipart/form-data"
Remove that from the code and see if it works. The form-data enctype is used for uploading data, for example image files. You need to access the form elements slightly differently if you use that enctype.
If you are uploading data, then the ASP object you are using (for example ASP Upload) will have functions to access form fields. Request.form("") wont work.
Accessing the form values would be along the lines of:
Set yourUploadComponent = CreateObject("Your.UploadComponentClassString")
sFormValue = yourUploadComponent.Form.Item("txtName").Value
You will need to read the objects documentation.
What is the most correct way to include another XHTML page in an XHTML page? I have been trying different ways, none of them are working.
<ui:include>
Most basic way is <ui:include>. The included content must be placed inside <ui:composition>.
Kickoff example of the master page /page.xhtml:
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Include demo</title>
</h:head>
<h:body>
<h1>Master page</h1>
<p>Master page blah blah lorem ipsum</p>
<ui:include src="/WEB-INF/include.xhtml" />
</h:body>
</html>
The include page /WEB-INF/include.xhtml (yes, this is the file in its entirety, any tags outside <ui:composition> are unnecessary as they are ignored by Facelets anyway):
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h2>Include page</h2>
<p>Include page blah blah lorem ipsum</p>
</ui:composition>
This needs to be opened by /page.xhtml. Do note that you don't need to repeat <html>, <h:head> and <h:body> inside the include file as that would otherwise result in invalid HTML.
You can use a dynamic EL expression in <ui:include src>. See also How to ajax-refresh dynamic include content by navigation menu? (JSF SPA).
<ui:define>/<ui:insert>
A more advanced way of including is templating. This includes basically the other way round. The master template page should use <ui:insert> to declare places to insert defined template content. The template client page which is using the master template page should use <ui:define> to define the template content which is to be inserted.
Master template page /WEB-INF/template.xhtml (as a design hint: the header, menu and footer can in turn even be <ui:include> files):
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title><ui:insert name="title">Default title</ui:insert></title>
</h:head>
<h:body>
<div id="header">Header</div>
<div id="menu">Menu</div>
<div id="content"><ui:insert name="content">Default content</ui:insert></div>
<div id="footer">Footer</div>
</h:body>
</html>
Template client page /page.xhtml (note the template attribute; also here, this is the file in its entirety):
<ui:composition template="/WEB-INF/template.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<ui:define name="title">
New page title here
</ui:define>
<ui:define name="content">
<h1>New content here</h1>
<p>Blah blah</p>
</ui:define>
</ui:composition>
This needs to be opened by /page.xhtml. If there is no <ui:define>, then the default content inside <ui:insert> will be displayed instead, if any.
<ui:param>
You can pass parameters to <ui:include> or <ui:composition template> by <ui:param>.
<ui:include ...>
<ui:param name="foo" value="#{bean.foo}" />
</ui:include>
<ui:composition template="...">
<ui:param name="foo" value="#{bean.foo}" />
...
</ui:composition >
Inside the include/template file, it'll be available as #{foo}. In case you need to pass "many" parameters to <ui:include>, then you'd better consider registering the include file as a tagfile, so that you can ultimately use it like so <my:tagname foo="#{bean.foo}">. See also When to use <ui:include>, tag files, composite components and/or custom components?
You can even pass whole beans, methods and parameters via <ui:param>. See also JSF 2: how to pass an action including an argument to be invoked to a Facelets sub view (using ui:include and ui:param)?
Design hints
The files which aren't supposed to be publicly accessible by just entering/guessing its URL, need to be placed in /WEB-INF folder, like as the include file and the template file in above example. See also Which XHTML files do I need to put in /WEB-INF and which not?
There doesn't need to be any markup (HTML code) outside <ui:composition> and <ui:define>. You can put any, but they will be ignored by Facelets. Putting markup in there is only useful for web designers. See also Is there a way to run a JSF page without building the whole project?
The HTML5 doctype is the recommended doctype these days, "in spite of" that it's a XHTML file. You should see XHTML as a language which allows you to produce HTML output using a XML based tool. See also Is it possible to use JSF+Facelets with HTML 4/5? and JavaServer Faces 2.2 and HTML5 support, why is XHTML still being used.
CSS/JS/image files can be included as dynamically relocatable/localized/versioned resources. See also How to reference CSS / JS / image resource in Facelets template?
You can put Facelets files in a reusable JAR file. See also Structure for multiple JSF projects with shared code.
For real world examples of advanced Facelets templating, check the src/main/webapp folder of Java EE Kickoff App source code and OmniFaces showcase site source code.
Included page:
<!-- opening and closing tags of included page -->
<ui:composition ...>
</ui:composition>
Including page:
<!--the inclusion line in the including page with the content-->
<ui:include src="yourFile.xhtml"/>
You start your included xhtml file with ui:composition as shown above.
You include that file with ui:include in the including xhtml file as also shown above.
I'm using html provided by a designer to create a master page.
The html doctype is set to be HTML 4.0 Strict. The meta tags in the html do not have closing end tags (they end with > rather than />) and this html is compliant using the W3 validator tool.
For example:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
However, ASP.NET changes the head attributes before rendering the page and modifies the end tags, as below
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Now the addition of the /> at the end of the tag causes errors in the W3 validator.
How do I prevent this from happening, and ensure the html rendered is as the designers intended?
You can change how ASP.NET renders HTML by forcing it to use a particular XHTML conformance rendering mode. I'm not sure what other side effects changing the renderer may have on the HTML output produced by ASP.NET. This can be set in the web.config with the following.
<system.web>
<!-- other elements here -->
<xhtmlConformance
mode="Legacy" />
</system.web>
There are other caveats to using the "Legacy" rendering mode, see the following MSDN resources for other rendering modes and details.
XHTML Standards in Visual Studio and ASP.NET
How to: Configure XHTML Rendering in ASP.NET Web Sites
If I want to be XHTML strict and my headers are as follows:
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
If I pass the HTML Tidy check is the SGML Parser check also necessary given that I want to be XHTML compliant? (I have no real reason for this, I'm just being fussy until I have a reason to be less fussy.) When I look at the "Cleanup" suggested output from the Firefox plug-in validator, it gives the following code that is missing the closing part of the <input> tag.
<form method="post" action="set_anonymous">
<input type="submit" value="Be anonymous">
</form>
Does this kind of "cleaned up" code suggest I am using the validator improperly?
Have you tried going the other way? XHTML documents are plain XML files and their schema is defined in a dtd file.
That means any xml validator can tell you if your document is valid or not.
This is how you can validate if a document is proper:
Download XMLstarlet.
Go to XHTML dtd page and download these files:
xhtml1-strict.dtd
xhtml-lat1.ent
xhtml-special.ent
xhtml-symbol.ent
put them all in one folder. Inside that folder, put your XHTML document (let's call it test.xhtml) and run the following command:
xml val -e -d xhtml1-strict.dtd test.xhtml
If your response is "test.xhtml - valid", you've got a valid document.
If not, the -e flag will tell you what went wrong.
I have following form,
<form action="contact_us.asp" method="post" enctype="multipart/form-data" name="form1" id="form1">
<input name="firstname" type="text" id="firstname" size="30" />
<input name="lastname" type="text" id="lastname" size="30" />
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
But when I am trying to get value of these post variables in my ASP file contact_us.asp then it returns blank. Code is below:
<%#LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<%
Dim FirstName, LastName, Email, Message
FirstName = request.form("firstname")
LastName = request.form("lastname")
response.write(FirstName & "OK")
%>
Its returning only "OK" to me. nothing in Message variable?
Please help me and tell me what's wrong here?
Classic ASP doesn't provide built-in support for working with `multipart/form-data. This is a surprisingly basic deficiency even for a language of ASP's venerable age, but what're you gonna do about it, move to ASP.NET? (Yes? oh. well never mind then.)
If you aren't doing file uploads, it's easiest just to stick with the default enctype (which is application/x-www-form-urlencoded). The only advantage of multipart/form-data is that you can put file uploads in it. (Theoretically, it would also have the advantage that you can specify character encodings definitively. But no browser actually does that.)
If you do need to handle multipart/form-data in Classic ASP you will need to parse the incoming request body yourself, splitting it into fields and values. Or rather, more typically, use an existing library to do it*.
That library will usually provide separate interfaces for reading the uploaded files and the other form values. This completely replaces use of the Classic ASP Request.Form interface. Exactly where you can find it depends on the library you choose. This does mean that if you want to have a form that can respond to either enctype equally you have to check the type and use one of the two different interfaces.
*: There are loads. for example. I'm not endorsing either of these as such... neither of them actually parse multiparts properly as per the standard and both seem a bit lax about filename security (never store a file under the user-submitted filename! security disaster!). But that seems to be par for the course for ASP upload scripts. At least, unlike many, they're not asking money for them.
I found by removing enctype completely (which defaults to application/x-www-form-urlencoded) from the form tag that Request.Form("SomeInputTagId") worked fine with method="post". I also didn't need to install any third party readers. Hope this helps.
Don't use enctype="multipart/form-data"
Remove that from the code and see if it works. The form-data enctype is used for uploading data, for example image files. You need to access the form elements slightly differently if you use that enctype.
If you are uploading data, then the ASP object you are using (for example ASP Upload) will have functions to access form fields. Request.form("") wont work.
Accessing the form values would be along the lines of:
Set yourUploadComponent = CreateObject("Your.UploadComponentClassString")
sFormValue = yourUploadComponent.Form.Item("txtName").Value
You will need to read the objects documentation.