Want to display file list last modified in JSP - spring-mvc

I am using spring mvc 3 and my controller is passing a List from the controller to the JSP:
List suitableMatches = new ArrayList();
...//Some code that adds Files to this list
model.addAttribute("lists", suitableMatches);
and in my jsp :
<tr>
<td class="readOnly">${list.name}</td>
<td class="readOnly">${idiomSearch.testCaseID}</td>
<td class="readOnly">${list.lastModified}</td>
<td class="readOnly">Download</td>
</tr>
But I am getting the error :
Error 500: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.el.PropertyNotFoundException: Property 'lastModified' not found on type java.io.File
Can anyone tell me please ,how is it that list.lastModified is not working?

Depending on the version of the JSP Expression Language that your web server is using you might not be able to access non-getter methods with this syntax.
${list.name} works as File has a method called getName(). However, File does not have a method called getLastModified(), the method is just called lastModified() so you have to add brackets when calling a method that is not a getter.
So the code should read ${list.lastModified()} and you will need to use at least v2.2 of the EL jar so this will need to either be included explicitly in the web application or use a servlet container that already includes this library.
A good source for the el syntax can be found in stackoverflow's el tag description

you can use a combination of JSTL and expression language for this like
<jsp:useBean id="dateValue" class="java.util.Date"/>
<jsp:setProperty name="dateValue" property="time" value="${file.lastModified()}"/>
<fmt:formatDate pattern="yyyy-MM-dd HH:mm:ss z" value="${dateValue}" />

Related

Thymeleaf: how use the dates.format method with i18n through object model

I am working with Spring Framework and Thymeleaf 3.0.9.RELEASE
About a list report, In a #Controller exists the following:
model.addAttribute("personas", personaService.findAll());
model.addAttribute("dateFormatPattern", "date.format.pattern");
I have read the following:
Thymeleaf: Use #dates.format() function for format date with internatinalization.
Thus in the view exists the following:
<tbody>
<tr th:each="persona : ${personas}" >
<td th:text="${personaStat.count}">###</td>
<td th:text="${persona.id}">###</td>
<td th:text="${persona.nombre}">###</td>
<td th:text="${persona.apellido}">###</td>
<td th:text="${#dates.format(persona.fecha, #messages.msg('date.format.pattern'))}">###</td>
Until here "${#dates.format(persona.fecha, #messages.msg('date.format.pattern'))}" works fine.
Thus from:
model.addAttribute("dateFormatPattern", "date.format.pattern")
you can see that date.format.pattern (the value of the attribute) is used directly in #messages.msg('date.format.pattern').
Thus until here model.addAttribute("dateFormatPattern", "date.format.pattern") is useless or is not need it
I want to know (if is possible) how would be the correct syntax to use the dateFormatPattern (the key of the attribute) instead, it to expect that Thymeleaf retrieve the date.format.pattern value and then retrieve the correct pattern from the .properties file.
Reason: the scenario is where there is a high potential possibility to change date.format.pattern value to other and thus it to be reflected through many views (html files about reports where the Date field is used) than the dateFormatPattern key instead.
Thus if date.format.pattern is changed to date.format.abc.pattern it should be in the server side, not for each .html file. Therefore dateFormatPattern (the key of the attribute) remains without changes

Thymeleaf errors messages optimization

I looked at this tutorial: https://spring.io/guides/gs/validating-form-input.
Using th:errors results in <br> separeted error messages. I want to have an unordered list. So I've defined a fragment like this ...
<td th:fragment="validationMessages(field)" th:if="${#fields != null and field != null and #fields.hasErrors(field)}">
<ul>
<li th:each="error : ${#fields.errors(field)}" th:text="${error}"></li>
</ul>
</td>
... and using it with ...
<td th:replace ="form :: validationMessages('age')"></td>
Is there a "clean code" solution / best practice, like overriding the render implementation of th:errors?
You could probably create your own Thymeleaf Processor, based on org.thymeleaf.spring4.processor.attr.SpringErrorsAttrProcessor, that used your own method of delimiting errors, and then use that rather than the one Thymeleaf gives you. It doesn't look particularly designed for extending, though.
I think the way you did it is probably best. I tend to prefer my HTML templates to be in a templating language (like Thymeleaf) rather than in Java code. You can modify as needed (such as adding styling classes) and it's clear what the code does. This is exactly the kind of thing template fragments are made for.

