I have the following class:
Types.java:
public class Types {
public static class PC {
public static enum Motherboard {
OPTION1("option 1"),
OPTION2("option 2"),
OPTION3("option 3");
private final String displayValue;
private Motherboard(String displayValue) { this.displayValue = displayValue; }
public String getDisplayValue() { return this.displayValue; }
}
};
};
In my Thymeleaf template I have:
<select name="select-motherboard">
<option th:each="size : ${T(jre.maintainme.utils.strings.Types.PC.Motherboard).values()}" th:value="${size}" th:text="${size.displayValue}"></option>
</select>
However, this doesn't seem to work. If however, I put the Motherboard enum into the Types Class, it does... Is there a way I'm missing to be able to nest enums in classes and use them in Thymeleaf?
SOLUTION:
In order to go into nested classes, you need to add a $ between them. I.e:
${T(jre.maintainme.utils.strings.Types$PC$Motherboard)}
What did work for me was the first thing you tried:
${T(com.my.package.path.Client.Type).values()
However, IntelliJ claims it can't resolve Type, but IntelliJ lies!
How do you reference an constants with EL on a JSP page?
I have an interface Addresses with a constant named URL. I know I can reference it with a scriplet by going: <%=Addresses.URL%>, but how do I do this using EL?
EL 3.0 or newer
If you're already on Java EE 7 / EL 3.0, then the #page import will also import class constants in EL scope.
<%# page import="com.example.YourConstants" %>
This will under the covers be imported via ImportHandler#importClass() and be available as ${YourConstants.FOO}.
Note that all java.lang.* classes are already implicitly imported and available like so ${Boolean.TRUE} and ${Integer.MAX_VALUE}. This only requires a more recent Java EE 7 container server as early versions had bugs in this. E.g. GlassFish 4.0 and Tomcat 8.0.0-1x fails, but GlassFish 4.1+ and Tomcat 8.0.2x+ works. And you need to make absolutely sure that your web.xml is declared conform the latest servlet version supported by the server. Thus with a web.xml which is declared conform Servlet 2.5 or older, none of the Servlet 3.0+ features will work.
Also note that this facility is only available in JSP and not in Facelets. In case of JSF+Facelets, your best bet is using OmniFaces <o:importConstants> as below:
<o:importConstants type="com.example.YourConstants" />
Or adding an EL context listener which calls ImportHandler#importClass() as below:
#ManagedBean(eager=true)
#ApplicationScoped
public class Config {
#PostConstruct
public void init() {
FacesContext.getCurrentInstance().getApplication().addELContextListener(new ELContextListener() {
#Override
public void contextCreated(ELContextEvent event) {
event.getELContext().getImportHandler().importClass("com.example.YourConstants");
}
});
}
}
EL 2.2 or older
This is not possible in EL 2.2 and older. There are several alternatives:
Put them in a Map<String, Object> which you put in the application scope. In EL, map values are accessible the usual Javabean way by ${map.key} or ${map['key.with.dots']}.
Use <un:useConstants> of the Unstandard taglib (maven2 repo here):
<%# taglib uri="http://jakarta.apache.org/taglibs/unstandard-1.0" prefix="un" %>
<un:useConstants className="com.example.YourConstants" var="constants" />
This way they are accessible the usual Javabean way by ${constants.FOO}.
Use Javaranch's CCC <ccc:constantsMap> as desribed somewhere at the bottom of this article.
<%# taglib uri="http://bibeault.org/tld/ccc" prefix="ccc" %>
<ccc:constantsMap className="com.example.YourConstants" var="constants" />
This way they are accessible the usual Javabean way by ${constants.FOO} as well.
If you're using JSF2, then you could use <o:importConstants> of OmniFaces.
<html ... xmlns:o="http://omnifaces.org/ui">
<o:importConstants type="com.example.YourConstants" />
This way they are accessible the usual Javabean way by #{YourConstants.FOO} as well.
Create a wrapper class which returns them through Javabean-style getter methods.
Create a custom EL resolver which first scans the presence of a constant and if absent, then delegate to the default resolver, otherwise returns the constant value instead.
The following does not apply to EL in general, but instead to SpEL (Spring EL) only (tested with 3.2.2.RELEASE on Tomcat 7).
I think it is worth mentioning it here in case someone searches for JSP and EL (but uses JSP with Spring).
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<spring:eval var="constant" expression="T(com.example.Constants).CONSTANT"/>
You usually place these kinds of constants in a Configuration object (which has getters and setters) in the servlet context, and access them with ${applicationScope.config.url}
You can't. It follows the Java Bean convention. So you must have a getter for it.
I'm defining a constant in my jsp right at the beginning:
<%final String URI = "http://www.example.com/";%>
I include the core taglib in my JSP:
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Then, I make the constant available to EL by following statement:
<c:set var="URI" value="<%=URI%>"></c:set>
Now, I can use it later. Here an example, where the value is just written as HTML comment for debugging purposes:
<!-- ${URI} -->
With your constant class, you can just import your class and assign the constants to local variables. I know that my answer is a sort of quick hack, but the question also bumps up when one wants to define constants directly in the JSP.
I implemented like:
public interface Constants{
Integer PAGE_SIZE = 20;
}
-
public class JspConstants extends HashMap<String, String> {
public JspConstants() {
Class c = Constants.class;
Field[] fields = c.getDeclaredFields();
for(Field field : fields) {
int modifier = field.getModifiers();
if(Modifier.isPublic(modifier) && Modifier.isStatic(modifier) && Modifier.isFinal(modifier)) {
try {
Object o = field.get(null);
put(field.getName(), o != null ? o.toString() : null);
} catch(IllegalAccessException ignored) {
}
}
}
}
#Override
public String get(Object key) {
String result = super.get(key);
if(StringUtils.isEmpty(result)) {
throw new IllegalArgumentException("Check key! The key is wrong, no such constant!");
}
return result;
}
}
Next step put instance of this class into servlerContext
public class ApplicationInitializer implements ServletContextListener {
#Override
public void contextInitialized(ServletContextEvent sce) {
sce.getServletContext().setAttribute("Constants", new JspConstants());
}
#Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
add listener to web.xml
<listener>
<listener-class>com.example.ApplicationInitializer</listener-class>
</listener>
access in jsp
${Constants.PAGE_SIZE}
Static properties aren't accessible in EL. The workaround I use is to create a non-static variable which assigns itself to the static value.
public final static String MANAGER_ROLE = 'manager';
public String manager_role = MANAGER_ROLE;
I use lombok to generate the getter and setter so that's pretty well it. Your EL looks like this:
${bean.manager_role}
Full code at https://rogerkeays.com/access-java-static-methods-and-constants-from-el
Yes, you can. You need a custom tag (if you can't find it somewhere else). I've done this:
package something;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.taglibs.standard.tag.el.core.ExpressionUtil;
/**
* Get all class constants (statics) and place into Map so they can be accessed
* from EL.
* #author Tim.sabin
*/
public class ConstMapTag extends TagSupport {
public static final long serialVersionUID = 0x2ed23c0f306L;
private String path = "";
private String var = "";
public void setPath (String path) throws JspException {
this.path = (String)ExpressionUtil.evalNotNull ("constMap", "path",
path, String.class, this, pageContext);
}
public void setVar (String var) throws JspException {
this.var = (String)ExpressionUtil.evalNotNull ("constMap", "var",
var, String.class, this, pageContext);
}
public int doStartTag () throws JspException {
// Use Reflection to look up the desired field.
try {
Class<?> clazz = null;
try {
clazz = Class.forName (path);
} catch (ClassNotFoundException ex) {
throw new JspException ("Class " + path + " not found.");
}
Field [] flds = clazz.getDeclaredFields ();
// Go through all the fields, and put static ones in a Map.
Map<String, Object> constMap = new TreeMap<String, Object> ();
for (int i = 0; i < flds.length; i++) {
// Check to see if this is public static final. If not, it's not a constant.
int mods = flds [i].getModifiers ();
if (!Modifier.isFinal (mods) || !Modifier.isStatic (mods) ||
!Modifier.isPublic (mods)) {
continue;
}
Object val = null;
try {
val = flds [i].get (null); // null for static fields.
} catch (Exception ex) {
System.out.println ("Problem getting value of " + flds [i].getName ());
continue;
}
// flds [i].get () automatically wraps primitives.
// Place the constant into the Map.
constMap.put (flds [i].getName (), val);
}
// Export the Map as a Page variable.
pageContext.setAttribute (var, constMap);
} catch (Exception ex) {
if (!(ex instanceof JspException)) {
throw new JspException ("Could not process constants from class " + path);
} else {
throw (JspException)ex;
}
}
return SKIP_BODY;
}
}
and the tag is called:
<yourLib:constMap path="path.to.your.constantClass" var="consts" />
All public static final variables will be put into a Map indexed by their Java name, so if
public static final int MY_FIFTEEN = 15;
then the tag will wrap this in an Integer and you can reference it in a JSP:
<c:if test="${consts['MY_FIFTEEN'] eq 15}">
and you don't have to write getters!
You can. Try in follow way
#{T(com.example.Addresses).URL}
Tested on TomCat 7 and java6
Even knowing its a little late, and even knowing this is a little hack - i used the following solution to achieve the desired result. If you are a lover of Java-Naming-Conventions, my advice is to stop reading here...
Having a class like this, defining Constants, grouped by empty classes to create kind of a hierarchy:
public class PERMISSION{
public static class PAGE{
public static final Long SEE = 1L;
public static final Long EDIT = 2L;
public static final Long DELETE = 4L;
...
}
}
can be used from within java as PERMISSION.PAGE.SEE to retrieve the value 1L
To achieve a simliar access-possibility from within EL-Expressions, I did this:
(If there is a coding-god - he hopefully might forgive me :D )
#Named(value="PERMISSION")
public class PERMISSION{
public static class PAGE{
public static final Long SEE = 1L;
public static final Long EDIT = 2L;
public static final Long DELETE = 4L;
...
//EL Wrapper
public Long getSEE(){
return PAGE.SEE;
}
public Long getEDIT(){
return PAGE.EDIT;
}
public Long getDELETE(){
return PAGE.DELETE;
}
}
//EL-Wrapper
public PAGE getPAGE() {
return new PAGE();
}
}
finally, the EL-Expression to access the very same Long becomes: #{PERMISSION.PAGE.SEE} - equality for Java and EL-Access. I know this is out of any convention, but it works perfectly fine.
#Bozho already provided a great answer
You usually place these kinds of constants in a Configuration object (which has getters and setters) in the servlet context, and access them with ${applicationScope.config.url}
However, I feel an example is needed so it brings a bit more clarity and spare someone's time
#Component
public Configuration implements ServletContextAware {
private String addressURL = Addresses.URL;
// Declare other properties if you need as also add corresponding
// getters and setters
public String getAddressURL() {
return addressURL;
}
public void setServletContext(ServletContext servletContext) {
servletContext.setAttribute("config", this);
}
}
There is a workaround that is not exactly what you want, but lets you active almost the same with touching scriptlets in a quite minimal way. You can use scriptlet to put value into a JSTL variable and use clean JSTL code later in the page.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# page import="com.whichever.namespace.Addresses" %>
<c:set var="ourUrl" value="<%=Addresses.URL%>"/>
<c:if test='${"http://www.google.com" eq ourUrl}'>
Google is our URL!
</c:if>
I am trying to get a List of custom object of type linked list into html using Sightly. But I a unable to read them in sightly. Sample Code is pasted below:
Java Bean:
public class MiniNavBean {
private String fPath;
private String activeAttr;
public MiniNavBean(String fPath, String activeAttr){
this.fPath = fPath;
this.activeAttr = activeAttr;
}
public String getFpath() {
return fPath;
}
public void setFpath(String fpath) {
this.fPath = fpath;
}
public String getActiveattr() {
return activeAttr;
}
public void setActiveattr(String activeattr) {
this.activeAttr = activeattr;
}
}
Java class which extends WCMUsePojo:
public class MiniNav extends WCMUsePojo {
private List<MiniNavBean> navList;
MiniNavBean miniNav;
public List<MiniNavBean> getNavList() {
return navList;
}
public void setNavList(List<MiniNavBean> navList) {
this.navList = navList;
}
#Override
public void activate() {
navList = new LinkedList<MiniNavBean>();
fPath = "fpaths";
activeAttr = "activeattrs;"
miniNav = new MiniNavBean(fpath, activeattr);
navList.add(miniNav);
}
}
Html file (Sightly):
<div data-sly-include="/apps/project/components/global.jsp"></div>
<div data-sly-use.mininav="com.components.MiniNav" data-sly-unwrap>
<div data-sly-list.navlist="${mininav.navList}">
<li>
<p>${navlist.fPath}</p>
<p>${navlist.activeAttr}</p>
</li>
</div>
When I am trying to execute the above code, I am able to see the linked list getting instantiated with the data in the java class. However when I am trying to display the values of the list in the front end, sightly is unable to read it.
Since the LinkedList is of CustomObject type(MiniNavBean) I suspect sightly is unable to read it as it doesn't know about this bean because we didn't refer that bean anywhere. Is there a way to fix this using sightly tags and read the data ?
Sightly would loop over Java objects too. I don't think it is issue with Sightly. Looks like your getters are wrong. Change your bean as shown below
public class MiniNavBean {
private String fPath;
private String activeAttr;
public MiniNavBean(String fPath, String activeAttr){
this.fPath = fPath;
this.activeAttr = activeAttr;
}
public String getfPath() {
return fPath;
}
public void setfPath(String fPath) {
this.fPath = fPath;
}
public String getActiveAttr() {
return activeAttr;
}
public void setActiveAttr(String activeAttr) {
this.activeAttr = activeAttr;
}
}
If you do not wish to change the bean, then you can access the getters directly in the Sightly file and check if it is working fine.
<div data-sly-include="/apps/project/components/global.jsp"></div>
<div data-sly-use.mininav="com.components.MiniNav" data-sly-unwrap>
<div data-sly-list.navlist="${mininav.navList}">
<li>
<p>${navlist.getFpath}</p>
<p>${navlist.getActiveattr}</p>
</li>
</div>
EDIT: To explain more based on the comments
You cannot access the fields which are private outside the class and are usually done using the public getter methods. However, in Sightly when you use the field name after the dot operator, you are not accessing the field directly, instead it calls the corresponding getter method based on the Java specification for naming getters / setters. So as per spec, your getters and setters were wrong in the bean due to which it didn't work.
Like I mentioned above, you can change only your bean and your code will work fine. Or you can leave your bean as is and change Sightly code to get things working.
In your example you are neither assigning a value to the navList member of MiniNav nor adding the MiniNavBean instance to it.
Add the following lines to your activate() method:
this.navList = new LinkedList<>();
this.navList.add(navList);
Also, the Java getters and HTL/Sightly accessors are not properly aligned, ie: for getFpath() you should use navlist.fpath
In case you already have those, are you getting any compile or runtime errors from HTL/Sightly?
HTL/Sightly is generally using reflection to lookup properties of objects so it does not care much about their type.
I am trying to create a simple DB web application using MySQL, JDBC, Netbeans.
I have the following code in a .jsp page
<c:if ${sessionScope.Staff.getStatus() == sessionScope.Staff.financial_staff_status} >
Financial staff
</c:if>
where sessionScope.Staff contains an object of Staff Data type:
public class StaffData
{
//constants
public final byte default_staff_status = 0;
public final byte financial_staff_status = 1;
public final byte legal_staff_status = 2;
public final byte secretarial_staff_status = 3;
//other data
StaffData()
{
//initializations
}
void authenticate(int staff_num, String passwd) throws ClassNotFoundException, SQLException
{
//connect to sever, blah, blah
}
public String getName()
{
return this.name;
}
public int getNumber()
{
return this.staff_number;
}
public byte getStatus()
{
return this.status;
}
}
I am setting the session object in advance:
request.getSession().setAttribute("Staff", currentStaff);
I am getting the following error:
javax.el.PropertyNotFoundException: Property 'financial_staff_status' not found on type staff.StaffData
In the staff data object in the session, public methods such as getName() can be accessed, but public members such as financial_staff_status cannot.
Why am I getting this problem? The problem seems to be with the final variables. Non final variables can easily be accessed without a problem.
You actually have three problems with the EL expression:
<c:if ${sessionScope.Staff.getStatus() == sessionScope.Staff.financial_staff_status} >
The conditional expression to be evaluated should be within the mandatory test attribute
The property status should be accessed as Staff.status as it already has a public getter method
The property financial_staff_status requires a public getter method in class StaffData. EL is strict with javabeans compliance of object classes and how properties are accessed (must be via a public getter)
Additionally, its not strictly necessary to qualify the scope of an attribute, unless you have multiple attributes with the same name in different scopes or wish to be explicit for clarity. The different scopes will be searched starting with the narrowest (pageScope) through to the widest (applicationScope).
Having added the public getter for the financial_staff_status property to your class, the expression should be:
<c:if test="${sessionScope.Staff.status == sessionScope.Staff.financial_staff_status}">
or simply:
<c:if test="${Staff.status == Staff.financial_staff_status}">
Is it possible to overload an EL method in JSF 1.1 using Facelets as your view handler? If so, how?
For example, I have this code defining my EL methods (which are defined in namespace k):
public static String doStuff( String s ) {
return doStuff( null, s );
}
public static String doStuff( Map<String,String> m, String s ) {
...
return something;
}
When I try to call #{k:doStuff("hey!")} from my Facelets page, I get this error:
Function 'k:doStuff' specifies 2 params, but 1 was declared
It turns out that no matter how you declare the function, it is being put in a Map with its name used as a key. So - no function overloading.
However, you can define the name of the function in the XML to be different, and then you can have overloaded methods. It's a bit counter-intuitive. But then your functions will be accessible via different names in the pages.
You can achieve the same thing dynamically, by supplying suffixes to overloaded method names (which you put in the map). Either way it's not such a good solution.
It looks like the problem was with how it was declared. For example, I was using this to declare my methods:
public class KTagLib extends AbstractTagLibrary {
public static final String NAMESPACE = "http://mysite.blah/tags";
public static final KTagLib INSTANCE = new KTagLib();
public KTagLib() {
super(NAMESPACE);
try{
try {
Method[] methods = KTags.class.getMethods();
for (int i = 0; i < methods.length; i++) {
if (Modifier.isStatic(methods[i].getModifiers())) {
this.addFunction(methods[i].getName(), methods[i]);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
and using the following configuration:
<?xml version="1.0"?>
<!DOCTYPE facelet-taglib PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
<facelet-taglib>
<library-class>mypackage.KTagLib</library-class>
</facelet-taglib>
However, the this.addFunction() is essentially calling put() on a java.util.Map object so that duplicate methods can't be added since the keys are the same between doStuff.
To solve this problem, I'll have to explicitly declare the methods in the *.taglib.xml unless anyone knows of a way to dynamically solve the problems.