Thymeleaf expression objects: #session and ${session....} - spring

There are two ways to obtain a session attribute in Thymeleaf:
${#session.getAttribute('attr')}
${session.attr}
What is the difference between both session "objects"? Is there a situation in which one would work and not the other?

#session usage only works in Web Context ie. it is a helper to directly access to the javax.servlet.http.HttpSession object associated with the current request. This is clearly stated in documentation. So you are directly accessing the Context object here not the variable that is defined by the thymeleaf.
$session is a shortcut for accessing session attributes. This is not a context object, but it is a map added to the context as a variable, which is added by thymeleaf explicitly. You can find the relevant information here.

Related

JSF 1.2: Too many small session beans: Is it bad and what the alternatives

There's a JSF 1.2 application with no way to switch to another version/technology in the observable future. It's often needed to show a small (modal) form that needs some state kept across several requests. After the work is done (confirmed or canceled) this state is not needed until the form opens again. There are a lot of such forms and session objects (separate per-form session beans or members of special huge session beans) are used for keeping their state. The sessions may last long enough, probably the whole working day. So a lot of objects unnecessarily load the session scope.
Is there a simple, standard way of cleaning a session object when it's no longer needed? What are your solutions regarding to that?
# Alex,
As you have mentioned that you have multiple view/page that you want to render/preserve in multiple requests and that remain persist until user session is not expire .
This is only because bean scope is session , May be you have did it is to avoid multiple db call to achieve performance just avoid reloading same info from database on each request.
I think you create a collection in user session bean or any other session bean where you find best (as per your choice but I will advise to create New Bean for only this purpose).
In this collection you just put your model data what you want to display on page do not register this bean in context file. persist the object where you required like you have three pages .P1,P2 & P3 and after P3 you want to remove model1 (your pojo)from session then on navigation event just remove model1 from collection .
//Sample code which help to understand what I am saying
#Session
UserBean {
Map tempBean<Obejct,String>=new HashMap<Object,String>();
//just for example suppose you want to load Model1
public Model1 viewP1() {
if(tempBean.get("P1info")==null){//key for P1 view
Model1 m1=db.getP1info();
tempBean.put("P1info",m1);
}
return (m1)tempBean.get("P1info");
}
}
To remove the Model1 data from session just set the value as null for key "P1info" in case of above code, You can use WeakHashMap,If you do not want to remove key from Map.But make sure to delete value part on your trigger of event after which you do not want to persist the Model1 value in session.
I hope this will work in your case .Please let me know in case of any problem in implementation since I have not shared working code ,but only showing concept.
Try to give a look here
link
I would run a remove on all the object once "the work is done".

How do I consume Classes i have stored in Application?

I have defined a class like this:
Class Foo
Public SomePublicProperty
Public Function init(p_somePublicProperty)
set init = Me
SomePublicProperty= p_somePublicProperty
End Function
End Class
And then consumed that in my global.asa Application_OnStart lke this:
Dim fooInstance
Set fooInstance = New Foo.init("Some data")
fooArray = Array(fooInstance)
Application("fooArray") = fooArray
Which works fine but when i get the value back out of the application store on another page i can't get at the property...
fooArray = Application("fooArray")
fooArray(0).SomePublicProperty 'This line returns an error - Object doesn't support this property or method
I have tried putting the class definition into the second page, but it doesn't help.
What have I missed?
I have just found this question. Am I right in assuming the same rule re serialization applies equally to the Application object? and so i shouldn't try and do this?
Unfortunately you can't do this, here is a the best explanation I could find as to why;
From Can I store VBScript class objects in a Session variable?
This is because VBS classes are NOT true classes. They are really just in-memory collections of info, and there is no way to guarantee (for example) that an instance that is stored in one page will even come close to matching the class definition in another page.
This is not the same as using Server.CreateObject() COM objects which can be stored and retrieved from both the Application and Session objects.
You have a couple of options;
Serialise the object yourself, in a structured string then use this to de-serialise the object when needed.
Create a COM wrapper for your VBScript class and stop using Class statement altogether. As COM objects can be stored in Application and Session objects this should work as long as the COM class is single threaded.
Convert your class into an Array and use this instead.
Useful Links
Application Object (IIS)
Setting the Scope of COM Objects in ASP Pages - Giving an Object Application Scope

How to debug an entity's validation mapping in Symfony 2.4?

I would like to determine whether an entity property is required or not.
Does anyone know how to access all of the constraints for a given entity property?
I want to check if the NotBlank constraint is active for a certain propery.
information:
You can check the mapping information for a class (or object) with the help of the service:
validator.mapping.class_metadata_factory
The underlying class is:
Symfony\Component\Validator\Mapping\ClassMetadataFactory
The service provides a method getMetadataFor() that allows you to obtain the active mapping metadata for a class (or object).
This method returns an instance of...
Symfony\Component\Validator\Mapping\ClassMetadata
... that provides a getPropertyMetadata(string $property) method that returns the Metadata for a given property name.
example usage:
Inside a controller (or any other ContainerAware instance) you can do:
$factory = $this->container->get('validator.mapping.class_metadata_factory');
$classMetadata = $factory->getMetadataFor('Your\Bundle\Entity\Name');
$propertyMetadata = $classMetadata->getPropertyMetadata('propertyName');
View the list of Supported Validation Constraints Reference from Symfony web site
You can try ladybug bundle. It is very easy to use and shows in detail and nicely to see all properties and info inside an object.

How many instances of an Action class are created in Struts 1.x

I was searching to know the number of instances created per Action class in Struts 1.x, then I found that it is a Singleton. But I have a doubt: In the action mapping section of struts-config.xml we define the action tag, where there is a scope variable. In that scope variable we can put the value as request, session etc. I wonder that if Action class is a Singleton, then what does this scope variable signify? Does the number of instances per Action class depend upon the scope variable, i.e. if scope is set to "session" the number of instance created depend upon the number of user connected?
The "scope" attribute specifies the scope of the ActionForm only. This allows wizard-like session-based forms to span action mappings, or request-based forms that last only a single request.
There is a single Action instance per mapping (in contrast to an instance per-request, as some frameworks do). Actions should be treated like servlets, and written with thread-safety in mind.

Attaching an object to another context in EF 4

I have a baseclass in my website with a property: CurrentUser.
The get method of this property will create a new context and get a User object from the database based on auth cookie information. So far so good.
But since the context is closed, all I can do outside this, is to call properties directly under User, for example FirstName.
But as soon as I try to get a relation for example, like CurrentUser.UserOffices this won't work since I didn't include UserOffices in the query.
Is there a way to create a new context outside the baseclass which I can attach the CurrentUser object to? I have tried ctx.Attach(CurrentUser) with no luck.
You may wonder why I don't include UserOffices. This is simply because there are very many relations to different tables and I don't want to include them all since it differs between my web pages what relations are needed.
Any ideas?
You can try to Attach your entity and then explicitly load property:
ctx.Attach(CurrentUser);
ctx.LoadProperty(CurrentUser, u => u.UserOffices);
I'm not sure if this works with POCOs.
You can also query for object again with Includes specifing navigation properties you need.
The other choice is simply load UserOffices with Linq-to-entities query restricting where condition to current user.

Resources