using #request.requestURI.equals() in Spring Boot - spring-boot

I am new in Spring Boot, please don't be angry...
If I have a link: "http://localhost:8080/menu" and with what I have coded, I can highlight tag. But when I transfer to another URL, of course it will not work anymore. Is there a way to maintain activating tag if new URLs will look like this? : http://localhost:8080/menu/new/.../new99
<a class="nav-link" th:classappend="${#request.requestURI.equals('/menu')}? 'active': ''" href="/board/boardList?list=5">menu</a>
I really cannot think something better than this. Sorry...

Related

How to load corectly image in thymeleaf (spring boot project)?

I want to display the image after an error has occurred in my spring boot application. I read the documentation and tried several ways and it still doesn't work.
I read the documentation and tried several ways and it still doesn't work.
I try this:
<img th:src="#{/images/error_image.jpg}"/>
and this:
<img src="../static/images/error_image.jpg" th:width="1000" th:src="#{/images/error_image.jpg}"/>
This is my project structure:
project structure
Hovering over a photo with ctrl directs me to a photo, so the path is good.
link
but all the time I see this:
error
[EDIT]
The issue is with below controller method, you should provide mapping below.
#GetMapping // add mapping here
public String showSignUpForm()
{
return "register";
}
Make sure static resource is not configured to different path other static/ at your end.
If you have below configuration in application.properties, remove it.
spring.resources.static-locations=
Make sure you have below configurations in application.properties, spring boot's auto-configuration will set it to resource/static/
spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
It's not an issue but still, change /images/ to images/
<img src="../static/images/error_image.jpg" th:width="1000" th:src="#{images/error_image.jpg}"/>

Spring Boot example for #NotEmpty annotaion

I am developing spring boot application which has freemarker for UI.
I want to use #NotEmpty annotation for radio button. e.g.
"input type="radio"
I can see examples on internet but they are with form tag(spring tag).
Can someone give me example for above without form tag.
You can use
<#spring.formRadioButtons "x.y"/>
<#spring.showErrors "<br>"/>
to display the validation messages.
Have a look at this tutorial:
https://hellokoding.com/form-data-binding-and-validation-example-with-java-spring-boot-and-freemarker/

Spring MVC insert URL to another endpoint in the view

I'm migrating a Play! 1.2 web application and moving to Spring Boot + Spring MVC. Some views contain URLs to other endpoints. For example, I display the book title on the page and next to it I want to add the URL to go the book's details page (e.g. localhost/books/{id}).
In Play! 1.2 the controllers are static, and there is also a Router which can create the full URL for a method belonging to another controller (Router.getFullUrl("BookController.bookDetails", args)), but how do I achieve this with Spring MVC?
Best regards,
Cristian.
If you are trying to get the app/deployed name automatically in .jsp files to make the urls, then please make use of context path. An example below :
<c:set var="context" value="${pageContext.request.contextPath}" />
<script src="${context}/themes/js/jquery.js"></script>
From your requirement "admin.myapp.com","admin-test.myapp.com" are server names right? Something like http://admin.myapp.com/book/{bookId},http://admin-test.myapp.com/book/{bookId}. In Spring app, relative path in jsp can be accessed using pageContext.request.contextPath
I also found the UriComponentsBuilder and ServletUriComponentsBuilder. They are similar to the Play! Framework router and provide methods for building URI's, handling parameters and the query etc. We chose to annotate the controllers' methods using constants and then use the same constants with the UriComponentsBuilder to build back the path and create the request query for GET requests.

How to implement view-based spring security for links?

I have a new project and I want to implement spring-security along with other components of spring framekwork.
I plan to implement spring security into 2 levels, Request URL-level and view-level
For Request URL-Level, I'd use the <intercept-url> tag to restrict URL access only for authorized users.
For View-Level security, I'll use it into two parts of the application;
For the web app menu to restrict menus for users who authorized to.
And inside the pages to restrict some parts of the page for users who authorized to.
The confusion I had is regarding implement spring security for menu links.
Hence I need to use spring taglibs <authorize> tag's url attribute (to reuse<intercept-url> patterns/access combination) , then I'll need to write menu links by hand like this:
<security:authorize url="/admin/superadmin/**" >
Super admin page
</security:authorize>
Where I've the following intercept url rule:
<intercept-url pattern="/admin/superadmin/**" access="hasRole('ROLE_SUPER_ADMIN')" />
The point is, I have all the rules in the Database table, and I want to draw links dynamically based on the roles/links saved in the table.
So, the question is how to draw menu links dynamically, and at the same time still use the <authorize> taglib?
The <authorize> tag can do what you need automatically. I assume your menu JSP looks like (without the security part) :
<c:foreach items="${menus}" var="menu">
<a href=${menu.url}>${menu.label}</a>
</c:foreach>
You can simply add security that way :
<c:foreach items="${menus}" var="menu">
<security:authorize url=${menu.url}>
<a href=${menu.url}>${menu.label}</a>
</security:authorize>
</c:foreach>
The Spring security reference manual says that as you use the namespace the authorize tags creates a dummy web request for the supplied URL and invokes the security interceptor to see whether the request would succeed or fail. This allows you to delegate to the access-control setup you defined using intercept-url declarations within the namespace configuration and saves having to duplicate the information (such as the required roles) within your JSPs

Spring security : dynamic roles

I would like to do something like this in my jsp (using spring and struts):
<security:authorize ifAllGranted="ACCESS_<s:property value='%{#attr.currentQueue}'/>">
Sadly it doesn't seem to work since I can't see the content I should see right now. However, there is no error either.
Any idea whether it should work ? Is the method wrong ?
Solved the problem using another Struts OGNL : ${currentQueue} instead of using a property tag. Which gives :
<security:authorize ifAllGranted="ACCESS_${currentQueue}">

Resources