This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!

Vojislav Kovacevic

+ Follow
since Apr 25, 2019
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Vojislav Kovacevic

I have 2 user roles - user and admin , if I go to a url where any authentication is needed and I am not authenticated - I get redirected to the login page, which is fine.

However, if I am logged in as user, and I try to access an admin only page, I get the home page displayed, but the url stays the same. I want to actually redirect to the home page rather than just display it.

So is there any way to make the accessDeniedPage method redirect, because as I said the home page is displayed but the url does not change.
1 month ago
I did it but it's kind of messy.

First of all, for some weird reason (weird to me at least), the following does not work even if I know for a fact the 2 fields have the same string.

So because the above doesn't work for some reason (by "doesn't work" I mean it doesn't return true when it should), I added another @Transitional field as a flag and rewrote the above like so:

At this point it does return true / false as it should.

And to also display the message below the form input, I added another @Transitional field with validation, and modified the above code:

Now it seems to be working as expected, but if there is a better (shorther) way of doing the same thing I'd much appreciate it!
1 month ago
Nothing, password is its own field and also form input.

And for some reason, if the passwords do not match, I get the error when I iterate through all the errors which is fine, but if they do match, I just realized that I get an exception for some reason - List of constraint violations:[ ConstraintViolationImpl{interpolatedMessage='Passwords must match'

So if they don't match, I get an error n my html file which is fine and correct (just not at the right place), but if they do match I get an exception.

Maybe I am going about checking if both fields match in the wrong way.
1 month ago
I have a registration form which works fine (both the validation and persisting to the database).

However, I have a problem displaying a message for the "confirm password" field.

In my Entity class, I have a field marked with @Transient, because I don't want that field to represent a table column, and then I use @AssertTrue to validate that it matches the password field.

That works fine in the sense that the form does not pass validation if both passwords don't match, and also if I loop through the errors in my html file like so:

I do see the correct message (e.g. "Passwords must match").

But I also have the error messages displaying below each form input, like so:

But since confirmPassword does not have validation applied to it, how can I make it so that it displays like for example for the password field?

I tried something like this which of course didn't work:

what do I need to use instead of confirmPassword in this line

to make it display the error message?
1 month ago
I am using @ControllerAdvice to use shared data accross my application.

It all worked fine until I did an ajax request.

The problem is that the relevant data from @ControllerAdvice is always a step behind.

This is the relevant code from @ControllerAdvice

And my view:

Now, If I use the application normally without ajax, meaning if let's say I add an item to the cart and the page refreshes normally, then it all works as expected, I get the correct values for csize and ctotal, however if I use ajax to hit the appropriate url and then return the view, the csize and ctotal variables are always a step behind, so for example if there are no items added and I add the first item, I don't see a difference via ajax, and then if I add another then the value of csize is 1 (instead of 2), then if I add another it is 2 instead of 3 and so on. But if I refresh the page it is correct.

So basically via ajax the data from @ControllerAdvice is a step behind, any idea why? I mean I basically get "why", because I guess only an html view is returned and there is no real redirect, but can I still achieve proper values somehow?
1 month ago
Hi Stephen,

It's just the logic I went with on the fly.

The ids are there as keys to separate each product (which is what the Cart class contains).

I will think about what you said and try to implement it and see if it's a better solution.

I am always open to better code!
1 month ago
Any way to get rid of unchecked warning when I do this?

I am using @SuppressWarnings("unchecked"), is there a better option in your opinion?
1 month ago
I am saving a HashMap into a session like so:


But how am I supposed to get it out of the session back into a HashMap ?

I am aware that the session data is stored as an object but do not know how to get it into a `HashMap`

I've tried:


But it says

cannot from Object to HashMap

1 month ago
First of all for what it's worth, out of few forums you are the only one that helped me with the correct suggestions, so thanks a lot!

What doesn't work? Do you still get 404 errors?

It all works now, I meant before I set the path.

All static files that already exist (images, css, js) are rendered as is, without needing to specify the path, so why doesn't this apply straight after storing data (uploading the image in my case) as well?

Basically if I just want to just get static data (images, css, js) I don't need to set paths, but if I want to upload files and view them as soon as I upload them then for some reason I need to set the paths for that, why?
1 month ago
OK I finally did it like so:

So is this a must because I am developing locally, and also if I deployed this application, would it be enough to just get rid of this code or would I need to actually modify it so that it works as expected online as well?

Also, why doesn't it work upon redirect (as soon as the image is uploaded), but if I refresh the page after uploading or just go the page before uploading it does work?
1 month ago
Hi Rob and thank you for your time!

Honestly I'm struggling, I come from a PHP background and am not used to all this configuration for simple stuff.

I am using Spring Tools 4 so I do have a class that implements WebMvcConfigurer.

I also have the file.

I've visited the link you gave me however I don't get it, since you know the location of my media folder, any change of you just telling me the correct code for the WebConfig class?

Also, you said that

Path src/main/resources/static/media will not work

- Do you mean that just for serving the images or uploading them as well, since uploading works fine?


I tried the following however it is still not working (I can see the image when I first go to the edit page, but not after submitting the form, basically the same situation as before).

1 month ago
Hi there, I am newish to Spring and Java.

I have an spring boot / thymeleaf app where I can add a product, which contains some product info and an image.

When I edit a product, if I change the image, I redirect back to the same page, however even though the new image is successfully uploaded, it is not showing up (error 404 and type of image is  json), but I can for example inspect the image src and open that source and actually see the image, and also if I again refresh the page (after the initial redirect) the image is there.

So the image is there but it is simply not showing up upon initial redirect.

Relevant code from the POST request:

The GET request (the redirect url)

In the view I render the image like so:

in chrome when I inspect network/img, I can see that upon the redirect the image is of type json(this is when the image is not showing up), and then when I refresh again and the image does show - then the type is jpg

guess the problem is that the image is of type json, why is it of type json, maybe I am not saving the image correctly?
1 month ago