I've been working local on tomcat server 7, now I uploaded my project to a server but it has tomcat 6. So the following doesn't work:
model.addAttribute("category",cat);
<div class="span4">
<h2>
Category:
<c:out value="${category}" />
</h2>
<br />
<c:forEach items="${categoryList}" var="item">
<div class="alert alert-init">
<c:url value="/getInit/${item.getiID()}" var="url" />
<c:out value="${item.getTitle()}" />
</div>
</c:forEach>
</div>
It gives me following error code:
org.apache.jasper.JasperException: /WEB-INF/views/categoryinitiatives.jsp(34,5) The function iIDGetter must be used with a prefix when a default namespace is not specified
I've been searching all over the web to find a solution for this problem, but without success. Does anyone know how to fix this?
If you wanna access a property of a bean using EL, just write item.title instead of item.getTitle(). It will automatically call the getter and probably solve your problem.
Moreover, item.getiID() isn't a valid name. If you have a property iID in your bean, you have to name the getter getIID() to access it using EL. Have a look at the lowerCamelCase syntax.
Related
I have been working on a system that provides bilingual support to a website from a database using the <spring:message /> tag library.
I can read/write to the applications en/fr properties files. When I hardcode what would be a new key <spring:message /> will display it correctly. Ex) <spring:message code="f12345' /> will display "test data".
What I am having problems doing is using a dynamic key. No matter how I try to attach the key it fails.
Here are the methods I have tried group by end result.
500 error org.apache.jasper.JasperException: (line: [104], column: [29]) [quote/equal] symbol expected
<spring:message var><c:out value="${CLUObject.SpringKey_name()}" /></spring:message>
<spring:message var='<c:out value=\"${CLUObject.SpringKey_name()}\" />" text="wrong" />
<spring:message code="<c:out value="${CLUObject.SpringKey_name()}" />" text="${CLUObject.SpringKey_name()}" />
<spring:message code=<c:out value="${CLUObject.SpringKey_name()}" /> text="${CLUObject.SpringKey_name()}" />
<spring:message code= <c:out value="${CLUObject.SpringKey_name()}" /> />
Next we have the non-server crash which simply causes the table to not display
<spring:message var='<c:out value="application.message" />' arguments="${CLUObject.SpringKey_name()}" />
<spring:message var='<c:out value="${CLUObject.SpringKey_name()}" />' text="wrong" />
<spring:message code="<c:out value=\"${CLUObject.SpringKey_name()}\" />" />
<spring:message code="${CLUObject.SpringKey_name()}" />
<spring:message code="messageCode" arguments="$value1}" />
<c:set var="temp" > <c:out value="${CLUObject.SpringKey_name()}" /> </c:set><td><spring:message code="messageCode" arguments="${temp}" htmlEscape="false" /></td>
The best I have managed to get to simply displays the text of the key instead of the value. Which can already be done using <spring:message text="${CLUObject.SpringKey_name()}" />'
In the applicationResources file
messageCode=Test message for {0}.
Then inside the jsp page
<c:set var="temp" > <c:out value="${CLUObject.SpringKey_name()}" /> </c:set>
displays "Test message for CLUVALUE.C1111."
I found one site online which appeared to be doing the same thing.
<form:select path="${path}">
<c:forEach var="i" items="${items}">
<form:option value="${i[itemValue]}">
<c:choose>
<c:when test="${localize}">
<spring:message code="${i[itemLabel]}" text="${i[itemLabel]}"/>
</c:when>
<c:otherwise>
<c:out value="${i[itemLabel]}"/>
</c:otherwise>
</c:choose>
</form:option>
I have found a second example where they have a dynamic key with the spring message being used in a forEach loop. Search for spring:message and its 14/17.
<c:forEach items="${errors.allErrors}" var="error">
<spring:message code="${error.code}" text="${error.code}"/><br/>
</c:forEach>
I have found a third example.
In the three examples I have found, the spring:message are all used the same, with the code and text attributes being the same.
I cant see the output, and I cant be sure that their text argument isnt being displayed instead.
Here is the controller block
List<CLU_STRUT> myCLUs = cluService.BuildCLUs();
model.addAttribute("CommonLookUp", myCLUs);
And here is the JSP
<c:forEach var="CLUObject" items="${CommonLookUp}" varStatus="vs">
<tr>
<c:set var="temp" > <c:out value="${CLUObject.SpringKey_name()}" /> </c:set>
<td><spring:message code="messageCode" arguments="${temp}" htmlEscape="false" /></td>
<td><c:if test="${CLUObject.getCountNew() gt 0}"> <a href='drillview?drillvalue=${CLUObject.SpringKey_name()}&mode=drill&drilltype=I'><c:out value="${CLUObject.getCountNew()}" /></a></c:if><c:if test="${CLUObject.getCountNew() eq 0}">0</c:if></td>
<td><c:if test="${CLUObject.getCountMod() gt 0}"> <a href='drillview?drillvalue=${CLUObject.SpringKey_name()}&mode=drill&drilltype=U'><c:out value="${CLUObject.getCountMod()}" /></a></c:if><c:if test="${CLUObject.getCountMod() eq 0}">0</c:if></td>
<td><c:if test="${CLUObject.getCountDelete() gt 0}"> <a href='drillview?drillvalue=${CLUObject.SpringKey_name()}&mode=drill&drilltype=D'><c:out value="${CLUObject.getCountDelete()}" /></a></c:if><c:if test="${CLUObject.getCountDelete() eq 0}">0</c:if></td>
<td><c:out value="${CLUObject.getCountTotal()}" /></td>
</tr>
</c:forEach>
TLDR; How to use spring:message where the key is dynamic in a jsp page.
I finally figured out what the issue was.
Short Answer: the inversion of control was causing things to happen now how I expected. I was trying to access a function or member, and it kept trying to use a get version of it. Since I did not have specifically getmember() to access the variable, it was doing strange and wrong things.
By introducing public String getspring_name() {return spring_name;} all of a sudden the <spring:message code="${CLUObject.spring_name}" /> works.
Long Answer: In another section of my code I discovered what I thought was a private string member being accessed in a jsp page. After considerable testing of other private variable types and functions, my original thought that I was gaining access to the private member was disproved. Yet, this specific one was being accessed. After more testing I discovered when I had a get function in a very specific naming convention this behavior was reproducible. So in this case I had a private String CLU_Name, and was lucky enough to have created the get function as getCLU_Name(). Having named other get functions like getCountDelete() for Count_Delete was not allowing me to reproduce the behavior for THEM, because they did not fit the naming pattern.
I have a basic SpringBoot app. using Spring Initializer, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file.
in my property file:
signup.form.error.email.already.exists=Email (already taken)
in my controller:
protected static final String ERROR_MESSAGE_KEY = "errorMessageKey";
model.addAttribute(ERROR_MESSAGE_KEY, "signup.form.error.email.already.exists");
in the template:
<div th:if="${errorMessageKey != null}" class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">x</span>
</button>
<p th:text="#{errorMessageKey}" />
</div>
But this is what I see inthe template instead of Email (already taken)
??errorMessageKey_en_US??
Try using
<p th:text="#{signup.form.error.email.already.exists}" />
When you code this:
model.addAttribute(ERROR_MESSAGE_KEY, "signup.form.error.email.already.exists");
Spring is not resolving the message from the message.properties file: you are simply putting the string "signup.form.error.email.already.exists" in the model...
NOTE the expression #{errorMessageKey} is returning the name of the source file containing the specified message.
Putting properties to Model is not how you resolve the properties in Thymeleaf. See the docs where they explain how to do it properly.
It is because of Thymeleaf is trying to fetch locale message. And by default it is trying en_US.
You can look at this answer
I have below CheckBox in JSP file
<input type="checkbox" name="vehicle" value="Bike"
onclick="javascript:selectCustomers(${sessionScope.custId});">
Getting the following error:
org.apache.jasper.JasperException: customer.jsp(1419,33) According to TLD or attribute directive in tag file,
attribute onclick does not accept any expressions
Can we not use expression language in JavaScript (in my case under onClick() Event)?
When a JSP page is called, the following happens, in this order:
Server checks to see if the .jsp has already been compiled and whether or not it has changed since it was last compiled.
Server runs the jsp through the Jasper compiler, which interprets the jsp into Java code, anything that is not Java (CSS, HTML, JavaScript, etc) is placed in a String.
The Java code is compiled and executed.
The results are placed in the response and sent to the user.
So, your statement: ${sessionScope.custId} is executed before the the HTML is sent to the user, and the input of selectCustomers() function is already set to before calling it.
For more info have a look at my another post JSP inside ListItems onclick
How to verify it?
Right click in the browser and look at the view source.
Try below sample code that might help you.
Enclose ${...} inside the single quotes.
<c:set var="custId" value="1234" scope="session" />
Before :
<c:out value="${sessionScope.custId}"></c:out>
<input type="checkbox" name="vehicle" value="Bike"
onclick="javascript:selectCustomers('${sessionScope.custId}');">
<c:set var="custId" value="4321" scope="session" />
After:
<c:out value="${sessionScope.custId}"></c:out>
View Source code: (Right click in browser to view it)
Before : 1234
<input type="checkbox" name="vehicle" value="Bike"
onclick="javascript:selectCustomers('1234');">
After: 4321
Try this:
<input type="hidden" id="custId" name="custId" value="${sessionScope.custId}">
<input type="checkbox" name="vehicle" value="Bike" onclick="javascript:selectCustomers();">
function selectCustomers(){
var custId = document.getElementById('custId').value;
}
I have some code here.
<c:set var="songId" value="${param.songid}"/>
<c:import var="xml" url="WEB-INF/comment.xml" />
<x:parse var="doc" doc="${xml}" scope="session" />
<c:catch var="ex">
<x:forEach var="cmt" select="$doc//*[songId=$songId]" varStatus="counter">
<li>
<div class="avacmtSide">
</div>
<div class="ctcmtSide">
<x:out select="$cmt/uploader"/>
<div style="padding:10px;"><x:out select="$cmt/comment"/> </div>
</div>
<div class="clear"></div>
</li>
</x:forEach>
When I run it there is a error java.lang.ClassNotFoundException: org.apache.xpath.VariableStack.
When I search google for this error. People said there is missing xalan library. But I have added xalan-2.7.0.jar in my project and it doesn't work. Anyone who know it please help me. Thanks.
The variable syntax is incorrect:
[songId=$songId]
It should be this:
"$doc//*[#songId=${pageScope:songId}]"
Using JSTL Data as XPath Variables
Scoped variables can be used in XPath expressions ($implicitObject:variableName) similar to how they are used in EL (${implicitObject.variableName}). If the implicit object is omitted, scopes will be searched in standard order. Note that the “.” and “[]” notations cannot be used for accessing bean properties.
References
XPath: Variable Reference
JSTL - using variables in an xpath?
JSTL Quick Reference
%{#request.contextPath} doesn't work inside an s:a tag in Struts2. (Struts 2.2.1 to be specific.) Is there a way to make it work? It works in other Struts2 tags.
Here are two lines in a JSP file in a Struts 2 project whose context path is "/websites":
<s:a href="%{#request.contextPath}/clickme" theme="simple">Click here.</s:a>
<s:form method="post" action="%{#request.contextPath}/submitme" theme="simple"></s:form>
And here is the output:
Click here.
<form id="submitme" name="submitme" action="/websites/submitme" method="post"></form>
Notice that the context path is left off the anchor but is included in the form.
P.S. I can't use ${#pageContext.request.contextPath} here because ${} isn't allowed in Struts2 tags. Besides, I'm trying to be consistent. And I also try generally to avoid ${} since it does not auto-escape the output.
Thanks!
This should work:
<s:set id="contextPath" value="#request.get('javax.servlet.forward.context_path')" />
<s:a href="%{contextPath}/clickme" theme="simple">Click here.</s:a>
However, you're not supposed to do this. When you need an url, use the <s:url> tag:
<%-- Without specifying an action --%>
<s:url id="myUrl" value="clickme" />
<s:a href="%{myUrl}" theme="simple">Click here.</s:a>
<%-- With an action --%>
<s:url id="myUrl" action="clickme" />
<s:a href="%{myUrl}" theme="simple">Click here.</s:a>
By the way, you don't need a context path for the action attribute of a form:
<s:form method="post" action="submitme" theme="simple"></s:form>
Does Struts 2 support EL?
You can use ${request.contextPath} if it does....