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!
Related
[See below for updates]
I am having a hard time defining a pattern. My colleague says it's adaptor pattern. I'm not sure. We're stuck mainly because we want to correctly name our components.
Question: Is it adapter pattern? If not what is it? If it is something else, is this the best way to implement the idea?
To put it in summary, it is a main component(is this the adapter?) that shares an interfaces with sub-components (are these providers?). The main components decides/orchestrates which of the sub-components are called. The main component behaves as some sort of "wrapper" to call one of the others that have the same interface. The instances of which are injected through the constructor.
Assumptions:
For simplicity, we will ignore DR/IoC for now, but we understand and apply the pattern/principle.
The code is not the best implemented form...feel free to suggest.
My use of the words main/sub does not infer some kind of inheritence...just bad naming on my part, if confusing.
It's language-agnostic, because I love contributions from C# and Java guys, and the knowledge they share.
I am using a Social Networking scenario where a main component gets stats on a hastag and instantiates the appropriate Social Sub Component (
There is a Social Component interface:
ISocialComponent
{
SomeStatsObject GetStats(string hashTag);
}
Social Sub-Components implement ISocialComponent Interface
Twitter Sub-Component
public class TwitterSubComponent : ISocialComponent
{
public SomeStatsObject GetStats(string hashTag)
{
return SomeMethodThatReturnsStatsObject(hashTag);
}
private SomeMethodThatReturnsStatsObject(string hashTag)
{
//... Twitter-specific code goes here
}
}
Facebook Sub-Component
public class FacebookSubComponent : ISocialComponent
{
public SomeStatsObject GetStats(string hashTag)
{
return SomeMethodThatReturnsStatsObject(hashTag);
}
private SomeMethodThatReturnsStatsObject(string hashTag)
{
//... Facebook-specific code goes here
}
}
Instagram Sub-Component
public class InstagramSubComponent : ISocialComponent
{
public SomeStatsObject GetStats(string hashTag)
{
return SomeMethodThatReturnsStatsObject(hasTag);
}
private SomeMethodThatReturnsStatsObject(string hashTag)
{
//... Instagram-specific code goes here
}
}
Main Component
There is a main social component object that calls any one of the Sub-Components (defined below) that implement the shared ISocialComponent interface
public class MainSocialComponent : ISocialComponent
{
//this is an enum
private RequestedNetwork _requestedNetwork{ get; set;}
//the SocialComponent instance is injected outside of this class
private readonly ISocialComponent _socialComponent;
public MainSocialComponent(ISocialComponent socialComponent)
{
_socialComponent = socialComponent;
}
public SomeStatsObject GetStats(string hashTag)
{
return _socialComponent.GetStats(hashTag)
/**** original code, kept for historical purposes****
switch(_requestedNetwork)
{
case RequestedNetwork.Twitter:
var twit = new TwitterSubComponent();
return twit.GetStats(hashTag)
break;
case RequestedNetwork.Facebook:
var fb = new FacebookSubComponent();
return fb.GetStats(hashTag)
break;
case RequestedNetwork.Instagram:
var in = new InstagramSubComponent();
return in.GetStats(hashTag)
break;
default:
throw new Exception("Undefined Social Network");
break;
}*/
}
}
Updates:
I see why some say it is Factory pattern because it is creating objects. I had mentioned that we use an IoC container and DR. It was my mistake to exclude that. I have refactored the code
As others have mentioned, this is part of the Factory/Service pattern which is pretty popular for Dependency Injection and Inversion of Control.
Right now though there is no reason to declare your sub components as non static, since you aren't saving your instances to anything.
So it seems to me unless you have missing code where you add the components to a list or something, you could just do this:
public static class InstagramSubComponent : ISocialComponent
{
public static SomeStatsObject GetStats(string hashTag)
{
return stuff;
}
}
public class MainSocialComponent : ISocialComponent
{
//this is an enum
private RequestedNetwork _requestedNetwork{ get; set;}
private static var Mappings = new Dictionary<string, Func<SomeStatsObject>> {
{ "Twitter", TwitterSubComponent.GetStats },
{ "Facebook", FacebookSubComponent.GetStats },
{ "Instagram", InstagramSubComponent.GetStats }
}
public SomeStatsObject GetStats(string hashTag)
{
return Mappings[hashTag].invoke();
}
}
}
Now if you are doing stuff like actually saving your instances of sub components to a list for later or whatever, then that changes everything. But I am not seeing that so there's no reason not to just make it all static if these methods are simple.
If they are very complex then you'll want to use dependency injection so you can unit test everything proper.
I believe you can extract creation of SubComponent into a Factory and pass this Factory to MainSocialComponent. Inside of the GetStats method, you will call _factory.Create(hashTag); and than call GetStats on the returned object.
This way you'll have factory pattern.
This is definitely not an adapter pattern.
An adapter pattern does the following in most cases :
• Works as a bridge between two incompatible interfaces.
• Allows classes with incompatible interfaces work together
Your case is more like a Factory pattern. You use high level abstraction and return the type of interface/component whenever you need to.
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 have problem with creating constructor, which Jenkins can call for some JSON data originating from a Jelly form,. For testing, I created a minimal Jenkins plugin with mvn hpi:create and following two custom files:
src/main/resources/foo/hyde/jenkins/plugins/OptionalBlockSampleBuilder/config.jelly
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:block>
<table>
<f:optionalBlock name="enableText" title="Enable optional text" checked="${instance.enableText}">
<f:entry title="Optional text" field="text">
<f:textbox />
</f:entry>
</f:optionalBlock>
</table>
</f:block>
src/main/java/foo/hyde/jenkins/plugins/OptionalBlockSampleBuilder.java
package foo.hyde.jenkins.plugins;
public class OptionalBlockSampleBuilder extends hudson.tasks.Builder {
public final String text;
public final boolean enableText;
#org.kohsuke.stapler.DataBoundConstructor
public OptionalBlockSampleBuilder(String text, Boolean enableText) {
this.text = text;
this.enableText = (enableText != null) && enableText;
}
#Override
public boolean perform(hudson.model.AbstractBuild build, hudson.Launcher launcher, hudson.model.BuildListener listener) {
listener.getLogger().println("OptionalBlockSampleBuilder " + enableText + "/" + text);
return true;
}
#hudson.Extension
public static final class DescriptorImpl extends hudson.tasks.BuildStepDescriptor<hudson.tasks.Builder> {
public boolean isApplicable(Class<? extends hudson.model.AbstractProject> aClass) {
return true;
}
public String getDisplayName() {
return "Optional Block Sample";
}
}
}
I'm building against pom.xml parent <groupId>org.jenkins-ci.plugins</groupId><artifactId>plugin</artifactId><version>1.454</version>, and everything builds, Netbeans 6.9.1 launches Debug Jenkins and I get to create a job with this build step. Everything works if I don't check that checkbox, and I get expected OptionalBlockSampleBuilder false/null to job's console output.
But if I do check the checkbox and add text, then saving/applying the job config gives this exception from the depths of Jenkins code, when it tries to call my constructor:
java.lang.RuntimeException:
Failed to instantiate class
foo.hyde.jenkins.plugins.OptionalBlockSampleBuilder
from {
"enableText":{"text":"xx"},
"kind":"foo.hyde.jenkins.plugins.OptionalBlockSampleBuilder",
"stapler-class":"foo.hyde.jenkins.plugins.OptionalBlockSampleBuilder"
}
There has to be a simple fix. I have tried many different changes, and also tried to see how other plugins use it, and finally created this minimal test plugin. How to fix it to make optionalBlock work?
The hint comes from the JSON data:
{
"enableText":{"text":"xx"},
"kind":"foo.hyde.jenkins.plugins.OptionalBlockSampleBuilder",
"stapler-class":"foo.hyde.jenkins.plugins.OptionalBlockSampleBuilder"
}
You can see here that enableText contains a child property, text. That means that the f:optionalBlock is actually expecting an encapsulation of all the fields contained within the block -- when the block is checked, you will receive an instance of the encapsulation field class; when it is unchecked, that field will be null. To use the optionalBlock properly, you would need the #DataBoundConstructor to take in a single nullable class instance that encapsulates the entire optionalBlock. For example:
private String text;
#DataBoundConstructor
public MyClass(EnableTextBlock enableText)
{
if (enableText != null)
{
this.text = enableText.text;
}
}
public static class EnableTextBlock
{
private String text;
#DataBoundConstructor
public EnableTextBlock(String text)
{
this.text = text;
}
}
Notice that the enableText field in this case is actually an instance of EnableTextBlock class, which contains a child property, text. That will satisfy the JSON object that is being sent in the form.
Instead, if all you need is a single field that has a checkbox to enable entry of that field, you might want to consider instead using the f:optionalProperty tag, which will take care of that single-field encapsulation for you. However, in many cases, the optionalBlock is actually needed to configure multiple fields, in which case the encapsulation class--as exampled above--is usually the correct way to go.
The encapsulation class does not have to be a static inner class; it could be a separate class within your package, but the important part is that the DataBoundConstructor should take in an argument that matches the JSON structure being passed from the form.
Or you can add inline tag to optionalBlock like this:
<f:optionalBlock inline="true">
if inline is present, the foldable section will not be grouped into a separate JSON object upon submission.
I have a working Flex/Java application, but if I log out of the channelSet and log back in, in the debug console I am seeing numerous instances of this error:
ReferenceError: Error #1056: Cannot create property smallMessage on mx.messaging.messages.AcknowledgeMessage.
The error itself doesn't seem to interfere with app.
The AcknowledgeMessage class is not my class -- and I don't know why the Java side and Flex side don't match up with regard to properties on their internal classes.
Any help is appreciated.
Versions:
Flex 4.1.0.16076
BlazeDS 4.0.0.14931
Spring-Flex 1.5.0.RELEASE
We are having exactly the same problem in our application. I've managed to hide the error using the following ugly hack.
First, create a class like so:
public class FixedAcknowledgeMessage extends AcknowledgeMessage {
private var _smallMessage : *;
public function FixedAcknowledgeMessage() { }
public function get smallMessage() : * {
return _smallMessage;
}
public function set smallMessage(value : *) : void {
_smallMessage = value;
}
}
And then, in your startup code, replace AcknowledgeMessage with your fixed one:
registerClassAlias("flex.messaging.messages.AcknowledgeMessage", FixedAcknowledgeMessage);
We also do the same hack for the classes ErrorMessage and AsyncMessage, which seem to suffer from the same problem. I have no idea if this hack may have some negative side effects, and I would love to find a more proper fix for it.
don't use same name as primary key what you used in the table name...
Use different name .....
for example......
VO object...
public class ColumnNameVO
{
public var ifId:int;
public var formatId:int;
public var position:int;
public var name:String;
public function ColumnNameVO() { }
}
Table pojo classs:
public class ColumnNameVO
{
public var Id:int;
public var formatId:int;
public var position:int;
public var name:String;
}
I want to acces to an attribute inherited by one of my java classes, in a jsp using jstl 1.2 :
Java :
public class LigneDeclarationBean {
private ELigneDeclaration ligneDecla;
private ETypeFormulaire typeForm;
...
public ELigneDeclaration getLigneDecla() {
return ligneDecla;
}
public class ELigneDeclaration extends ZELigneDeclaration implements Serializable {
...
}
public class ZELigneDeclaration implements Serializable {
private String sLibelle;
...
public String getSLibelle() {
return sLibelle;
}
}
JSP :
<%
List<LigneDeclarationBean> listelignes = (List) request.getAttribute("listeLignes");
// Affichage de la liste des ligneDeclas
for (int i = 0; i < listelignes.size(); i++) {
LigneDeclarationBean ligneDecla = listelignes.get(i);
%>
${ligneDecla.ligneDecla.sLibelle}
The error message :
message: The class 'package.ELigneDeclaration ' does
not have the property 'sLibelle'.
However in scriptlet it works fine
<%=ligneDecla.getLigneDecla().getSLibelle()%> return the right value. Is this a limitation of the jstl?
Is there another way to acces to my attribute using this taglib? This project do not use a presentation framework and jstl seems to be the only taglibs I could use.
That might be because of the single letter in the beginning. Try referring to it as ${A.SLibelle}. (i.e. both letters in upper-case).
That's a bit of a special case with EL, because your getter is getSLibelle(), and the parser seems to be unable to understand whether the field is lower or upper-case.
Your problem is found in your getter method:
The correct way to creating a getter method for sLibelle is:
/**
* #return the sLibelle
*/
public String getsLibelle() {
return sLibelle;
}
(yours have a capital S on your getter method declaration name). You can use Bozho's solution or fix the naming of your getter method.