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/
Related
I am using a spring boot application and have configured using Swagger UI.
I want to know whether we could pre-populate the example value with sample value so we can hit the "Try it out!" button without having to type in some sample values to get a response.
It must be present there.
Is there a way we can do this using annotations or a separate file which Swagger uses?
I am using a spring boot project with springfox-swagger2:2.7.0 and springfox-swagger-ui:2.7.0 with dependencies added using gradle.
Since the #ApiParam properties example and examples are not working (see this issue on GitHub), support for adding examples is limited/non existing.
What you can do for simple parameters (#RequestParam) is to add the #ApiParam annotation with the defaultValue property, like this:
#GetMapping
public List<Foo> findAll(
#RequestParam(required = false)
#ApiParam(defaultValue = "foo") // Put the default value here
String input) {
// ...
}
However, there is no support yet for doing this with #RequestBody parameters.
A possible workaround for #RequestBody parameters is by clicking on the code box at the right side of the Swagger tester, where it says Example value. If you click on it, it will insert that example into the field itself.
Here is a workaround for providing an example:
Inject html into swagger
#ApiParam(
name="whatever",
value="whatever",
defaultValue="none</p><p>Example: xyz</p>"
)
They don't protect against it in the latest version 2.9.2.
I am developing a myCustomTag.tag file to implement my custom tag in spring MVC supported project. I know that the developer using the myCustomTag would use the myCustomTag within the spring provided tag. For E.g. :
<form:form action="..." commandName="...>
<myprefix:mycustomTag ............ />
</form:form>
Is it a good design to use a spring's MVC tag ( let's say form:input tag) within a custom myCustomTag.tag file implementation ?
Absolutely, I don't see a reason why you would not want to use spring tags within a custom tag.
For example, for a large form, you might want to separate sections into their own tags and spring tags are perfectly ok to be used there (ie, if it is syntactically allowed).
I have spring webflow applicaiton, which has a contact info page and while submitting the form, it goes to spring validator.
And executing validations, if any wrong data then we adding a error message to validationcontext. Using ${errorMessags} we list the error messages.
But I have a scenario where I need to show a warning message to user in middle of page.
Either through spring validation or spring flowscope variable in Spring Validator
Is there any way to do it?
Thanks in Advance!!!
Why do you want to do that on validator? Do you want to show that warning in next view?
I guess you could set message in or in to flashScope variable and show it on next page.
In the Spring docs, the section on multipart support has an example of specifying a multipart form as this:
<form method="post" action="upload.form" enctype="multipart/form-data">
This doesn't use the Spring form tags which I am familiar with and instead submits the form to the upload.form action.
How does Spring know what upload.form means? Do you have to create a mapping for it and, if so, where is this done?
upload.form is the URL of the HTTP POST request that is created by this form. It will be relative to the URL of the view that generated the form in the first place, so if the form shown above is rendered by /mysite/myapp/myform.html, it will submit to /mysite/myapp/upload.form
It then becomes a matter of configuring Spring to handle "/upload.form" request accordingly, such as through #RequestMapping if you're using annotation-based Spring MVC. See here for more information.
I am maintaining a Spring MVC Web apps written in spring 2.5. Now I want to add some enhancement into it but
currently encountering some problem.
I created a select box and set the multiple option to true and disabled it also.
The only way to populate the the select is thru making some ajax call.
<form:select path="dataFiles" multiple="true" disabled="true">
</form:select>
publiv class MyData{
private List<String> dataFiles = new ArrayList<String>();
//getters
//setters
}
After the ajax call returns, I automatically select each options. I dont want user to edit this anymore.
But my problem is, during form submit they dont get binded into my commandbean. Is there any workaround for this?
I tried removing the disabled attribute and everything work well, but my problem is, it violates my requirements.
Any hints about this?
Using InitBinder annotation and CustomerCollection utilty provided by Spring framework.
I would write something like below in my controller.
#InitBinder()
public void initBinder(WebDataBinder binder) throws Exception
{
binder.registerCustomEditor(Collection.class, new CustomCollectionEditor());
}
Refer Spring documentation about binders here
http://static.springsource.org/spring/docs/2.5.x/reference/validation.html#beans-beans-conversion