I want to display a div with all error messages in my page header. I know how to do for for a single command eg:
<spring:hasBindErrors name="<my_command>">
<div class="error_box">
<c:forEach var="error" items="${errors.allErrors}">
<spring:message message="${error}"></spring:message><br />
</c:forEach>
</div>
</spring:hasBindErrors>
but I would like to have global div for every command So I wouldn't need to repeat nearly same code. Any help appreciated :)
The only idea I have is:
<%
Enumeration en = request.getAttributeNames();
while(en.hasMoreElements()) {
String attr = (String)en.nextElement();
if(attr.startsWith("org.springframework.validation.BindingResult")) {
%>
<spring:hasBindErrors name='<%= attr.substring(attr.lastIndexOf(".")+1) %>' >
<div class="error_box">
<c:forEach var="error" items="${errors.allErrors}">
<spring:message message="${error}"></spring:message>
<br />
</c:forEach>
</div>
</spring:hasBindErrors>
<%
}
}
%>
Maybe one day I will get a better answer :)
Related
Im trying to get something out of my database to add to a class in my JSP.
<div class="selection <c:out value='${bill.category}'/>">
<c:forEach items="${bills}" var="bill">
<div class="selection <c:out value='${bill.category}'/>">
<p><c:out value="${bill.name}"/></p>
<div class="plus-icon">
<ion-icon size="large" name="add-circle-outline</ion-icon
</div>
</div>
</c:forEach>
Yes , you can do that by using jstl tag like below :
<c:set var="category" value="${bill.category}"/>
And then use above in div like below :
<!--will print whatever is there in category-->
<div class="selection${category}">
</div>
I'm making a form to add events to a list but I need to be able to see in that form the previous events from that list. I have a jsp that shows a list of events but it takes an attribute usually added by the controller when you access the list directly from the browser.
So how can I add the list jsp filling that attribute so it shows a list and not just the headers?
I have alreay included the jsp using
<jsp:include page="comp_list.jsp"></jsp:include>
And it shows the headers but as there is no attribute it shows no list. I tried adding attributes to the include like:
<jsp:include page="comp_list.jsp">
<jsp:attribute name="compensaciones">
${compensaciones}
</jsp:attribute>
</jsp:include>
But when I do it it shows an error telling me that this cannot be done.
Also tried params but that would not be the answer for me because params are treated in the controller and not in the jsp itself.
I'm just getting the header of the list which is fine, but i would like to see the list.
Edit: this is how i build the list in case you are wondering
<tbody>
<c:forEach var="comp" items="${compensaciones}">
<td>${comp.getSomething()}</td>
...
</c:forEach>
</tbody>
<jsp:include page="pagename.jsp" />
i thing you are not provide extension in your include tag
Sometimes since I've had prior experience back in the days with HTML and PHP, we simply just would include things like navigation and header for easier management.
I'm unsure for what purpose you're including JSP, but here's an example of how I've done it since I include JSP if a boolean is true.
The site that is accessed:
<div class="row">
<div class="col-lg-2">
</div>
<div class="col-lg-8">
<%
if (loggedin) {
%>
<%# include file="includes/profile.jsp" %>
<% } else {
out.println("<div><h2>You aren't logged in!</h2></div>");
}
%>
</div>
<div class="col-lg-2">
</div>
</div>
And the top and bottom of the JSP file I'm including doesn't contain things like head, title, html, body etc.
<%
User currUser = null;
currUser = (User) session.getAttribute("user");
%>
<div>
<h2>Du er logget ind, velkommen <% out.println(currUser.getUsername().toUpperCase()); %></h2> <br>
<h5>Din Saldo: <b><% if (currUser.getBalance() < 0) { out.println("<font color='red'>" + currUser.getBalance() + "</font>");} else { out.println("<font color='green'>" + currUser.getBalance() + "</font>");} %></b></h5>
<br>
<form class="form" method="post" action="#">
<h4>Oplysninger: </h4>
<h6>
For at ændre oplysninger skal du indtaste dit kodeord!
</h6>
Nuværende Kode: <input type="password" class="form-control" placeholder="kodeord" name="password" required>
<hr>
Email: <input type="text" class="form-control" value="<% out.println(currUser.getEmail()); %>" name="email"> <br>
Brugernavn: <input type="text" class="form-control" value="<% out.println(currUser.getUsername()); %>" name="username"> <br>
<input type="submit" class="btn btn-default" value="Indsend Oplysninger" />
</form>
</div>
When importing JSP into JSP, it's important to know that they will conflict if 1. Duplicate Local Variables. 2. Certain HTML tags aren't correct.
Your intended use seems very complicated to me since I'm not quite understanding it, sorry :3 but if you can't get it working consider to throw it to a session and if it's a one time use then remove it once you used it.
This is working from dacades
<%# include file="../scripts/standard_head_declaration.jsp" %>
i need some help with these
I have header.jsp, login.jps and register.jsp.
I build my Body with Header.jsp and another JPS.
I want to change my 'Help' link of Header.jsp depending on the Jsp i build. How i can do that?
I looked for javascript solution but is a 'dirty' solucion and Sonar is saying 'God DONT DO THAT'.
More details to explain mysefl, my Header.jsp has that code:
<ul class="pull-right">
<c:if test="${empty sessionScope.USER}">
<li><spring:message code="header.manual"/><span><spring:message code="header.help"/></span></li>
</c:if>
If i build with Login.jsp i want that href linked to "url.com/home.public?help_login" and if is Register.jsp i want href liked to "url.com/home.public?help_register".
INFO: when i use register.jsp my url is "url.com/register.public" otherwise i use login.jsp my url is "url.com/login.public".
Sorry if my english is bad, but i think you will understand it.
EDIT: Layout.jsp
<body class="home" onload="nobackbutton();">
<c:choose>
<c:when test="${not empty sessionScope.ADMIN}">
<tiles:insertAttribute name="headerAdmin" />
<tiles:insertAttribute name="menuAdmin" />
</c:when >
<c:otherwise>
<tiles:insertAttribute name="header" />
<tiles:insertAttribute name="menu" />
</c:otherwise>
</c:choose>
<div class="container-fluid">
<tiles:insertAttribute name="miga" />
<tiles:insertAttribute name="body" />
</div>
</body>
Finally i used a document.ready (javascript) to identify de href and modify it and make sonar ignore that because i found nothing better than these. I don't want lose more time ...
Thnk Hooch for help
Can't you simply do something like the following?
<%
if (${sessionScope.USER} != null) {
%>
<li><spring:message code="header.manual"/><span><spring:message code="header.help"/></span></li>
<%
}else{
%>
<li><spring:message code="header.manual"/><span><spring:message code="header.help"/></span></li>
<%
}
%>
Edit: since you need to know the current page you could check
<%= request.getServletPath() %>
In the header.jsp
I have a webpage that dynamically places a text in a span. It is something like:
<table id=table_1>
<td>
<span> $available/$total <span><label id=label_1>Servers available</label>
</td>
</table>
The only thing i know is that either "x/y Servers available" or "No Servers Available" would be displayed on the webpage. Please let me know how can I get this x and y.
<html>
<body>
<div class="refresh_div" >
<table id="Server_status_table" >
<!-- -----------For Server Status box header ---------------->
<tr>
<%
int num=Integer.parseInt(request.getAttribute("availWorkerCount").toString());
int tot=Integer.parseInt(request.getAttribute("totalWorkerCount").toString());
int avg = 0;
if(tot!=0 && num>0){
avg=num*100/tot;
}
pageContext.setAttribute("total", tot);
%>
<c:choose>
<c:when test="${ total eq 0 }">
<!-- -----------For showing how many servers are available now---------------->
<td>
<label id="Avaibility_l" style="font-size: 120%;">Availability</label>
</td>
<td>
<span>No server available</span>
</td>
</c:when>
<c:otherwise>
<!-- -----------For showing how many servers are available now---------------->
<td>
<label id="Avaibility_l" style="font-size: 120%;">Availability</label>
<br/>  
</td>
<td >
<!-- -----------For showing the availability bar---------------->
<div id="progress" class="progress">
<div id="bar" class="bar" style="width:<%=avg %>%;"></div>
</div>
<!-- -----------For showing the availability progress detail message---------------->
<span>
${availWorkerCount} / ${totalWorkerCount}<label id="Progress_message_l"> occupied</label>
</span>
</td>
</c:otherwise>
</c:choose>
</tr>
<tr>
<!-- -----------For showing the approximate Queue Wait time ---------------->
<td>
<label id="Queue_wait_time_l" style="font-size: 120%;">Queue waiting time</label>
</td>
<!-- -----------For showing the approximate wait time detail message---------------->
<td >
<label id="Wait_time _message_1stpart_l" >Approximately</label>
${queueWaitingTime}
</td>
</tr>
</table>
</div>
</body>
</html>
I need ${availWorkerCount} , ${totalWorkerCount} and ${queueWaitingTime}
I have an idea. Please follow the below steps might work for you, this answer is from the HTML prospective. I not sure what you mean by
I need ${availWorkerCount} , ${totalWorkerCount} and ${queueWaitingTime}
Full example code after the steps. Just to mention, I have not ran this code.
1.Search the Element label by text
label[contains(.,'Servers available')]
you can add id check with it also like
label[contains(#id,'label_1') and contains(.,'Servers available')]
2.get the label and then get the parallel element span. Span should have the value
CODE :
List<WebElement> elements = driver.findElements(By.xpath("label[contains(#id,'label_1') and contains(.,'Servers available')]");
for(// now loop over the elements)
{
WebElement elem = elements.get(0);
List<WebElement> spanelems = elem.findElements(By.xpath("../span"));
spanelems.get(0).getText() // this should give you the data under the span
}
With WebDriver you're picking text off from the page after it has finished loading. Thus getting this kind of "dynamic" text won't be a problem. Your example is only dynamic on the server side, in the client side the text from these variables would be static content and just as easy to grab from the page as any other text.
I suggest you put an id attribute to the <span> you're looking for, then it's super simple to get its contents with Selenium.
For example:
<span id="myStuff">foobar</span>
And fetch it with:
String contentsOfMySpan = driver.findElement(By.id("myStuff")).getText();
* EDIT *
Since you can't change original code, here's the hard way to do it:
By qwt = By.xpath("//label[#id = 'Wait_time _message_1stpart_l']/parent::td/text()[2]");
By wc = By.xpath("//label[#id = 'Progress_message_l']/parent::span/text()[1]");
String queueWaitingTime = driver.findElement(qwt).getText();
String[] workerCounts = driver.findElement(wc).getText().split(" / ");
String availWorkerCount = workerCounts[0];
String totalWorkerCount = workerCounts[1];
I am having a problem with JSTL and empty operator. I already made few simple pages and everything worked fine, but now I have:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<form action="/Projekt/myaccount" method="post">
<table border="1">
<tr>
<td>Artist</td>
<td>Record Name</td>
<td>Delete</td>
</tr>
<c:forEach var="item" items="${records}">
<tr>
<td>${item.artist}</td>
<td>${item.recordName}</td>
<td>
<input type="checkbox" name='${item.recordName}|${item.recordName}'/>
</td>
</tr>
</c:forEach>
</table>
<hr/>
<input type="submit" name="back" value="back"/>
<c:if test='${not empty "${records}"}'>
<input type="submit" name="delete" value="delete selected"/>
</c:if>
</form>
</body>
</html>
now no matter if I set the records attribute or not, the delete button shows up:
<c:if test='${not empty "${records}"}'>
<input type="submit" name="delete" value="delete selected"/>
</c:if>
in normal situation to records attribute I pass ArrayList and then use foreach, but sometimes ArrayList is empty, so in those situations I don't want delete button to show up, I fought that easiest way to achieve this would be to use this empty operator. Where am I making a mistake?
I even tried to manually set this attribute to null:
if (ar.size() != 0)
request.setAttribute("records", ar);
else
request.setAttribute("records",null);
EDIT:
#Qwe: yes you are right, it worked for me before because I tested if attribute was empty in my way, it was always true, because I used wrong construct, but it worked because I just wanted to show one string, if there was no String nothing showed up so I was thinking that everything worked fine.
<c:if test='${not empty "${records}"}'> as well as <c:if test="${!empty '${showWarning}'}"> (from your comment) will always resolve to true because you're are actually testing if a String ${records} is empty or not, and obviously it is not.
Just to be sure - by String ${records} I mean a String value, just as if you were assigning it in Java like String foo = "${records}";.
The next line of code will test if records variable (that is looked up from page, request, session or application scope) is empty or not:
<c:if test="${not empty records}">
The line of code is 100% guaranteed to work :)
Also, request.setAttribute("records",null) is a bad way to remove attributes because empty tests not just request scope, but page, session etc. Use <c:remove var='records'/> instead.