I am new to JSTL, could someone please tell me how to convert below JSP and HTML code to full JSTL with no scriptlet in the page?
I'd also be grateful for suggestions about some good resources to learn JSTL and advanced JSP concepts such as JSF and spring with CRUD example.
This is curd example taken from http://javaknowledge.info/?p=478. I took this example because I thought it is 100% JSTL implementation but I was wrong.
My IDE is NetBeans.
<form method="POST" action='UserController' name="frmAddUser">
<% String action = request.getParameter("action");
System.out.println(action);
%>
<% if (action.equalsIgnoreCase("edit")) {%>
User Name : <input type="text" name="uname"
value="<c:out value="${user.uname}" />" readonly="readonly"/> (You Can't Change this)<br />
<%} else {%>
User Name : <input type="text" name="uname"
value="<c:out value="${user.uname}" />" /> <br />
<%}%>
Password : <input
type="password" name="pass"
value="<c:out value="${user.password}" />" /> <br />
Email : <input
type="text" name="email"
value="<c:out value="${user.email}" />" /> <br />
<% if (action.equalsIgnoreCase("edit")) {%>
Registration : <input
type="text" name="dob"
value="<fmt:formatDate pattern="yyyy/MM/dd" value="${user.registeredon}" />" readonly="readonly"/>(You Can't Change this) <br />
<%} else {%>
Registration : <input
type="text" name="dob"
value="<fmt:formatDate pattern="yyyy/MM/dd" value="${user.registeredon}" />" />(yyyy/MM/dd) <br />
<%}%>
<input type="submit" value="Submit" />
</form>
There are only two types of scriptles:
print the current action into system's out - there is no JSTL alternative
if-else statement can be rewritten using
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:choose>
<c:when test='${ fn:toLowerCase(param.action) eq "edit" }'>
...
</c:when>
<c:otherwise>
...
</c:otherwise>
</c:choose>
Years ago i learnd alot from this videos:
https://www.youtube.com/user/3n3xus/videos
Too bad, its in German and eclipse.
You learn
how to write your own JSTL-Framework.
how to write and navigate in jsf
work with richfaces.
how to work with facelets.
Related
Hello i can't get any value from a from in my index.jsp file. I'm using tomcat7, after running index.jsp and clicking send button nothing happen. System.out.prinln() print nothinig or null ;(
index.jsp
%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<form acton="addnewtask.jsp" method="post" >
<label for="name">Name</label>
<input type="text" class="form-control" id="name" placeholder="Name">
<button type="submit" class="btn btn-danger">Add</button>
</body>
</html>
addnewtask.jsp
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%
String s = request.getParameter("name");
System.out.println(s);
%>
Do you know what am i doing wrong ?
This line allows you to get a parameter with its name not its id :
String s = request.getParameter("name");
Add a name to your input and correct the typo of the attribute action of your form :
<form action="addnewtask.jsp" method="post" >
<label for="name">Name</label>
<input name="name" type="text" class="form-control" id="name" placeholder="Name">
<button type="submit" class="btn btn-danger">Add</button>
<form>
String s = request.getParameter("xyz");
Add a name to your input box
<form action="addnewtask.jsp" method="post" >
<label for="name">Name</label>
<input type="text" class="form-control" name="xyz" id="name" placeholder="Name">
<button type="submit" class="btn btn-danger">Add</button>
<form>
I have a <div> tag:
<div id="content">
<jsp:include page="some.jsp"/>
</div>
Also I have a few buttons:
<input type="submit" name="btn1" value="Page1">
<input type="submit" name="btn2" value="Page2">
I need when I click btn1 some.jsp changes to page1.jsp and to page2.jsp when click btn2.
Use dynamic include,
<jsp:include page="<%= myVariable %>" flush="true" />
Pd: Take a look a flush http://www.coderanch.com/t/484149/JSP/java/flush-true-jsp-include-tag.
1- Instead of using static include, you can use dynamic include, then you can do something like this:
<jsp:include page="${somePage}" flush="true" />
2- Use javascript to change the action of the form depending on the button you click:
<input type="submit" value="Page1" name="btn1"
onclick="document.forms[0].action = 'somePage.jsp'; return true;" />
Im using this solution : My form is :
<FORM>
<select name="choix">
<option value="choix 1">choix 1</option>
<option value="choix 2">choix 2</option>
<option value="choix 3">choix 3</option>
</select>
<input type="submit" />
</FORM>
and im using this in the same jsp page to include a page as what i select in that form:
<%
Ch = request.getParameter("choix");
%>
<div id="inculde_page">
<jsp:include page="<%= "layouts/" + Ch + ".jsp" %>" />
</div>
I have the following JSP.
<s:form id="cursorWait" theme="vs" action="list" method="get" acceptcharset="utf-8">
<div class="chckbox">
<s:checkbox id="as" name="as" />
<quartzi:text name="ts" />
</div>
<div class="chckbox">
<s:checkbox id="aec" name="aec" />
<quartzi:text name="tec" />
</div>
<div class="chckbox">
<s:checkbox id="aua" name="aua" />
<quartzi:text name="tua" />
<br/>
<br/>
</div>
<s:select name="seekRA" list="listeChoixRA" value="%{seekRA}" emptyOption="true"/>
<s:textfield id="numAff" name="seekCode" key="numeroaffaire" />
<s:textfield id="libAff" name="seekLibelleText" key="libelle" />
<s:a href="#" cssClass="action_effacer_sans_label" title="%{getText('effacer')}" onClick="gomme()"></s:a>
<s:hidden name="firstSearch" value="done" />
<s:submit id="recherche" key="rechercher" cssClass="wait"/>
</s:form>
<jsp:include page="inc_complex_search.jsp"/>
The three div checkbox are redundant to other files, so I decided to put them in a JSP file that I include. My page becomes:
<s:form id="cursorWait" theme="vs" action="list" method="get" acceptcharset="utf-8">
<jsp:include page="inc_checkbox_entity.jsp"/>
<s:select name="seekRA" list="listeChoixRA" value="%{seekRA}" emptyOption="true"/>
<s:textfield id="numAff" name="seekCode" key="numeroaffaire" />
<s:textfield id="libAff" name="seekLibelleText" key="libelle" />
<s:a href="#" cssClass="action_effacer_sans_label" title="%{getText('effacer')}" onClick="gomme()"></s:a>
<s:hidden name="firstSearch" value="done" />
<s:submit id="recherche" key="rechercher" cssClass="wait"/>
</s:form>
<jsp:include page="inc_complex_search.jsp"/>
with inc_checkbox_entity.jsp being:
<%# taglib prefix="s" uri="/struts-tags"%>
<%# taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%# taglib prefix="q" uri="/WEB-INF/tlds/q.tld"%>
<div class="chckbox">
<s:checkbox id="as" name="as" />
<q:text name="toutessocietes" />
</div>
<div class="chckbox">
<s:checkbox id="aec" name="aec" />
<quartzi:text name="tec" />
</div>
<div class="chckbox">
<s:checkbox id="aua" name="aua" />
<quartzi:text name="tua" />
</div>
And the result is... different...
and I don't understand why.
Shouldn't those two pieces of code produce the same exact page? What makes them different?
Here is a screenshot:
If I have the code in the main JSP, rather than using an include, then the search entities are under the block "société .. Unités analytiques" and aligned with it.
PS:
I have tried to use
<c:import url="inc_checkbox_entity.jsp"/>
but in this case the part between:
<s:select...
and <s:submit...
doesn't appear anymore.
If you use <jsp:include> there should be a jsp fragment to be included. But your custom tags didn't like to be included. Thus you might want to modify a jsp fragment so you can use <jsp:include> tag.
<%# taglib prefix="q" uri="/WEB-INF/tlds/q.tld"%>
<div class="chckbox">
<input type="checkbox" id="as" name="as" value="${as}"/>
<q:text name="toutessocietes" />
</div>
<div class="chckbox">
<input type="checkbox" id="aec" name="aec" value="${aec}"/>
<quartzi:text name="tec" />
</div>
<div class="chckbox">
<input type="checkbox" id="aua" name="aua" value="${aua}"/>
<quartzi:text name="tua" />
</div>
Some custom tags might also not work, better remove/replace them with JSP content if possible.
In jsp I'm have this:
<h3>Order total: <fmt:formatNumber type="NUMBER" value="${order.total}"/></h3>
In html when open page:
<h3>Order total: <fmt:formatNumber value="20.0" type="NUMBER"/></h3>
But I'm don't see number, see only Order total:
full jsp:
<html xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:spring="http://www.springframework.org/tags"
xmlns:form="http://www.springframework.org/tags/form"
xmlns:fmt="http://java.sun.com/jstl/format">
<jsp:output omit-xml-declaration="yes"/>
<jsp:directive.page contentType="text/html;charset=UTF-8" />
<head><title>Spizza</title></head>
<body>
<h2>Your order</h2>
<h3>Deliver to:</h3>
<b>${order.customer.name}</b><br/>
<b>${order.customer.address}</b><br/>
<b>${order.customer.city}, ${order.customer.state}
${order.customer.zipCode}</b><br/>
<b>${order.customer.phoneNumber}</b><br/>
<hr/>
<h3>Order total: <fmt:formatNumber type="NUMBER"
value="${order.total}"/></h3>
<hr/>
<h3>Pizzas:</h3>
<c:if test="${fn:length(order.pizzas) eq 0}">
<b>No pizzas in this order.</b>
</c:if>
<br/>
<c:forEach items="${order.pizzas}" var="pizza">
<li>${pizza.size} :
<c:forEach items="${pizza.toppings}" var="topping">
<c:out value="${topping}" />,
</c:forEach>
</li>
</c:forEach>
<form:form>
<input type="hidden" name="_flowExecutionKey"
value="${flowExecutionKey}"/>
<input type="submit" name="_eventId_createPizza"
value="Create Pizza" />
<c:if test="${fn:length(order.pizzas) gt 0}">
<input type="submit" name="_eventId_checkout"
value="Checkout" />
</c:if>
<input type="submit" name="_eventId_cancel"
value="Cancel" />
</form:form>
</body>
</html>
The documentation says:
Standard Syntax:
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
XML Syntax:
<anyxmlelement xmlns:fmt="http://java.sun.com/jsp/jstl/fmt" />
I have a class with a property called title and I have a getter/setter that gets and sets the property. If the property is P I need to print the word "Peer" on the page, and if it's T I need to print "Team" on the page. Can I do this in a JSP without using a scriplets? I tried using
<jsp:getProperty name="value" class"classname" />
but from there I have no idea how to use a conditional in a JSP. Please help.
Use JSTL, as #CoolBeans says. It would look something like this:
In the servlet,
// where myBean is an instance of the class with [get|set]Title
request.setAttribute("myFoo", myBean);
Then, in the JSP,
<c:choose>
<c:when test="${myBean.title eq 'P'}">Peer</c:when>
<c:when test="${myBean.title eq 'T'}">Team</c:when>
</c:choose>
If you're not familiar with JSTL, I'd recommend reading through the JSP section of the Java EE 5 Tutorial, or picking up a copy of Head First Servlets and JSP (it's quite good).
You should use JSTL. Here is an example:
<%# taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<c:if test="${yourClass.p eq 'P'}">PEER</c:if>
I used properties to set and get data from the user, by creating a java class handler and use a "useBean"... Here is some code I generated to fix my problem... hope it helps..
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login and Do Action</title>
</head>
<% // java code
%>
<body>
<jsp:useBean id="myBean" scope="session" class="org.mypackage.IFPWAFCAD.NameHandler" />
<jsp:setProperty name="myBean" property="name"/>
<jsp:setProperty name="myBean" property="screenName"/>
<jsp:setProperty name="myBean" property="username"/>
<jsp:setProperty name="myBean" property="password" />
<h1>Hello <jsp:getProperty name="myBean" property="screenName" />! Ready to Login and perform Action</h1>
<form method="post" action="DBConnection">
<table border="0">
<thead>
<tr>
<th>
</th>
<th>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>Table:</td>
<td><input type="radio" name="tableName" value="product"/>Produce
<<input type="radio" name="tableName" value="customer" />Customer
<input type="radio" name="tableName" value="actor" checked="checked" />Actor</td>
</tr>
<tr>
<td>Action:</td>
<td>
<select name="action" value="0">
<option value="0">Choose a Action...</option>
<option value="create">Create</option>
<option value="read">Read</option>
<option value="update">Update</option>
<option value="delete">Delete</option>
<option value="fancy">Fancy Display</option>
<option value="pass">Pass to JSP File</option>
</select>
</td>
</tr>
<tr>
<td>Record ID:</td>
<td><input type="text" name="tid" size="3"/></td>
</tr>
<tr>
<td>
First Name:
</td>
<td>
<input type="text" name="firstname" size="30"/>
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<input type="text" name="lastname" size="30"/>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="hidden" name="name" value="<jsp:getProperty name="myBean" property="name" />" />
<input type="hidden" name="username" value="<jsp:getProperty name="myBean" property="username" />" />
<input type="hidden" name="screenName" value="<jsp:getProperty name="myBean" property="screenName" />" />
<input type="hidden" name="password" value="<jsp:getProperty name="myBean" property="password" />" />
</td>
</tr>
</tbody>
</table>
<input type="submit" value="Login" />
<input type="reset" name="clear" value="Clear" />
</form>
</body>