java code in inputText - java

i am trying to include a java code into the value of the inputText in my jsf page but an error occur
according to tld or attribute directive in tag file attribute value
does not accept any expressions
Here is my jsf page.
<%# page contentType="text/html" %>
<%# taglib uri="http://java.sun.com/jsf/html" prefix="html" %>
<%# taglib uri="http://java.sun.com/jsf/core" prefix="core" %>
<%# page language="java" %>
<core:view>
<html:form>
<html:outputLabel value="Informations " style="FONT-SIZE: xx-large;"/>
<br />
<br />
<%
final String property=System.getProperty("jboss.server.home.dir");
%>
<html:outputLabel value="RĂ©pertoire de configuration: " />
<html:inputText value='<%=property%>'/>
</html:form>
</core:view>
Doesn ' t work either with double quote or nothing
How to resolve this problem please ?
Thank you very much

The problem is with this line of code:
<html:inputText value='<%=property%>'/>
JSF uses Expression Language to populate/read values to/from a JavaBean. You will have to create a POJO action (called ManagedBean) with a variable property and link it there.
E.g.
public class ConfigurationAction {
private String property = System.getProperty("jboss.server.home.dir");
/**NOTE: MUST create a getter and setter. **/
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
}
Don't forget to map the ManagedBean. In JBoss Seam, you will just add an #Name annotation above the class like, #Name("configurationAction").
Finally, render this in JSF with Expression Language (EL)
<html:inputText value="#{configurationAction.property}"/>
Where configurationAction is the name of your ManagedBean, and property is the instance of the ManagedBean.

Related

Getting variable in JSP EL by constant does not work

I use GlassFish 4.1 web profile which as I understand uses EL 3.0. I did everything as was explained here - https://stackoverflow.com/a/3735006/5057736 however my implementation of this solution doesn't work.
This is my constant class
public class CommonKeys {
public static final String TITLE = "SOME_KEY";
}
This is how I set attribute:
request.setAttribute(CommonKeys.TITLE, "TEST");
This is my jsp:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%#page import="org.temp.CommonKeys"%>
<div> Method 1:<%=request.getAttribute(CommonKeys.TITLE)%></div>
<div> Method 2:${requestScope[CommmonKeys.TITLE]}</div>
<div> Method 3:${requestScope["SOME_KEY"]}</div>
This is the output I get
Method 1:TEST
Method 2:
Method 3:TEST
Why does Method 2 not work?
<c:set var="TITLE" value="<%=CommmonKeys.TITLE%>" />
Method 2:${requestScope[TITLE]}
Change your code as per above, should be working fine. It works for me.

How can be a JSP ".tag" file documented?

There is a JSP Tag file like this:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# attribute name="attr" required="true" type="java.lang.String" rtexprvalue="true" description="FOOO" %>
<%# attribute name="var" required="true" type="java.lang.String" rtexprvalue="false" description="BAAR" %>
<%# variable name-from-attribute="var" variable-class="java.lang.String" alias="attrValue" scope="AT_BEGIN" %>
<c:set var="attrValue" value="${requestScope[attr]}" />
It seems to be possible to document the attributes. How to add documentation to the tag itself?
The proper place to describe a tag and its properties (including attributes) is the Tag Library Descriptor. The description attribute of the attribute directive is IMHO a left-over "from the dark past" and does not have any real use nowadays.

Strange behavior switching from JSF to JSP2 EL

I am learning JSF and was trying to implement a small exercise on EL (with JSF page and JSP2). i found a strange behavior and completely clueless of the reason behind it. Please find my code below and scenario
Backing bean class
package task2;
public class FavortiteColors_1 {
private String firstName = "Tarun";
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
Below is the snapshot of faces config xml which is required for this example
Faces config.xml
<managed-bean>
<managed-bean-name>favColors_1</managed-bean-name>
<managed-bean-class>task2.FavortiteColors_1</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
JSP Code
<%# taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%# taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD><TITLE>Accessing Bean Properties</TITLE>
<LINK REL="STYLESHEET"
HREF="./css/styles.css"
TYPE="text/css">
</HEAD>
<BODY>
<tr><B>Accessing Bean Properties using JSP 2 EL: version 1<B></tr>
<% System.out.println("Accessing Bean Properties using JSP 2 EL: version 1"); %>
<OL>
<LI>First Name ${favColors.firstName}
</OL>
<BR><BR>
<tr><B>Accessing Bean Properties using JSF 1.1 EL <B></tr>
<% System.out.println("Accessing Bean Properties using JSF 1.1 EL"); %>
<UL>
<LI>First Name = <h:outputText
value="#{favColors.firstName}"/>
</UL>
<BR><BR>
<tr><B>Accessing Bean Properties using JSF 1.1 with JSP2EL: version 2<B></tr>
<% System.out.println("Accessing Bean Properties using JSF 1.1 with JSP2EL: version 2"); %>
<OL>
<LI>First Name ${favColors.firstName}
</OL>
<BR><BR><BR>
<P>
</f:view>
*The output is as below: *
Accessing Bean Properties using JSP 2 EL: version 1
First Name
Accessing Bean Properties using JSF 1.1 EL
First Name = Tarun
Accessing Bean Properties using JSF 1.1 with JSP2EL: version 2
First Name Tarun
With this the background below are my questions
Q1: I am trying to fetch a string from the backing bean using JSF 1 El and JSP 2 EL. I am trying to fetch the name using three expressions. First and 3rd one are JSP2 EL and the funny thing is that the first one is not able to fetch the name where as the third one (with the exactly same syntax is able to fetch the name. Using the SOP, I am getting the that first expression is unable to trigger the backing bean whereas the second and third one are doing it successfully. Why is such a scenario happening? Looking forward for your explanation.
Regards Tarun

How to get an item

JSP tag code is:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# attribute name="items" required="true" %>
${items[0]}
JSP code is:
<%# taglib prefix="t" tagdir="/WEB-INF/tags"%>
<t:input items="${form.items}"></t:input>
Maybe I forgot type of the attribute or something else? Why is the way to access values different in JSP and JSP tag?
The default type of attributes is java.lang.String. If you expect something else, specify the expected type:
<%# attribute name="items" required="java.util.Collection" %>
or
<%# attribute name="items" required="java.lang.Object" %>
for example.

How to get an item from the String[] attribute in JSTL/JSP tag

In plain JSP I can get first item by EL ${form.items[0]}, but in a JSP tag the same expression throws the following exception:
javax.el.PropertyNotFoundException: Could not find property 0 in class
java.lang.String
The value of ${form.items} is [Ljava.lang.String;#315e5b60.
JSP tag code is:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# attribute name="items" required="true" %>
${items[0]}
JSP code is:
<%# taglib prefix="t" tagdir="/WEB-INF/tags"%>
<t:input items="${form.items}"></t:input>
Maybe I forgot type of the attribute or something else? Why is the way to access values different in JSP and JSP tag?
You need to specify the expeded type of the custom tag attribute. By default, it's java.lang.String, and the JSP container coerces the attribute to a string before passing it to your tag. It thus calls toString on your String array.
<%# attribute name="items" required="true" type="java.lang.String[]" %>
or
<%# attribute name="items" required="true" type="[Ljava.lang.String" %>
should do the trick. If neither does, using
<%# attribute name="items" required="true" type="java.lang.Object" %>
should, but it's less clear.

Categories