import a created class in jsp

ive got a servlet with a class file contained within file path webapps/ass2/WEB-INF/classes/User.class, the class represents a user bean required for my application. In jsp, ive go the imports organised as `<%#page import="mypck.User" %>. im being thrown an error by apache tomcat when i try to load the page,
An error occurred at line: 12 in the jsp file: /fourm.jsp
User cannot be resolved to a type
9:
10: <head>
11: <%
12: User user = (User)session.getAttribute("userBean");
13:
14: String username = user.getName();
15:
i guess i havent imported the class correctly then? my question is: how do i import this class to the jsp file
a class file contained within file path webapps/ass2/WEB-INF/classes/User.class
...
<%#page import="mypck.User" %>
This doesn't match. The User.class has got to be placed in the mypck folder representing the package. Fix it accordingly: webapps/ass2/WEB-INF/classes/mypck/User.class.
Unrelated to the concrete problem, using scriptlets is discouraged since a decade. I recommend to take a JSP pause and invest some time in learning taglibs and EL. With EL, you can just show the username like follows:
<p>Welcome, ${userBean.name}</p>
See also:
How to avoid Java code in JSP files?
Our EL wiki page
Your import is correct syntax-wise, but is the package declaration correct?

add button in show.jspx in a spring roo mvc project

I've created a spring roo project using 'Getting started with spring roo' as a starting point. The project is created in STS using roo 1.1.5. I've added neo4j graph and is able to create nodes with simple edges and create the web-part issuing 'controller all --package ~.web'.
The project is a simple web-app with a Person and Race node and a Participant-edge with start-time, end-time, total-time and race-id. Since the edge Participant is a #RelatedToVia it becomes a #RelationshipEntity and I want to add a button to save Participant.
I found WEB-INF/tags/form/field/table.tagx where the add-, modify-, delete-buttons and friends are defined, ie.:
<c:if test="${update}">
<td class="utilbox">
..
But where do I set the variable update? I've looked through the code that is created by STS, but unable to find it. Pardon if this is obvious.
regards
Claus
Edit:
I found out that WEB-INF/tags/form/show.tagx have the knobs to enable/disable for instance the update-button:
<c:if test="${empty update}">
<c:set var="update" value="true" />
</c:if>
So I will add my new button in this file. The spring framework seems so well laid out. Just have to find the various places.
regards
Claus
The value for update is obtained from attributes you specify when you use the tag created using the tagx.
For an example,
If form:table was used as in a jspx and if the following was set, you will recieve true in your update variable if it was assigned using a directive. However it seems that the true is set as default in the form:table tag within Spring Roo.
If you want to set it to false, when using you have to set the value to the attribute as following.
<form:table update="false" />
If you want to go deeper into this, look in to the table.tagx file you have mentioned you will find the following line which explains it.
<jsp:directive.attribute name="update" type="java.lang.Boolean" required="false" rtexprvalue="true" description="Include 'update' link into table (default true)" />
Cheers.

How is the Spring MVC spring:bind tag working and what are the meanings of status.expression and status.value?

Let's discuss on the following example:
<spring:bind path="user.userName">
<input type="text" name="${status.expression}" value="${status.value}"/>
<span class="fieldError">${status.errorMessage}</span>
</spring:bind>
When this view snippet gets rendered, what do ${status.expression} and ${status.value} get evaluated to? Where do these values come from?
See this link for an explanation of what the status variables mean.
status.expression: the expression that was used to retrieve the bean or property
status.value: the actual value of the bean or property (transformed using registered PropertyEditors)
status.errorMessages: an array of error messages, resulting from validation
The status object is evaluated when the binding is done.
Also have in mind that Spring 2.0 introduced new form tags, which are probable better suited for your needs.
The bind tag documentation of Spring 3.0
See Also: BindStatus

Resources