JSP property lookup error - java

I'm getting the following error in ours logs:
Error looking up property "foo" in
object type "foo.bar". Cause: null
java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedMethodAccessor363.invoke(Unknown
Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1773)
I cannot for the life of me recreate it, I was wondering if anyone has any experience with this kind of problem with JSP/Java Bean. What I wanted to know was, will this prevent the user from getting the web page to show up?
I know this isn't a whole lot of information, but any advice could help.

Something on some page is trying to "navigate" into a bean instance (a Java object, that is), and it's trying to get to a property that isn't there on the bean in question.
<span id='name'>${fn:escapeXml(someBean.user.fullName)}</span>
If the bean "someBean" has no "user" property, of if the user object has no "fullName" property, you get an exception like that.

From what you're giving here, the only suggestion I have is to make sure that you have indeed a property called "foo" and to not have a period in "foo.bar". You cannot name your variables/objects using a period in the name. JSP will automatically go and look for a property called "bar" in "foo". Call it instead "fooBar".

Java is calling the getter method on the bean providing the property which is in turn throwing an exception. If you can see the target exception - that is the target of InvocationTargetException you will know what is causing this to fail.

Related

Thymeleaf - throw exception is template variable not found in Context

I want Thymeleaf to throw an exception if a variable used in a template is not found in the Context. It seems by default Thymeleaf will inject empty text in an HTML tag if the bound variable is not found in the Context. This seems dangerous as it essentially silently hides errors.
<div data-th-text="${amount}">Blah</div>
Code:
Context context = new Context();
// never set "amount" variable
Output:
<div></div>
I assumed there would be a "strict" mode where it would throw an exception with the variable name and additional context if the variable is not found in the Context. I've been searching stackoverflow and the API docs but cannot find anything like this. I thought of overriding the Context getVariable so that it does a containsVariable check, but I'm not sure if that has performance implications. Also, I cannot capture any metadata about where in the template it failed. Am I missing something obvious?
I'm using Thymeleaf as a standalone engine -- not as part of a web/spring app.
You can do this by taking advantage of th:assert. If this assertion is not satisfied then thymeleaf will throw an exception during document processing and print a relevant error to know why it failed.
So your code will be
<div th:assert="${amount} != null" data-th-text="${amount}">Blah</div>
If context variable amount is never set it would be null so then the assertion will fail which will produce the exception.

Magnolia Validation field

When editing an Input Field and trying to add a Validator I get this error:
The validator field I'm talking about
Cannot convert class java.util.LinkedHashSet to class java.lang.String
I understand the error but not sure how to fix it.
Did anyone run into this problem and how did you fix it?
My magnolia version: 5.4.6
Thanks!
If I guess the context correctly, this validation field is from the Form module; more precisely it is a Twin-column field, configured in the formEdit dialog.
The "Cannot convert class" error usually occurs when the Vaadin field is incompatible with the Magnolia/JCR property it's trying to save to.
Use the JCR Browser app to see (and delete) if there is any pre-saved String value for the validation property of this component. Well-formed "multi-values" are displayed within square brackets, e.g. [email].
I could not reproduce this error on the Magnolia Demo—currently running 5.6.3, so if the above doesn't solve it, this could come from the dialog/field configuration, if you use a custom one. As a reference, the formEdit dialog is configured at:
/modules/form/dialogs/formEdit/form/tabs/tabMain/fields/validation.

JavaBeans: Can not read Variables from JavaBeans class

i got a simple programm that begins on an input form where the user fills in 3 numbers. The form action refers to a controller servlet where i store the data in the Bean class with the setter methods I have defined.
number.setNumber1(Double.parseDouble(request.getParameter("number1")));
Till now I stored the Number object in the request with
request.setAttribute("numbers", number);
and forwarded it to the output page where i could get it with ${numbers.biggestNumber ( getter-Method that simply determines the biggest Number) }. A tutorial i am using says I could also get the data directly from the Bean by using this piece of code:
<jsp:useBean id="num" scope="session" class="model.Numbers"/>
<c:out value="${num.biggestNumber}/>
but somehow the Bean uses another object of the Numbers-class. I see the advantage of this technique, because I dont have to put the Numbers object into the request. Can someone tell me how I can use the same Numbers object I stored the data before?
I already read that I shouldnt use "jsp:setProperty..." to store the data on the input page, but if i cant get the information i wrote manually to the Bean, I have to ask myself why I should use the JSP JavaBeans annotation at all.
I used the search function but could not find an answer suitable to my question, or maybe I am just not experienced enough to get them in a more advanced context... Any help would be welcome
if you are using this one,
request.setAttribute("numbers", number);
And using requestDispacher redirecting then at target page you can do likewise,
into JSP file :
<jsp:useBean id="numbers" scope="request" class="model.Numbers"/>
<c:out value="${numbers.biggestNumber}/>
Here,
you did with wrong scope=session, means you are putting value into 'request' scope and try to pull it from session is wrong.
also maintain name of attribute 'same' while putting/getting from scope. here, name="numbers" maintain while putting/getting
Okay my mistake was that I thought JavaBeans-Jsp-Tags would save time and code. Indeed I had to create a HttpSession-Object that stores the ID of the used JavaBean
HttpSession sess = request.getSession(true);
sess.setAttribute("number", number);
Now the JavaBean-Tag in my Output.jsp knows which object to use (the one created in the Input.jsp). As far as I do understand now, the only advantage of the JavaBean-Jsp-Tag above normal Parameters added to the Request is that I can use the Bean-Class in the whole Session and am not dependend on the Request-Object.

