I have a xhtml file with <ice:outputText value="#{exam.testName}"/>
The part of the webpage associated with that statement just shows the Test Name.
I have the whole java source code and I am trying to find the database query that brings back the test names. I don't know what exam.testName means and what it refers to in the code for it to get the test name for that page.
Thanks!
It is using a JSF (Java Server Faces) component; probably from the ICEFaces component library (you should search your xhtml namespaces to be sure, though)
It shows some text obtained from calling getTestName() in a bean identified as exam (either the bean class is Exam or it has a #Named annotation specifying such a name for the bean, or it is defined in the faces-config.xml file.
Related
I'm creating a debugging tool that logs output for a specific (and commonly used) jsp tag. It would be helpful if i could also log the name of the jsp page and the line number where the custom tag is called.
Is it possible to get access to the jsp name and line number where a custom tag is called from inside the custom tag?
No, you can not get a line number, where a custom tag is called.
Also you can not get exact JSP name, but via PageContext object, which is available to you in TagSupport, you can get servlet name (which will look like 'index_jsp'):
((Servlet)pageContext.getPage()).getServletConfig().getServletName()
But this is not specified in any specification, so naming scheme differs between JSP containers.
Short version
How is one supposed to make nested templates in Thymeleaf when using Spring? It appears asterisk notation is not supported ("*{mailingAddress}") inside th:object attributes in Spring. Is there a work-around / different tag to use?
Long version
For example, let's say I have these classes:
class Address { String street; }
class Person { Address mailingAddress; Address shippingAddress; }
class Order { int orderNo; Person customer; }
So I make an address.html Thymeleaf template:
<span th:text="*{street}"></span>
We test it with a sample Address. Looks good.
and I make a person.html Thymeleaf template that references the address like so:
<span th:text="*{firstName}"></span>
<span th:object="${person.shippingAddress}">
<span th:include="fragments/address :: address"></span>
</span>
And we test it with an example person. I could even reference the same template and set the context to be the ${person.mailingAddress}. So far so good.
Now let's make our Order template. Only, hey, wait. Earlier, in our person.html file we said ${person.shippingAddress} but now we need it to say ${order.customer.shippingAddress}. If I were not using Spring I'd put the following into person.html:
<span th:text="*{firstName}"></span>
<span th:object="*{shippingAddress}">
<span th:include="fragments/address :: address"></span>
</span>
That way, no matter what my path to getting here all I have to care about is that my current context has a shippingAddress. I could then use person.html directly as well as within my order.html template.
Unfortunately I am in Spring, so I get the following exception:
org.thymeleaf.exceptions.TemplateProcessingException:
The expression used for object selection is *{shippingAddress},
which is not valid: only variable expressions (${...}) are
allowed in 'th:object' attributes in Spring-enabled environments.
(include:510)
at org.thymeleaf.spring4.processor.attr.SpringObjectAttrProcessor.validateSelectionValue(SpringObjectAttrProcessor.java:73)
at org.thymeleaf.standard.processor.attr.AbstractStandardSelectionAttrProcessor.getNewSelectionTarget(AbstractStandardSelectionAttrProcessor.java:69)
at org.thymeleaf.processor.attr.AbstractSelectionTargetAttrProcessor.processAttribute(AbstractSelectionTargetAttrProcessor.java:61)
To move forward I must duplicate all my nested templates. In this example, I would have one person.html with th:object="${person.mailingAddress}" calling to address.html, and a duplicate of person.html called orderCustomer.html where we change the line to th:object="${order.customer.mailingAddress}", but is otherwise identical.
Is there a work-around out there that would let me re-use templates?
You can report a bug to the thymeleaf developers in github, or fork the project to add this functionality and convince the Daniel Fernández to accept it.
https://github.com/thymeleaf/thymeleaf/issues
Or else, he is available in StackOverflow. You can simply send him a message about the posibility of integrating this functionality
https://stackoverflow.com/users/550664/daniel-fern%C3%A1ndez
apart from that there is nothing much we can do rather to stick with the approach of putting th:object="${person.mailingAddress}" and th:object="${order.customer.mailingAddress}" outside each import.
I would like to set a value of bean from another bean once a page is displayed. The target dataTable is included because it is also used in another context.
Like I said before, there are two beans involved:
bean1 'gets' a parameter, will load an object and display some properties.
bean2 is the bean that is responsible for the filtering / search in the dataTable.
relevant parts:
<f:metadata>
<f:viewParam name="objectId" value="#{bean1.objectId}" />
</f:metadata>
<!-- resolving works -->
#{bean1.object.name}
included search:
<p:dataTable>...<p:inputText value="#{bean2.value}">...</p:dataTable>
How to assign (a substring of) bean1.object.name to the value of the input text once at page request but keep the existing value attribute of the field? I don't want to mess up the included page but would prefer to solve it "outside" in my including jsf/xhtml file.
The best way to do this is using javascript.
Define a input hidden tag to hold the value of #{bean1.object.name}.
Now in javascript get the value of this field using document.getElementById("").value
Take the substring and assign to input text field using javascript.
I have a t:inputFileUpload inside the form, in the html of the display page the id of this component is form:inputFile but when I tried to get the component from the view root using "form:inputFile" the return is null, but when the "form:" is removed the return is the component. The component don't set the value in my managed bean, someone have this problem?
EDIT:
<h:form id="form" enctype="multipart/form-data">
<t:inputFileUpload id="inputFile" size="40" value="#{managedBean.inputFile}"/>
</h:form>
In the managed bean:
private UploadedFile inputFile;
with the gets and sets provided by Eclipse.
//This method scans the view root and returns the component with the id passed as parameter
findComponentInRoot("form:inputFile");
This returns null, but when I use:
//This method scans the view root and returns the component with the id passed as parameter
findComponentInRoot("inputFile");
The return is the component I'm looking for, but when I use the View Source in Internet Explorer the id of this component is "form:inputFile".
I don't know if this is related, but the component don't set the value in my managed bean and it's strange the fact that the id of the component is different from the HTML source.
I'm using JSF 1.2 Mojarra. Someone else has this problem? Or know why this happens?
EDIT2:
Okay, I'm very stupid, aparently the build wasn't working correctly and when the build was changed to other task from the Ant it worked (still don't know why, but simply worked). Sorry for the trouble.
You should use component binding or UIViewRoot#findComponent(). But that won't solve the problem of the uploaded file not being set. To fix it, first step is to ensure that you definied and configured the ExtensionsFilter properly as per the Tomahawk documentation, since it is the one responsible for parsing the multipart/form-data request and putting all the parameters among with the uploaded file back in the request parameter map, so that the FacesServlet can apply them and update the model values.
I guess findComponentInRoot is this (a minor detail you should've shared).
Anyway, using findComponent(..) or getChildren(..) always return the id of the components as defined in the page. The html id is something different that consists of the naming container:id.
How can I expose a Java bean to a JSP page by using struts? I know how to configure a StrutsAction to include a form-bean, but I wonder if there are other ways to interact with Java code from a JSP page? I ask this question because I don't understand fully a likely answer to a problem that I have asked here:
Clean way for conditionally rendering HTML in view?
EDIT:
I understand that a JavaBean is defined as a class that contains mainly getters and setters for its properties.
My problem was that I did not see how I can access parameters from Java classes in my JSP. Currently, I use a DynaForm to communicate parameters to the view. E.g. in the ActionClass I set the parameter, and in the JSP I can access it with
bean:write name="MyFormBean" property="myParameter"
My question was basically if there are other classes than a DynaForm class that can easily be accessed from inside the JSP with tags, and if so, if someone could provide an example.
In your action class:
MyBean myBean = new MyBean();
myBean.setSomeProperty("someValue");
request.setAttribute("myBean", myBean);
In your JSP:
<bean:write name="myBean" property="someProperty" scope="request"/>
You can do the same with session as well. Note that you don't have to explicitly specify the scope in <bean:write> tag - if you don't, Struts will look in all scopes from page to application.
More information on scopes is available in Java EE tutorial.