I have the following page (Spring NVC) which works fine:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Replace Cards</title>
</head>
<body>
<h3>Replace Cards</h3>
<br/><br/>
Give the numbers of cards in your hand you want replace and hit the button<br/>
Valid numbers: 1 to 5, numbers must be separated by space<br/>
#NEW CODE#
<br/>
<sf:form action="execReplaceCards" commandName="cards">
<table>
<tr>
<td><sf:input path="playerName" type="text"/></td>
<td><sf:input path="numbers" type="text"/></td>
<td><input value="Replace" type="submit"/></td>
</tr>
</table>
</sf:form>
</body>
</html>
until I add the following piece of code before the form tag
(marked above as #NEW CODE#):
Here is your hand:
<br/><br/>
<c:out value="Hand name: "/>
<c:out value="${hand.name}"/><br/>
<c:forEach var="card" items="${hand.cards.card}">
<c:out value="Card:[${card.suit} : ${card.rank}]"/><br/>
</c:forEach>
<c:out value="${hand.value}"/>
<br/>
This snippet doesn't do anything except displaying the players hand for reference but since I add this part I've got request syntactically incorrect error. Request never reaches the controller. What's wrong, why is it happening?
Related
I am currently working on a small web app project. The homepage has a link that, once clicked, calls a servlet that retrieves data from a database (in the form of an arraylist of objects), adds arraylist to session(as customers), and redirects to a jsp where the objects (customers) are displayed in a table. There is a "more details" column where each row has a button which submits a form, to another jsp, with a hidden input value with customerNumber as its value and num as its name.
I want, in the second jsp, to loop over all customers and only display the one whose customerNumber matches the one sent from the first jsp.
Current code:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<% String hidden = request.getParameter("num"); %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Customer Details Page</title>
</head>
<body>
<div>
<c:forEach var="c" items="${customers}">
<c:if test = "${hidden == c.customerNumber}">
<table>
<thead>
<tr>
<th>Customer Phone</th><th>Customer Country</th>
</tr>
</thead>
<tbody>
<tr>
<td>${c.phone}</td><td>${c.country}</td>
</tr>
</tbody>
</table>
</c:if>
</c:forEach>
</div>
</body>
</html>
The above code only prints the table heading. Any help appreciated.
Found the issue, was trying to access variable from scriplet directly from jstl tags. The following code works fine:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String hidden = request.getParameter("num");
pageContext.setAttribute("reqNum",hidden);
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Customer Details Page</title>
</head>
<body>
<div>
<c:forEach var="c" items="${customers}">
<c:if test = "${reqNum == c.customerNumber}">
<table border="1">
<thead>
<tr>
<th>Customer Phone</th><th>Customer Country</th>
</tr>
</thead>
<tbody>
<tr>
<td><c:out value="${c.phone}" /></td><td><c:out value="${c.country}" /></td>
</tr>
</tbody>
</table>
</c:if>
</c:forEach>
</div>
</body>
</html>
Instead of using
<td>${c.phone}</td><td>${c.country}</td>
You can try:
<td><c:out value="${c.phone}" /></td><td><c:out value="${c.country}" /></td>
I have Simple Spring form with employee personal info and Contact Info..I have 2 bean EmpPersonalInfo and EmpContactInfo,How do i bind 2 object with "command' and show in empform.
i got this error like Invalid property 'name' of bean class [java.util.HashMap]: Bean property 'name' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
empform
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>Insert title here</title>
</head>
<body>
<form:form action="saveemp" method="post">
<table>
<tr> <td> Name</td>
<td> <form:input path="name"/></td></tr>
<tr> <td>Id </td>
<td><form:input path="id"/> </td></tr>
<tr><td> Current Address</td>
<td><form:input path="empcontactinfo.currentAddress"/> </td> </tr>
</table>
<input type="submit" value="Submit">
</form:form>
</body>
</html>
Empcontroll.class
#RequestMapping("/empform")
public ModelAndView showForm()
{
Map<String,Object> modelmap=new HashMap<String,Object>();
modelmap.put("personalinfo",new EmpPersonalInfo() );
modelmap.put("contactinfo", new EmpContactInfo());
return new ModelAndView("empform","command",modelmap);
}
Path attribute puts value of input field into java properties using java beans convention to be used as #ModelAttribute in your controller method.
#RequestMapping(...)
public String saveEmp(#ModelAttribute("employee") EmpPersonalInfo employee) {
// ...
}
if you just want to display the values inside these two objects in that map, you could use,
<c:out value="${command['contactinfo'].contact}"/>
I'm creating a small shopping cart project where I get a list of products in a linked list from a servlet. Now, I am printing the values of the linkedlist in a table with an option for users to select the quantities they want.
Which ever has quantity selected should go on to the next page as an attribute
Here are few issues I am facing:
1: Since the list has more than 1 items, each item listed should have
input field. How to I dynamically change the input name for the
quantity which I can later use as an attribute.
2: The available quantity of items vary from product to product, how
to I set the max value of the quantity to the available stock.
3: If I can manage to get the values, how do I set all the attributes.
Is it in the for loop or outside the for loop?
Here is the code for the reference.Picture of what the page looks like
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%-- <%# taglib uri="http://java.sun.com/jstl/core" prefix="c" %> --%>
<%#page import="shoppingcart.model.items.*,java.util.*" %>
<%List<ItemDetailsPojo> listp = (List<ItemDetailsPojo>) session.getAttribute("ItemsData");
%>
<%-- <%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> --%>
<Center>
<h3 style="color: blue">Welcome To the World of Shopping</h3>
</Center>
<div align='right'>Logged in as:</div>
<div align="center">
<table border="10" cellpadding="5">
<caption>
<h2>List of Items</h2>
</caption>
<tr>
<th>ItemId</th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th>Available</th>
<th>Quantity</th>
</tr>
<%for(int i=0;i<listp.size();i++){%>
<tr>
<td><%out.println(listp.get(i).getItemId());%></td>
<td><%out.println(listp.get(i).getItemName());%></td>
<td><%out.println(listp.get(i).getCategory());%></td>
<td><%out.println(listp.get(i).getPrice());%></td>
<td><%out.println(listp.get(i).getQuantity());%></td>
<% int number = listp.get(i).getQuantity(); %>
<td><input type="number" name="should dynamically change according to the size of the list" min="0" max="should change according the the quantity available"></td>
</tr>
<%} %>
</table>
</div>
</body>
</html>
Try this:
<td><input type="number" name="quantity<%=i%>" min="0" max="<%=listp.get(i).getQuantity()%>"></td>
and you need to set all the attributes outside the loop.
Edit your code like this
<%List<ItemDetailsPojo> listp = (List<ItemDetailsPojo>) session.getAttribute("ItemsData");
%>
<%-- <%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> --%>
<Center>
<h3 style="color: blue">Welcome To the World of Shopping</h3>
</Center>
<div align='right'>Logged in as:</div>
<div align="center">
<table border="10" cellpadding="5">
<caption>
<h2>List of Items</h2>
</caption>
<tr>
<th>ItemId</th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th>Available</th>
<th>Quantity</th>
</tr>
<%
String name;
for(int i=0;i<listp.size();i++){%>
<tr>
<td><%out.println(listp.get(i).getItemId());%></td>
<td><%out.println(listp.get(i).getItemName());%></td>
<td><%out.println(listp.get(i).getCategory());%></td>
<td><%out.println(listp.get(i).getPrice());%></td>
<td><%out.println(listp.get(i).getQuantity());%></td>
<% int number = listp.get(i).getQuantity();
name="quantity".concat(listp.get(i).getQuantity());
%>
<td><input type="number"name="<%=name%>" min="0" max="should change according the the quantity available"></td>
</tr>
<%} %>
</table>
</div>
</body>
</html>
I am working on designing a web application using Java servlets and JSP and am having issues getting the JSP file to include the CSS file. Each JSP page that is called up includes references to header and footer JSP files and the header file includes the with link to the CSS. All of these files are in a secure resource folder "proforma" within my web application. Prior to trying to include the CSS, the security and the header/footer inclusions were working fine, and continue to do so - they are just not formatted per the CSS.
I have the CSS reference included in a header file as follows:
header.jsp
<%#page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="${pageContext.request.contextPath}/proforma/cssGuide.css" type="text/css"/>
<title>Pro Forma</title>
</head>
<body>
<h1>Pro Forma</h1>
When I use the pageContext Expression Langugae (${}) like the above, the page content that is loaded is the actual CSS content itself, not the actual content I am looking for.
However, if I use the following JSP code, I get the correct HTML content and the browser source review shows the loaded correctly, but there is no CSS source code:
Alternative header.jsp file (ProForma is the name of the application):
<%#page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../ProForma/proforma/cssGuide.css" type="text/css"/>
<title>Pro Forma</title>
</head>
<body>
<h1>Pro Forma</h1>
Here is a sample content page that I am trying to format:
<%#include file="../proforma/header.jsp" %>
<table>
<tr>
<td>
${createProjectResponse}
</td>
</tr>
<tr>
<td>
<form action="/ProForma/downloadInput" method="post">
<input type="submit" name="downloadInputButton" value="Download Input File" class="controlCenterButton"/>
</form>
</td>
</tr>
<tr>
<td>
<form action="/ProForma/AddProject" method="post">
<input type="hidden" name="actionValue" value="addProject"/>
<input type="submit" name="addPortfolioSubmit" value="Add Project" class="controlCenterButton"/>
</form>
</td>
</tr>
<tr>
<td>
<form action="/ProForma/SelectProjectController" method="post">
<input type="hidden" name="actionValue" value="selectProject"/>
<input type="submit" name="selectProjectSubmit" value="Select Project" class="controlCenterButton"/>
</form>
</td>
</tr>
</table>
<%#include file="../proforma/footer.jsp" %>
Here is the CSS file (very simple at this point until I get it working):
input.controlCenterButton{
width: 20em;
height: 2em;
}
For what it is worth, here is the footer.jsp file:
<p id="footer_p">Company</p>
</body>
</html>
Is there something that I am missing that is preventing the CSS file from being included? I appreciate any help.
I'm trying to create a JSP page with a field type "selec" and include in, a list of element which I got from a query in my databse.
My code:
<%boolean list=false;
List listEnv=(List)request.getAttribute("ListEnvironment");
if(listEnv!=null)%>
Environment:<select name="Environment">
<option></option>
<%for(int x=0;x<listEnv.size();x++){
if(x==0){
ListOneElement c=(ListOneElement)listEnv.get(x);%>
<option><%=c.getString1()%></option>
<%}else if(x==1){
ListOneElement c=(ListOneElement)listEnv.get(x);%>
<option><%=c.getString1()%></option>
<%}else if(x==2){
ListOneElement c=(ListOneElement)listEnv.get(x);%>
<option><%=c.getString1()%></option>
<%}else if(x==3){
ListOneElement c=(ListOneElement)listEnv.get(x);%>
<option><%=c.getString1()%></option>
// So on....
<%list=true;%>
<%if(!list){{%>
<%}}%>
<%}}%>
</select>
The above code is working fine, but my problem is that it fail when the result of my query has less elements than options are in my code.
How can I include a loop with the size of my query or something like that, in order to write as many options as element I have in my query. Let's say as dynamic?
Many thanks in advance
Use this on top of jsp file
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
and use for following for iterating
Select
<select>
<c:forEach var="item" items="${ListEnvironment}">
<option><c:out value="${item.string1}"/></option>
</c:forEach>
</select>
Note : never ever use scriptlets
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
Environment:<select name="Environment">
<option></option>
<c:forEach var="item" items="${ListEnvironment}">
<option>
<c:out value="${item.string1}"/>
</option>
</c:forEach>
</select>
</body>
</html>
<%for(int x=0;x<listEnv.size();x++){
ListOneElement c=(ListOneElement)listEnv.get(x);%>
<option><%=c.getString1()%></option><%}
This is already enough, you dont need the if conditions