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" />
Related
I have used jstl to display particular data from table named status(all columns are string type) on found.jsp. In my table I have entered 1 and 0 under status column where 1 is for IN and 0 is for OUT.
found.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# page import="java.io.*,java.util.*,java.sql.*"%>
<%# page import="javax.servlet.http.*,javax.servlet.*" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Found page</title>
<style>
header {
background-color:teal;
color:white;
text-align:center;
padding:5px;
}
section {
height:270px;
width:1050px;
float:right;
padding:87px;
}
footer {
background-color:black;
float:bottom;
color:white;
clear:both;
text-align:center;
padding:5px;
}
</style>
</head>
<body style="background-color:lightsteelblue;">
<header><h3>File Status!!</h3>
<br>
</header>
<font color="black">back</font>
<form action=" LogoutServlet" method="post">
<input type="submit" value="Logout" >
</form>
<form method="POST">
File Number:<input type="text" name="status" value="" size="20" />
<input type="submit" value="submit" name="submit" />
</form>
<br>
<section>
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/login"
user="root" password="root"/>
<sql:query dataSource="${snapshot}" var="result">
SELECT * from status where fname="${param.status}";
</sql:query>
<table border="1" width="100%">
<tr>
<th>File Number</th>
Below is the column where 1 and 0 are shown under
<th>File Status(IN=1 and OUT=0)</th>
<th>File Department</th>
<th>Date and Time</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td><c:out value="${row.fname}"/></td>
value comes from here
<td><c:out value="${row.fstatus}"/></td>
<td><c:out value="${row.department}"/></td>
<td><c:out value="${row.datetime}"/></td>
</tr>
</c:forEach>
</table>
</section>
<footer>
Copyright 2016 NSIC. All right reserved.
</footer>
</body>
</html>
I don't want 1 and 0 to be shown instead i want IN and OUT to be shown under the column and I don't want to change the database table entery from 1 and 0 to IN and OUT Is there a way I can do this?
you can do it with <c:choose> tag
<c:choose>
<c:when test="${row.fstatus=='1'}">
IN
</c:when>
<c:otherwise>
OUT
</c:otherwise>
</c:choose>
I have a problem with sending <form> to controller. When i'm pressing submit button it shows me 400 error. And when i'm trying to output smth into console in POST method it doesn't show it. Why controller doesn't get my form? Thanx!
Here is my JSP page
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%#taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%# taglib uri="http://www.springframework.org/security/tags"
prefix="sec"%>
<%#page language="java" session="true"%>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
%>
<!DOCTYPE html>
<fmt:requestEncoding value="utf-8" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Поиск оппонента | Haine and Vold</title>
<link rel="shortcut icon" href="<c:url value="/resources/faviicon/iconHV.ico"/>" type="image/x-icon">
<link href="<c:url value="/resources/bootstrap/bootstrap.css"/>"
rel="stylesheet" type="text/css">
<link href="<c:url value="/resources/CSS/styles.css"/>" rel="stylesheet"
type="text/css">
</head>
<body>
<div class="container">
<img class="img-responsive logo" src="/app/resources/logo.jpg">
</div>
<h5 class="text-center noparamserror">${noparams}</h5>
<c:url var="getSearch" value="/user/search" />
<br>
<div class="container text-center">
<form:form class="form-inline centerBlock" modelAttribute="users"
method="POST" action="/app/user/search" accept-charset="utf-8">
<div class="form-group">
<label class="control-label" for="weight"><spring:message
code="label.searchparamweight" /></label>
<form:select multiple="" class="form-control input-sm" path="weight"
name="weight">
<form:option value=""></form:option>
<form:option value="40"></form:option>
<form:option value="50"></form:option>
<form:option value="60"></form:option>
<form:option value="70"></form:option>
<form:option value="80"></form:option>
<form:option value="90"></form:option>
<form:option value="100"></form:option>
<form:option value="110"></form:option>
<form:option value="120"></form:option>
</form:select>
</div>
<div class="form-group">
<label class="control-label" for="gender"><spring:message
code="label.gender" /></label>
<form:select multiple="" class="form-control input-sm" name="gender"
path="gender">
<form:option value=""></form:option>
<form:option value="Мужской"></form:option>
<form:option value="Женский"></form:option>
</form:select>
</div>
<div class="form-group">
<label class="control-label" for="place"><spring:message
code="label.place" /></label>
<form:select class="form-control input-small" name="place"
path="place">
<form:option value=""></form:option>
<form:option value="Дзержинский"></form:option>
<form:option value="Киевский"></form:option>
<form:option value="Комминтерновский"></form:option>
<form:option value="Лененский"></form:option>
<form:option value="Московский"></form:option>
<form:option value="Октябрский"></form:option>
<form:option value="Орджоникидзевский"></form:option>
<form:option value="Фрунзенский"></form:option>
<form:option value="Холодная гора"></form:option>
<form:option value="Червонозаводской"></form:option>
</form:select>
</div>
<div class="form-group">
<label class="control-label" for="age"><spring:message
code="label.searchparamage" /></label>
<form:select multiple="" class="form-control input-small" name="age"
path="age">
<form:option value=""></form:option>
<form:option value="19"></form:option>
<form:option value="20"></form:option>
<form:option value="21"></form:option>
<form:option value="22"></form:option>
<form:option value="23"></form:option>
<form:option value="24"></form:option>
<form:option value="25"></form:option>
<form:option value="26"></form:option>
<form:option value="27"></form:option>
<form:option value="28"></form:option>
<form:option value="29"></form:option>
<form:option value="30"></form:option>
<form:option value="31"></form:option>
<form:option value="32"></form:option>
<form:option value="33"></form:option>
<form:option value="34"></form:option>
<form:option value="35"></form:option>
<form:option value="36"></form:option>
<form:option value="37"></form:option>
<form:option value="38"></form:option>
<form:option value="39"></form:option>
<form:option value="40"></form:option>
<form:option value="41"></form:option>
<form:option value="42"></form:option>
<form:option value="43"></form:option>
<form:option value="44"></form:option>
<form:option value="45"></form:option>
</form:select>
</div>
<button class="btn btn-success" type="submit">
<spring:message code="label.search" />
</button>
</form:form>
</div>
<br>
<div class="scrollTableSearch">
<c:if test="${!empty listUsersSort}">
<table
class="table table-striped table-bordered table-condensed table-hover">
<tr>
<th><spring:message code="label.name" /></th>
<th><spring:message code="label.surname" /></th>
<th><spring:message code="label.gender" /></th>
<th><spring:message code="label.weight" /></th>
<th><spring:message code="label.height" /></th>
<th><spring:message code="label.sport" /></th>
<th><spring:message code="label.place" /></th>
</tr>
<c:forEach items="${listUsersSort}" var="users">
<tr>
<td><a href="<c:url value='/user/search/${users.username}'/>"
target="_blank"> ${users.name}</a></td>
<td>${users.surname}</td>
<td>${users.gender}</td>
<td>${users.weight}</td>
<td>${users.height}</td>
<td>${users.sport}</td>
<td>${users.place}</td>
</tr>
</c:forEach>
</table>
</c:if>
</div>
</body>
<script src="<c:url value="/resources/Jquery/jquery-2.1.4.min.js"/>"
type="text/javascript"></script>
<script src="<c:url value="/resources/bootstrap/bootstrap.js"/>"
type="text/javascript"></script>
</html>
Here is my controller method.
#RequestMapping(value = "/user/search", method = RequestMethod.POST,produces = "text/plain;charset=UTF-8")
public String listUsersSort(
#RequestParam(value="weight") int weight,
#RequestParam(value="gender") String gender,
#RequestParam(value="place") String place,
#RequestParam(value="age") int age,
Model model, Principal principal)
{
System.out.println("int");
if(weight<40 && gender.equals("") && place.equals("") && age<19)
{
model.addAttribute("users", new Users());
model.addAttribute("noparams", "Задайте параметры поиска");
return "searchingUser";
}
else{
model.addAttribute("users", new Users());
model.addAttribute("listUsersSort", usersService.listUsersSort(weight, gender, place, age, principal.getName()));
model.addAttribute("allUserMess", this.dialogService.allNewMessForUser(principal.getName()));
model.addAttribute("countOfFeedbacks", this.feedbackService.checkUnreadFedbacks().size());
model.addAttribute("countOfNewUsers", this.usersService.countOfNewUsers());
return "searchingUser";
}}
You're getting the error because your code is somewhere in between using the form-backing bean, or binding the parameters from request. You should either remove the
modelAttribute and path attributes from your form.
Or remove the #RequestParam annotated arguments from your request, and create a form-backing object with weight, age, place, gender properties, and add the object to the controller method signature annotated with #ModelAttribute("users")
In your JSP page, actually you are missing the Context Root while giving request to your controller.
<form:form class="form-inline centerBlock" modelAttribute="users"
method="POST" action="/app/user/search" accept-charset="utf-8">
In this case the request url will be :
http://yourHost:portNo/app/user/search
So to solve this problem you can include your context root with the action:
<form:form class="form-inline centerBlock" modelAttribute="users"
method="POST" action="/your-context-root/app/user/search" accept-charset="utf-8">
OR by writing in this way:
<spring:url value="/app/user/search" var="formUrl"/>
<form:form class="form-inline centerBlock" modelAttribute="users"
method="POST" action="${formUrl}" accept-charset="utf-8">
My java code is
#RequestMapping(value = "/common_views/view-lecturer-Projects")
public String viewLecturerProjects(HttpServletRequest request, ModelMap map) {
String userId=request.getParameter("userId");
map.put("viewUserProjects", projectService.findProjectByUserId(Long.valueOf(userId)));
System.out.println("=================>Project type= "+projectService.findProjectByUserId(Long.valueOf(userId)).getTypeOfProject());
System.out.println("=================>Project title= "+projectService.findProjectByUserId(Long.valueOf(userId)).getTitleOfProject());
// System.out.println("===============>Project= "+(projectService.findProjectByUserId(Long.valueOf(userId))).get(0).getTitleOfProject());
return "common_views/viewLecturerProjects";
}
the page /common_views/view-lecturer-Projects is
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Index - SIL-Bridge</title>
</head>
<body>
<jsp:include page="/WEB-INF/includes/header.jsp" flush="true" />
<jsp:include page="/WEB-INF/includes/menubar.jsp" flush="false" />
<div class="main main-inner">
<div class="container">
<div class="row">
<div class="span12">
<div class="widget widget-nopad">
<div class="widget-header"><i class="icon-list-alt"></i>
<h3>Home</h3>
</div>
<!-- /widget-header -->
<div class="widget-content">
<form id="view-all-lecturer-project" class="form-horizontal">
<fieldset>
<table id="viewAllLecturersProject" class="table table-striped table-bordered">
<thead>
<tr>
<th>Title of the project</th>
<th>Type of the project</th>
</tr>
</thead>
<tbody>
<c:forEach items="${viewUserProjects}" var="viewUserProject">
<tr>
<td><c:out value="${viewUserProject.titleOfProject}"/></td>
<td><c:out value="${viewUserProject.typeOfProject}"/></td>
</tr>
</c:forEach>
</tbody>
</table>
<br/><br/>
</fieldset>
</form>
</div>
</div>
<!-- /widget -->
</div>
</div>
<!-- /row -->
</div>
<!-- /container -->
</div>
<!-- /main-inner -->
</body>
</html>
In this page values ${viewUserProject.titleOfProject},${viewUserProject.typeOfProject}` not disply in this page but java code print values
Write the following taglib import statement on top of your jsp.
<%# taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
and if you dont have JARSS for JSTL, then download from below URL and put that JAR into your Build Path of project.
http://www.oracle.com/technetwork/java/index-jsp-135995.html
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.
Following is my index.jsp:
<%# taglib prefix="sj" uri="/struts-jquery-tags"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<s:head/>
<sj:head/>
<script type="text/javascript" src="jquery.validate.js"></script>
<script type="text/javascript" src="validate.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" media="all" />
<title>My Air - Home</title>
</head>
<body>
<div id="MainPage">
<jsp:include page="header.jsp"></jsp:include>
<div id="mainContent">
<jsp:include page="menubar.jsp"></jsp:include>
<div id="mainContentTop">
<div id="ContentLeft">
<jsp:include page="search.jsp"></jsp:include>
</div>
<div id="contentRight">
<jsp:include page="topdeals.jsp"></jsp:include>
</div>
</div>
</div>
<jsp:include page="footer.jsp"></jsp:include>
</div>
</body>
</html>
Following is my search.jsp:
<%# taglib prefix="s" uri="/struts-tags"%>
<%# taglib prefix="sj" uri="/struts-jquery-tags"%>\
<s:form action="searchFlight" theme="css_xhtml" cssClass="form1small" name="searchFlightForm">
<div class="searchHeadersmall">Search Flight</div>
<div id="searchForm1">
<s:actionerror/>
<s:select list="#application.cityList" headerValue="--------Select--------" headerKey="select" name="searchFlightDetails.from" label="Leaving From"></s:select>
<s:select list="#application.destlist" headerValue="---------Select-------" headerKey="select" name="searchFlightDetails.to" label="Going To"></s:select>
<sj:datepicker id="date0" label="Date" name="searchFlightDetails.dateoftravel" readonly="true" minDate="0"/>
<s:select list="#{'1':'1','2':'2','3':'3','4':'4','5':'5'}" headerValue="" headerKey="" name="searchFlightDetails.noofpassengers" label="No of tickets" style="width:50%; float:left"></s:select>
<div class="inputsmall nobottombordersmall">
<s:radio label="Type" name="searchFlightDetails.nonstop"
list="#{'Y':'NonStop','N':'Normal'}"/>
</div>
<div id="submitdivid1">
<sj:submit value="Show Flights" targets="mainContent"/>
</div>
</div>
</s:form>
When form is submitted the following searchFlightSuccess.jsp is loaded in mainContent <div>:
<%# taglib prefix="s" uri="/struts-tags"%>
<%# taglib prefix="sj" uri="/struts-jquery-tags"%>
<s:if test="#session.username!=null">
<jsp:include page="menubaruser.jsp"></jsp:include>
</s:if>
<s:if test="#session.username==null">
<jsp:include page="menubar.jsp"></jsp:include>
</s:if>
<div id="searchHeaderHistory">Search Results</div>
<div class="CSSTableGenerator">
<table>
<colgroup>
<col span="1" style="width: 5%;">
<col span="1" style="width: 10%">
<col span="1" style="width: 15%">
<col span="1" style="width: 10%">
<col span="1" style="width: 15%">
<col span="1" style="width: 15%">
<col span="1" style="width: 15%">
<col span="1" style="width: 5%">
<col span="1" style="width: 10%">
</colgroup>
<thead>
<tr>
<td colspan="9">From:<s:property
value="searchFlightDetails.from" />
To:<s:property value="searchFlightDetails.to" /></td>
</tr>
</thead>
<tbody>
<tr>
<td>Flight ID</td>
<td>Provider</td>
<td>Departure Source</td>
<td>Via</td>
<td>Via Arrival</td>
<td>Via Departure</td>
<td>Destination Arrival</td>
<td>Fare</td>
<td></td>
</tr>
<s:iterator value="searchedFlightsList" var="flights">
<tr>
<td align="center"><s:property
value="#flights.flightDetails.fid" /></td>
<td align="center"><s:property
value="#flights.flightDetails.providerDetails.pname" /></td>
<td align="center"><s:property value="#flights.sourcedepdate" />,<s:property
value="#flights.sourcedeptime" /> hrs</td>
<td align="center"><s:property
value="#flights.flightDetails.routeDetails.via" /></td>
<td align="center"><s:property value="#flights.viaarrdate" />,<s:property
value="#flights.viaarrtime" />hrs</td>
<td align="center"><s:property value="#flights.viadepdate" />,<s:property
value="#flights.viadeptime" />hrs</td>
<td align="center"><s:property value="#flights.destarrdate" />,<s:property
value="#flights.destarrtime" />hrs</td>
<td width="5%" align="center"><s:property
value="#flights.flightDetails.fares2d" /></td>
<td width="5%" align="center">
<s:form
action="passengerDetailsLink" method="get" theme="simple"
id="passengerDetailsLink_%{#flights.sid}">
<s:hidden value="%{#flights.sid}" name="sid" />
<s:hidden value="%{#flights.flightDetails.fares2d}" name="fare" />
<s:hidden value="%{searchFlightDetails.from}" name="from" />
<s:hidden value="%{searchFlightDetails.to}" name="to" />
<s:hidden value="%{searchFlightDetails.noofpassengers}"
name="passengers" />
<s:hidden value="1" name="flag" />
<sj:submit value="Book" targets="mainContent"
cssClass="orangebuttonsmall" />
</s:form></td>
</tr>
</s:iterator>
</tbody>
</table>
</div>
The scenario is as follows:
I entered some values in search.jsp. I submitted the form, and accordingly some results got loaded into the mainContent <div>. Since form was submitted using Struts 2 jQuery plugin submit , the URL remains the same and hence browser back button doesn't work. If I want to go back, how can I do that.
I read about some hash technique but , I am not able to apply it to this.
In the header tag of Struts2 jQuery use ajaxhistory attribute. Setting this attribute to true enabled browser history for Ajax requests. For details see wiki of header tag.