Spring Form Validation Implementation - validation

My question is, what is the use of the springForm:form tag?
Why the prefix springform ??
Why can't I use my usual form tag??
Can someone tell me the difference?
I visited this link, but I'm unable to understand. Can someone explain it better?

A P it is just a custom tag reference.
<%# taglib prefix="springForm" uri="http://www.springframework.org/tags/form"%>
In above line one should informing that I am going to use spring tag using prefix springForm so that you will avail all spring tags support in your jsp.
And you can use normal form, but you won't avail the functionalities of spring tags.
Example path attribute is not supported in normal form,but it is supported in spring,so no need to map(binding),Spring will automatically map the value to mentioned path variable.

Related

Use of spring security jstl tag <sec:authority access="hasPermission(#domain, 'permission')> inside a jstl core loop

I'm using facelets (JSF2.1) and I'm trying to do something like:
<c:forEach var="domainObject" items="#{MB.listOfDomainObjects}">
<sec:authorize access="hasPermission(#domainObject,'PERMISSION_X')">
hello world
</sec:authorize>
</c:forEach>
I've tried changing #domainObject by #domainObject, #{domainObject}, $domainObject and a lot of other combinations with the same result: domainObject is not processed correctly. Sometimes I get an error saying the page cannot be constructed, others domainObject is null.
It's like the scope where the tag c:forEach puts the variable domainObject is not scanned by sec:authorize to find it.
I've tried also to force the use of a scope using the tag <c:set ... scope="view"/>. I've also tried to use <ui:repeat> with the same results, but considering the tag sec:authorize is a jstl one, I suppose is executed in build time (like c:forEach) and not in render time (like ui:repeat), so I think I must use c:foreach and not ui:repeat.
Any chance to solve my problem?

Can Thymeleaf do localized template lookups like Freemarker?

Freemarker (by default) uses the locale to build the file names it looks for when loading and including templates. For example, loading tos.ftl (the template) with the en_US locale would look for:
tos_en_US.ftl
tos_en.ftl
tos.ftl
This can be useful to translate whole pages when the pages are completely different between different languages. For example, a "Terms of Service" page might be mostly static so different languages would have completely different content. In this case, it is a hassle to externalize the whole content to messages loaded from message bundles.
I am now learning Thymeleaf and can't find any information about a similar functionality. I know that Thymeleaf uses localized message bundles to fill in th:text elements, but can it load localized versions of the template files?
Note: I'm using Spring Boot
I know it's a little bit too late but here is how I would attempt to solve this using the current version of Thymeleaf:
Presuming you have these 2 localised templates under path (from the template root) path/to/template:
localized_template_en.html
localized_template_ru.html
In your SomeController.class:
#GetMapping( "/locale_test" )
public String getLocalisedPage( Model model ) {
return "path/to/template/localized_template_"
+ LocaleContextHolder.getLocale().getLanguage();
}
As a result you will get the correct template given you use Spring localisation features (such as LocaleChangeInterceptor). If not - instead of using LocaleContextHolder implement your custom logic for postfix selection.
Thymeleaf's behaviour the same as Spring 4 MVC Internationalization (I guess you using Thymeleaf with Spring??), it uses messages.properties to realize that.
For example you have a template with #{hello} message:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title></title>
</head>
<body>
<span th:text="#{hello}">
</body>
#{hello} text will be binded to the messages.properties proprty hello.
If you locale would be another, e.g. ru_RU you just add messages_ru_RU.properties and change the locale of your application.
After that, your message will be taken from localized properties file.
Note that necessarily to have messages.properties file if you using localized messages file.

Persist url parameters across requests

I am using Spring MVC. There is a requirement that some user selections remain globally and always in the url parameter. I might also be able to remove it with code at will.
Is there something like Persistent Page Data(like in Tapestry http://tapestry.apache.org/persistent-page-data.html) for Spring MVC.
A link to a similar quesion thats unanswered: http://osdir.com/ml/java.appfuse.user/2005-08/msg00507.html
Thanks
Update
Eventually I used a simple technique, where:
1. I would capture the current page url with query paramters.
2. Add or replace the new parameters into this url.
<c:set var="contextPath" value="${pageContext.request.contextPath}"></c:set>
<c:set var="servletPath" value="${requestScope['javax.servlet.forward.servlet_path']}"></c:set>
<c:set var="currentPath" value="${contextPath}${servletPath}"></c:set>
ageone
where replaceUrlParameter() uses regex to replace or add the query parameter.
Keep this parameters in session and write the Servlet Filter that would add them to any request or would modify request URL so they are visible in URL.

How do I do substitutions for localised strings in freemarker

I'm using spring and freemarker and have the basics working.
I've got a properties file like
help.text=For further information please see the help page.
I'm currently outputting localised messages using
${rc.getMessage("help.text")}
However I'm having trouble figuring out how I can pass in my substitution variables. Can you help?
Cheers,
Peter
If I read the Spring API documentation about RequestContext (your rc?) correctly, then
${rc.getMessage("help.txt", ["yourHelpUrl"])}
might work, because getMessage can receive an additional List argument with message args, which you can supply via a FreeMarker sequence.
<#import "/spring.ftl" as spring/>
<#assign args = ["yourHelpUrl"]>
<#spring.messageArgs "help.txt" args/>
I always do the substitution variables in my Java code somewhere and then dump the fully localized text into a Map where it's accessed by Freemarker like this:
${localizedValues["help.txt"]}
If you are correctly configuring freemarker in the spring MVC context then the right way to do it is:
Import the spring macros in the template
Use the mensaje macro
<#import "/spring.ftl" as spring />
<#spring.messageText "code", "Default message"/>
See the documentation: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/view.html#view-velocity

how spring mvc tag works?

I am trying to write some kind of raw html to mimic what spring mvc tag produces after page rendering(and I do make them look exactly the same if you open them with a html element inspector). as I want to create dynamic input form using javascript. but it didn't work. it seems I had to use only what it offers: e.g. <form:input path="firstName" />.
to get the data binding working.
I thought the tag lib only help you to produce a html block that spring knows how to handle them in backend (action). from a web http perspective. what else it can send beyond a bunch of form data, and they should send the same thing. so I am really curious to learn what magic thing the tag lib dose beyond producing a html block.
the other thing I would like to know is where the model object is being hold when the form is being submit to a matched action. you know, you can get the model attribute by using #modelAttribute as the input parameter. is it in the original request object? or in the ActionRequest to which the dispatcherServlet build and put it. or even somewhere else?
thanks in advance.
I got it figured out. the raw html just works as spring tag does. as long as you are in the form tag block. you are okay to use raw html such as
<input type="text" id="abc" name="abc"/> just make sure name reflect your bean attribute path.
id is not mandatory and is just helping you to identify the very element. I guess I missed something when I work with the raw html by the time I ask the question. hope this helps for guys working with raw html approach, especially in case of dynamic input creation.

Resources