Freemarker and Struts 2, sometimes it evaluates as a sequence+extended_hash

First let me say that using Struts2 + Freemarker is a real blast.
Yet there's something is driving me crazy, because I cannot understand why it happens. I ask here as maybe someone else has an idea to share about it.
I've got an action, with a property.
Say
private String myText;
Then I've got a setter and a getter:
public void setMyText(String myText)
{
this.myText = myText;
}
public String getMyText()
{
if (myText == null)
myText = "(empty)";
return this.myText;
}
The result (in struts.xml) is a freemarker result.
So in my Freemarker template there's a line like the following:
<p>The text is: ${myText}</p>
Now consider I'm calling the action without any text parameter: say the url is
http:localhost:8080/myapp/myaction
As the getter provides a default value, when the action is processed and the result passed to my template, the property is set to the default; so I get (html on the browser side)
<p>The text is: (empty)</p>
If I call my action with the parameter set, instead (I mean with something like:
http:localhost:8080/myapp/myaction?myText=hallo
) things go wrong. Freemarker fires the following exception:
Exception occurred during processing request: For "${...}" content:
Expected a string or something automatically convertible to string
(number, date or boolean), but this has evaluated to a
sequence+extended_hash (String[] wrapped into f.e.b.ArrayModel)
It seems that "myText" is found twice...
What am I doing wrong? Or, at least, is there anyone that can explain to me why it happens?
P.S.: it's really found twice; the following is a way to workaround the problem:
<#if myText?is_sequence>${myText[0]}<#else>${myText}</#if>
Yet it seems to me not viable to wrap every variable in that way.
P.P.S.: a further hint: in the freemarker template there's a call to another action some lines before. Something like:
<#s.action var="innerAction" name="getTable" namespace="/foo" />
If I comment the line above, everything works fine.
The myText could be a variable from the freemarker context, but if you want to use action property
<p>The text is: ${action.myText}</p>
Note, that action prefix is not required to access action properties. A property resolution method is applied when resolving freemarker variables:
Property Resoloution:
Your action properties are automatically resolved - just like in a
velocity view.
for example ${name} will result in stack.findValue("name"), which
generally results in action.getName() being executed.
A search process is used to resolve the variable, searching the
following scopes in order, until a value is found :
freemarker variables
value stack
request attributes
session attributes
servlet context attributes
And later you can read what objects are accessible from the context.
Objects in the Context:
The following variables exist in the FreeMarker views
req - the current HttpServletRequest
res - the current HttpServletResponse
stack - the current OgnlValueStack
ognl - the OgnlTool instance
This class contains useful methods to execute OGNL expressions against arbitary objects, and a method to generate a select list using
the <s:select> pattern. (i.e. taking the name of the list property, a
listKey and listValue)
struts - an instance of StrutsBeanWrapper
action - the current Struts action
exception - optional the Exception instance, if the view is a JSP exception or Servlet exception view
The error might be caused by searches from the value stack and returning something that you didn't expect depending on the structure of the stack at the moment of execution.
Adding a prefix to the variable to point out the exact location of the property should fix the redundancy in the code when searching in the value stack.

Spring path binding: is it bound directly to the variable or does it invoke the constructor/setters?

I have a spring bound form (modelAttribute) which displays the user information.
The user's telephone number is displayed in a formatted manner but a requirement is that the number is saved to the database without any signs.
So in the getter method of my user object I format the telephone number according to the rules and in the setter I put the code to remove the special signs.
The formatting part works fine, but setter part where I remove the signs does not seem to occur.
In my constructor I also did:
setTelephoneNumber(TelephoneNumber);
So the constructor also invokes the setter.
I'm using Spring 3.0.4 and Spring-mvc.
Any input on this issue and how to resolve it would be appreciated.
edit:
controller section:
model.addAttribute("user", user);
JSP (shortened it a bit but this is the gist. submitUrl is due to a portal environment:
<form:form action="${submitUrl}" modelAttribute="user">
<form:input path="telephoneNumber"/>
</form>
Model telephoneNumber setter:
if(!StringUtils.isBlank(telephoneNumber)){
this.telephoneNumber = telephoneNumber.replaceAll("[^0-9]", "");
} else{
this.telephoneNumber= "";
}
And I think so because the value lands in the database with the formatting I used. (spacing)
Even if it is not the correct answer to your question:
I strongly recommend to do the formating in an other way then by setter getter
Spring 3.0 provideds something they called "type conversion"
spring blog with example
spring reference "Validation, Data Binding, and Type Conversion"
Using this would be much more cleaner.
Back to your question:
Spring path binding: is it bound directly to the variable or does it invoke the constructor/setters?
As fare I understand the Java Doc and some code snippets, Spring uses BeanWrapper (BeanWrapperImpl) to set the values of Beans (#see Reference: 5.4 Bean manipulation and the BeanWrapper). And BeanWrapperImpl behaves like the reference said:
uses setter and getter to access "simple" values.
It is exactly like the reference said in section "5.4.1 Setting and getting basic and nested properties": For an expression "name":
Indicates the property name
corresponding to the methods getName()
or isName() and setName(..)
So at least this answer your question, so I assume that the cause for your problem is some thing else.

Categories