Jsp syntax, style and coding conventions 2003-10-14 - By Erik Beijnoff
Back I'm interested in getting some feedback on what coding conventions and best practises people are using on this list.
Below I've provided a short page that is a simple example of a login page for a web application. When you look at examples, tutorials and discussions on the web, the JSP provided usually doesn't look like this. As far as I've seen, it's far more common to use the "old" ways of doing things, such as <%=...%> and often <%if%>...<%else%> statements and so on.
Granted, JSP 2.0 isn't even final yet, but I like it and code almost exclusively this way. Those of you who are familiar with this way of coding, do you find it easier, or do you think it makes the view more complicated than neccessary?
Those of you who are NOT familiar with JSP 2.0, do you find the syntax awkward, or do you see a point in doing it the way shown below?
Actually this page doesn't really show the way I code, since I have the skin part of the rendered html page separated from the content, but I think the example below is a little bit easier to understand if I show just one JSP document instead of two.
I'd gladly explain exactly what is happening below, if you are interested in a deeper understanding of exactly what the page is doing, but in short: it is a login page with separated message bundles specified by the users language setting, and integrated validation and error messaging for the form fields.
Regards Erik Beijnoff Addsystems
-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- -- ---- ---- ---- ---- <?xml version="1.0" encoding="ISO-8859 (See http://ISO-8859.ora-code.com)-1"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jstl/core_rt" xmlns:st="http://com.haywire.stingray.tag" version="2.0">
<jsp:directive.page contentType="text/html" pageEncoding="ISO-8859 (See http://ISO-8859.ora-code.com)-1"/>
<jsp:text> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "XHTML1-t.dtd" > </jsp:text>
<c:set value="${requestScope['com.haywire.stingray.state.object']}" var="state" scope="page"/> <c:set value="${state.user}" var="user" scope="page"/> <c:set value="${user.locale}" var="loc" scope="page"/> <c:set value="${state.msgBundles['page']}" var="msgPage" scope="page"/>
<html xml:lang="en">
<head> <style type="text/css"> @(protected) url(${user.color}); @(protected) url(${user.font}); </style> <title>${st:msg(msgPage, loc, 'title')}</title> </head>
<body> <div style="text-align:center;"> <div style="font-size:24px;">${st:msg(msgPage, loc, 'title')}</div>
${st:actionResult(state, loc)}
<form action="${action['login'].path}" method="post">
<p>${st:msg(msgPage, loc, 'name')}</p> <p><input name="username" type="text"/></p> <p class="error">${st:validateResult(state, loc, 'username')}</p>
<p>${st:msg(msgPage, loc, 'password')}</p> <p><input name="password" type="text"/></p> <p class="error">${st:validateResult(state, loc, 'password')}</p>
<input type="submit" value="${st:msg(msgPage, loc, 'login')}"/> </form> </div> </body> </html> </jsp:root> -- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- -- ---- ---- ---- ---
=========================================================================== To unsubscribe: mailto listserv@(protected) with body: "signoff JSP-INTEREST". For digest: mailto listserv@(protected) with body: "set JSP-INTEREST DIGEST".
Some relevant archives, FAQs and Forums on JSPs can be found at:
http://java.sun.com/products/jsp http://archives.java.sun.com/jsp-interest.html http://forums.java.sun.com http://www.jspinsider.com
|
|